-
Notifications
You must be signed in to change notification settings - Fork 9
chore: Parse identifiers for __ctHelpers usage rather than a naive string check #1896
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,13 +79,8 @@ export class CTHelpers { | |
| export function transformCtDirective( | ||
| source: string, | ||
| ): string { | ||
| // Throw when this symbol name is already in use | ||
| // for some reason. | ||
| if (source.indexOf(CT_HELPERS_IDENTIFIER) !== -1) { | ||
| throw new Error( | ||
| `Source cannot contain reserved '${CT_HELPERS_IDENTIFIER}' symbol.`, | ||
| ); | ||
| } | ||
| checkCTHelperVar(source); | ||
|
|
||
| const lines = source.split("\n"); | ||
| if (!lines[0] || !isCTSEnabled(lines[0])) { | ||
| return source; | ||
|
|
@@ -101,6 +96,27 @@ function isCTSEnabled(line: string) { | |
| return /^\/\/\/\s*<cts-enable\s*\/>/m.test(line); | ||
| } | ||
|
|
||
| // Throws if `__ctHelpers` was found as an Identifier | ||
| // in the source code. | ||
| function checkCTHelperVar(source: string) { | ||
| const sourceFile = ts.createSourceFile( | ||
| "source.tsx", | ||
| source, | ||
| ts.ScriptTarget.ES2023, | ||
| ); | ||
| const visitor = (node: ts.Node): ts.Node => { | ||
| if (ts.isIdentifier(node)) { | ||
| if (node.text === CT_HELPERS_IDENTIFIER) { | ||
| throw new Error( | ||
| `Source cannot contain reserved '${CT_HELPERS_IDENTIFIER}' symbol.`, | ||
| ); | ||
| } | ||
| } | ||
| return ts.visitEachChild(node, visitor, undefined); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. claude suggests that you could consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| }; | ||
| ts.visitNode(sourceFile, visitor); | ||
| } | ||
|
|
||
| function getCTHelpersIdentifier( | ||
| statement: ts.Statement, | ||
| ): ts.Identifier | undefined { | ||
|
|
||
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.
nice, it's very cool that this is actually faster than the string check - i for sure would not have guessed that!