fix(csharp-codegen): escape C# reserved keywords in generated identifiers#4535
Conversation
|
Thank you for opening a PR for this! We'll work on getting it reviewed. |
…iers - Add Identifier property to MemberDeclaration that prefixes C# keywords with @ - Use SyntaxFacts.GetKeywordKind to detect reserved keywords - Update all code generation sites to use Identifier instead of Name - Add test for keyword field names (class, params) in tables/reducers/procedures Fixes clockworklabs#4529
0bd05ba to
e49364a
Compare
|
I’ve pushed an update that rebases this PR onto current and resolves merge conflicts while preserving the C# keyword-escaping changes.\n\nWould appreciate a fresh pass when you have a moment. Thanks! |
|
Follow-up correction: rebased onto latest master and resolved conflicts. |
|
CLA is now signed ✅. Could a maintainer please re-run/approve the PR workflows? The latest workflow runs are currently in action_required state from the fork-trigger guard. |
… and lint issues
* Changed `IEquatable<{ShortName}>` to `IEquatable<{ShortNameIdentifier}`> to handle keyword type names.
* Fixed `GenerateReadOnlyAccessorFilters()` to use `identifierName` (escaped) instead of raw `name`
* Used different reserved keywords `event` and `params` in test to avoid `CS0542` and `CS0229` errors for duplicate accessor names.
* Corrected some lint issues.
rekhoff
left a comment
There was a problem hiding this comment.
Reviewed and tested changes. Looks good to me. I have pushed a few changes on some issues, and now all CI jobs are passing. I am marking this PR as approved to merge. I apricate the test being included, thanks for the submission!
31b9af3
Summary
This PR fixes an issue where C# reserved keywords (like
params,class,event, etc.) used as field or parameter names in SpacetimeDB types would cause compilation errors in the generated code.Problem
When a user defines a table or reducer with a field named using a C# reserved keyword:
The codegen would generate invalid C# like:
Solution
Added an
Identifierproperty toMemberDeclarationin the codegen that automatically detects C# reserved keywords usingSyntaxFacts.GetKeywordKind()and prefixes them with@when needed.Updated all code generation sites to use
Identifierinstead ofNamewhen generating:Added a regression test that verifies tables, reducers, and procedures with keyword field names compile successfully.
Test Plan
CSharpKeywordIdentifiersAreEscapedInGeneratedCodetest that creates a table with@classand@paramsfields, plus a reducer and procedure with keyword parametersFormerlyForbiddenFieldNamesfixture which already tests edge cases likeRead,Write,GetAlgebraicType)Fixes #4529