Repository navigation
Implement constant expression handling for enum members and private const fields - #97
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The new enum-member constant emission uses field.Name without keyword escaping, which can produce uncompilable generated code for valid enums containing escaped identifiers (e.g. @class).
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
This PR updates the source generator to inline enum members and const fields as Expression.Constant(...) nodes (instead of emitting reflective Expression.Field(... GetField(...)) access), aligning generated trees with C# compiler semantics and avoiding query-provider funcletization into parameters (per issue #95).
Changes:
- Inline enum member access and
constfields asExpression.Constant(...)during expression tree emission. - Add/adjust generator snapshot tests to validate constant emission for enums and private const fields.
- Add integration tests to ensure expanded expressions (and EF Core SQL) no longer contain literal-field
MemberExpressionaccess patterns.
| File | Description |
|---|---|
src/ExpressiveSharp.Generator/Emitter/ExpressionTreeEmitter.cs |
Inline constant fields/enum members into Expression.Constant to avoid reflective field access. |
tests/ExpressiveSharp.IntegrationTests/Tests/EnumComparisonTests.cs |
Adds runtime validation + tree-shape assertion ensuring no literal field-member access remains for enum-to-enum switch mapping. |
tests/ExpressiveSharp.EntityFrameworkCore.IntegrationTests/Tests/Sqlite/CommonScenarioTests.cs |
Adds SQL assertion that enum-switch translation emits CASE and avoids parameterization. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.cs |
Adds generator tests covering enum-to-enum switch mapping and private const fields. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.EnumToEnumSwitchMapping.verified.txt |
New snapshot verifying enum switch emits enum constants (not reflected fields). |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.PrivateConstFields.verified.txt |
New snapshot verifying private const fields are emitted as constants (including enum-typed const). |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/SwitchPatternTests.SwitchStatementWithReturnArms.verified.txt |
Updates snapshot to expect enum constants rather than reflective field access. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/MethodTests.ConstMember.verified.txt |
Updates snapshot to expect const field emission as Expression.Constant. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/LoopTests.ForEach_Min.verified.txt |
Updates snapshot to expect int.MaxValue emitted as a constant literal. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/LoopTests.ForEach_ConditionalMinWithMemberAccess.verified.txt |
Updates snapshot similarly for int.MaxValue. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.EnumRelationalPattern.verified.txt |
Updates snapshot to emit enum constants rather than reflective field access. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.EnumRelationalComparisonWithNullableEnum.verified.txt |
Updates snapshot to emit enum constants rather than reflective field access. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/EnumTests.EnumRelationalComparison.verified.txt |
Updates snapshot to emit enum constants rather than reflective field access. |
tests/ExpressiveSharp.Generator.Tests/ExpressiveGenerator/ConstructorTests.ProjectableConstructor_ReferencingStaticConstMember.verified.txt |
Updates snapshot to emit string const field as Expression.Constant. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |

fixes #95