-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(typescript-estree): if the template literal is tagged and the text has an invalid escape, cooked will be null
#11355
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
base: main
Are you sure you want to change the base?
Changes from all commits
cbe8cc9
a1902bb
4938a14
77960bf
c11a244
cd7550f
156b384
6103105
cbeb8d7
954e84e
b5bc96d
8fdb4db
2dc7c7b
2e5857a
8279f15
49dcf05
84ec93c
814604f
888e312
4c1891c
2a31006
27e9b9a
30ed17f
3d5888d
4d5d413
5e6e6b7
1ada433
a051525
a7311e6
8a67d05
b85dda1
5b43ca8
c1ee701
547a239
f151aeb
64628a3
fd02eb4
bcd84fa
58ab6ae
2f95549
6fb4c78
10cdcd2
e31cdbc
f43e69b
800deba
42368ec
d86ddbc
66b757d
1d73e5b
2d05e47
50871ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ export default createRule({ | |
| } else if (isNumberLiteral(member.initializer)) { | ||
| value = member.initializer.value; | ||
| } else if (isStaticTemplateLiteral(member.initializer)) { | ||
| value = member.initializer.quasis[0].value.cooked; | ||
| // cooked can only be null inside a TaggedTemplateExpression, which is not possible. | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| value = member.initializer.quasis[0].value.cooked!; | ||
|
Member
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. Suggestion: Use |
||
| } | ||
|
|
||
| if (value == null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,7 +202,11 @@ export default createRule({ | |
| receiverProperty.key.type === AST_NODE_TYPES.TemplateLiteral && | ||
| receiverProperty.key.quasis.length === 1 | ||
| ) { | ||
| key = receiverProperty.key.quasis[0].value.cooked; | ||
| const cooked = receiverProperty.key.quasis[0].value.cooked; | ||
| if (cooked == null) { | ||
|
Member
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. This is an impossible condition (and the test provided doesn't reach it). This branch refers to keys whose node type is a Suggestion: |
||
| continue; | ||
| } | ||
| key = cooked; | ||
| } else { | ||
| // can't figure out the name, so skip it | ||
| continue; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -401,6 +401,16 @@ export class Converter { | |
| } | ||
| } | ||
|
|
||
| #isValidEscape(text: string): boolean { | ||
|
Member
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. 😬 this and But, can we get away with checking if
Contributor
Author
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 So, find package is better. I found unraw package that parse escape chr and throws err when it is invalid. (I wanted to compare different packages, but I couldn't find any other suitable ones.) I committed.
Contributor
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. Maybe we can eval I see they are stored in
Contributor
Author
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. Ah, that's better. Thank you. For me, it's a much lighter and simpler way than adding dependencies, so I committed it. However, I know that eval has performance and security issues. Will this be okay? @JoshuaKGoldberg p.s I tried to use ts.createSourceFile(
'temp.ts',
`const str = \`${text}\`;`,
ts.ScriptTarget.Latest,
);convert > tagged template literal cooked > should set cooked to null for invalid escape sequences in tagged template literals
AssertionError: expected '\n\uXXXXᄑ\t' to be null
- Expected:
null
+ Received:
"
\\uXXXXᄑ "
convert > tagged template literal cooked > should set cooked to null for mixed valid and invalid escape sequences
AssertionError: expected '\uXXXX\xQW' to be null
- Expected:
null
+ Received:
"\\uXXXX\\xQW"
Member
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 I should defer to @bradzacher on this. I'm quite weary of taking on
Contributor
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. IMO, correctness is top priority for a "parser", if performance is important, why don't we always set it to
Contributor
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. Maybe we can ask ESTree to remove
Contributor
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. Raised an issue in ESTree estree/estree#333
Member
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'd agree -- but we're not a parser! TypeScript is the parser -- we're just an intermediary on top of it. Because it's an edge case that clearly few people use/care about I think that a bit of leeway to trade-off perf with "mostly correct" behaviour is fine, esp when the alternatives are:
Contributor
Author
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 committed as suggested by @bradzacher.
|
||
| if (/\\[xu]/.test(text)) { | ||
| const hasInvalidUnicodeEscape = /\\u(?![0-9a-fA-F]{4}|{)/.test(text); | ||
| const hasInvalidHexEscape = /\\x(?![0-9a-fA-F]{2})/.test(text); | ||
|
|
||
| return !hasInvalidUnicodeEscape && !hasInvalidHexEscape; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| #throwError(node: number | ts.Node | TSESTree.Range, message: string): never { | ||
| let start; | ||
| let end; | ||
|
|
@@ -1894,7 +1904,12 @@ export class Converter { | |
|
|
||
| // Template Literals | ||
|
|
||
| case SyntaxKind.NoSubstitutionTemplateLiteral: | ||
| case SyntaxKind.NoSubstitutionTemplateLiteral: { | ||
| const rawText = this.ast.text.slice( | ||
| node.getStart(this.ast) + 1, | ||
| node.end - 1, | ||
| ); | ||
|
|
||
| return this.createNode<TSESTree.TemplateLiteral>(node, { | ||
| type: AST_NODE_TYPES.TemplateLiteral, | ||
| expressions: [], | ||
|
|
@@ -1903,15 +1918,17 @@ export class Converter { | |
| type: AST_NODE_TYPES.TemplateElement, | ||
| tail: true, | ||
| value: { | ||
| cooked: node.text, | ||
| raw: this.ast.text.slice( | ||
| node.getStart(this.ast) + 1, | ||
| node.end - 1, | ||
| ), | ||
| cooked: | ||
| node.parent.kind === SyntaxKind.TaggedTemplateExpression && | ||
| !this.#isValidEscape(rawText) | ||
| ? null | ||
| : node.text, | ||
| raw: rawText, | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| case SyntaxKind.TemplateExpression: { | ||
| const result = this.createNode<TSESTree.TemplateLiteral>(node, { | ||
|
|
@@ -1938,32 +1955,43 @@ export class Converter { | |
| 'Tagged template expressions are not permitted in an optional chain.', | ||
| ); | ||
| } | ||
| return this.createNode<TSESTree.TaggedTemplateExpression>(node, { | ||
| type: AST_NODE_TYPES.TaggedTemplateExpression, | ||
| quasi: this.convertChild(node.template), | ||
| tag: this.convertChild(node.tag), | ||
| typeArguments: | ||
| node.typeArguments && | ||
| this.convertTypeArgumentsToTypeParameterInstantiation( | ||
| node.typeArguments, | ||
| node, | ||
| ), | ||
| }); | ||
| const result = this.createNode<TSESTree.TaggedTemplateExpression>( | ||
|
Member
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. Revert? |
||
| node, | ||
| { | ||
| type: AST_NODE_TYPES.TaggedTemplateExpression, | ||
| quasi: this.convertChild(node.template), | ||
| tag: this.convertChild(node.tag), | ||
| typeArguments: | ||
| node.typeArguments && | ||
| this.convertTypeArgumentsToTypeParameterInstantiation( | ||
| node.typeArguments, | ||
| node, | ||
| ), | ||
| }, | ||
| ); | ||
| return result; | ||
| } | ||
|
|
||
| case SyntaxKind.TemplateHead: | ||
| case SyntaxKind.TemplateMiddle: | ||
| case SyntaxKind.TemplateTail: { | ||
| const tail = node.kind === SyntaxKind.TemplateTail; | ||
| const rawText = this.ast.text.slice( | ||
| node.getStart(this.ast) + 1, | ||
| node.end - (tail ? 1 : 2), | ||
| ); | ||
| const isTagged = | ||
| node.kind === SyntaxKind.TemplateHead | ||
| ? node.parent.parent.kind === SyntaxKind.TaggedTemplateExpression | ||
| : node.parent.parent.parent.kind === | ||
| SyntaxKind.TaggedTemplateExpression; | ||
|
|
||
| return this.createNode<TSESTree.TemplateElement>(node, { | ||
| type: AST_NODE_TYPES.TemplateElement, | ||
| tail, | ||
| value: { | ||
| cooked: node.text, | ||
| raw: this.ast.text.slice( | ||
| node.getStart(this.ast) + 1, | ||
| node.end - (tail ? 1 : 2), | ||
| ), | ||
| cooked: | ||
| isTagged && !this.#isValidEscape(rawText) ? null : node.text, | ||
| raw: rawText, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
This change affects three rules.
Would it be better to simply ignore it? How should I handle it?
Once this is decided, I will also add tests.