-
Notifications
You must be signed in to change notification settings - Fork 188
fix: wrap invalid variable names in quotes in ts type generation #1189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe TypeScript type-generation template was updated to conditionally quote property keys for collection attributes when the key is not a valid TypeScript identifier. It introduces helper variables to compute the final property name (respecting strict mode) and to validate identifier syntax, emitting either an unquoted identifier or a quoted string key accordingly. The emitted types for attributes remain unchanged aside from key quoting. This affects the generated exported type blocks for collections in the produced TypeScript declarations. ✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
templates/cli/lib/type-generation/languages/typescript.js.twig (1)
94-94
: Broaden identifier check to support Unicode identifiers.TS allows Unicode ID_Start/ID_Continue. Current ASCII-only regex may over-quote valid keys (e.g.,
ümlaut
). Consider a Unicode-aware pattern.Suggested change:
-<% const isValidIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propertyName); -%> +<% const isValidIdentifier = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u.test(propertyName); -%>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
templates/cli/lib/type-generation/languages/typescript.js.twig
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: build (8.3, Swift56)
- GitHub Check: build (8.3, DotNet80)
- GitHub Check: build (8.3, WebNode)
- GitHub Check: build (8.3, WebChromium)
- GitHub Check: build (8.3, AppleSwift56)
- GitHub Check: build (8.3, Node20)
- GitHub Check: build (8.3, Python313)
- GitHub Check: build (8.3, Ruby30)
- GitHub Check: build (8.3, Python39)
- GitHub Check: build (8.3, Python312)
- GitHub Check: build (8.3, Node18)
- GitHub Check: build (8.3, Go118)
- GitHub Check: build (8.3, KotlinJava8)
- GitHub Check: build (8.3, Python311)
- GitHub Check: build (8.3, Node16)
- GitHub Check: build (8.3, FlutterStable)
- GitHub Check: build (8.3, FlutterBeta)
- GitHub Check: build (8.3, Android14Java17)
- GitHub Check: build (8.3, Android5Java17)
🔇 Additional comments (1)
templates/cli/lib/type-generation/languages/typescript.js.twig (1)
93-95
: Confirm strict-mode intent for renamed keys.Quoting happens after
toCamelCase
whenstrict
is true, so keys likefoo-bar
becomefooBar
(unquoted). If you intended to preserve original text when invalid (even in strict), consider toggling quoting before rename. Otherwise, this behavior is fine.
<%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute, collections) %>; | ||
<% const propertyName = strict ? toCamelCase(attribute.key) : attribute.key; -%> | ||
<% const isValidIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(propertyName); -%> | ||
<% if (isValidIdentifier) { %><%- propertyName %><% } else { %>"<%- propertyName %>"<% } %>: <%- getType(attribute, collections) %>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote keys safely to avoid invalid d.ts (escape quotes/newlines).
If an attribute key contains a "
or control chars, "${propertyName}"
will break. Use JSON.stringify
to output a valid TS string literal.
Apply this diff:
- <% if (isValidIdentifier) { %><%- propertyName %><% } else { %>"<%- propertyName %>"<% } %>: <%- getType(attribute, collections) %>;
+ <% if (isValidIdentifier) { %><%- propertyName %><% } else { %><%- JSON.stringify(propertyName) %><% } %>: <%- getType(attribute, collections) %>;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
<% if (isValidIdentifier) { %><%- propertyName %><% } else { %>"<%- propertyName %>"<% } %>: <%- getType(attribute, collections) %>; | |
<% if (isValidIdentifier) { %><%- propertyName %><% } else { %><%- JSON.stringify(propertyName) %><% } %>: <%- getType(attribute, collections) %>; |
🤖 Prompt for AI Agents
In templates/cli/lib/type-generation/languages/typescript.js.twig around line
95, the template currently emits property keys using raw double quotes which
will break if the attribute name contains quotes or control characters; replace
the naive quoted form with JSON.stringify(propertyName) when the name is not a
valid identifier so the output is a valid, escaped TypeScript string literal
(i.e., use JSON.stringify to produce the quoted/escaped key), keeping the
identifier branch unchanged and passing attribute/collections to getType as
before.
What does this PR do?
(Provide a description of what this PR does.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)
Related PRs and Issues
(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)
Have you read the Contributing Guidelines on issues?
(Write your answer here.)
Summary by CodeRabbit