-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
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 |
---|---|---|
|
@@ -8,9 +8,6 @@ | |
{ | ||
"path": "../typescript-estree/tsconfig.build.json" | ||
}, | ||
{ | ||
"path": "../types/tsconfig.build.json" | ||
}, | ||
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. Because of this message by nx,
There are a lot of changes like this due to sync, but even after reading the documentation, I'm not sure if this is correct. |
||
{ | ||
"path": "../scope-manager/tsconfig.build.json" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,6 @@ | |
{ | ||
"path": "../typescript-estree" | ||
}, | ||
{ | ||
"path": "../types" | ||
}, | ||
{ | ||
"path": "../scope-manager" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,6 @@ | |
{ | ||
"path": "../tsconfig-utils" | ||
}, | ||
{ | ||
"path": "../types" | ||
}, | ||
{ | ||
"path": "./tsconfig.build.json" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,6 @@ | |
{ | ||
"path": "../visitor-keys" | ||
}, | ||
{ | ||
"path": "../types" | ||
}, | ||
{ | ||
"path": "../typescript-estree" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,6 @@ | |
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "../types" | ||
}, | ||
{ | ||
"path": "../utils" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,7 @@ function isEntityNameExpression( | |
} | ||
|
||
export class Converter { | ||
#isInTaggedTemplate = false; | ||
private allowPattern = false; | ||
private readonly ast: ts.SourceFile; | ||
private readonly esTreeNodeToTSNodeMap = new WeakMap(); | ||
|
@@ -401,6 +402,78 @@ export class Converter { | |
} | ||
} | ||
|
||
#isValidEscape(text: string): boolean { | ||
function isHex(hex: string): boolean { | ||
return /^[0-9a-fA-F]+$/.test(hex); | ||
} | ||
|
||
const validShort = [ | ||
'f', | ||
'n', | ||
'r', | ||
't', | ||
'v', | ||
'b', | ||
'\\', | ||
'"', | ||
"'", | ||
'`', | ||
'0', | ||
'$', | ||
]; | ||
|
||
for (let index = 0; index < text.length; index++) { | ||
const char = text[index]; | ||
if (char !== '\\') { | ||
continue; | ||
} | ||
|
||
const nextChar = text[index + 1]; | ||
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. Don't care about if nextChar doesn't exist because of typescript error & parsing error. |
||
|
||
if (validShort.includes(nextChar)) { | ||
index += 1; | ||
continue; | ||
} | ||
|
||
// unicode | ||
if (nextChar === 'u') { | ||
if (text[index + 2] === '{') { | ||
const closingBraceIndex = text.indexOf('}', index + 3); | ||
if (closingBraceIndex === -1) { | ||
return false; | ||
} | ||
|
||
const hex = text.slice(index + 3, closingBraceIndex); | ||
if (!isHex(hex) || hex.length === 0 || hex.length > 6) { | ||
return false; | ||
} | ||
index += closingBraceIndex; | ||
continue; | ||
} else { | ||
const hex = text.slice(index + 2, index + 6); | ||
if (!isHex(hex) || hex.length !== 4) { | ||
return false; | ||
} | ||
index += 5; | ||
continue; | ||
} | ||
} | ||
|
||
// hex | ||
if (nextChar === 'x') { | ||
const hex = text.slice(index + 2, index + 4); | ||
if (!isHex(hex) || hex.length !== 2) { | ||
return false; | ||
} | ||
index += 3; | ||
continue; | ||
} | ||
|
||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
#throwError(node: number | ts.Node, message: string): asserts node is never { | ||
let start; | ||
let end; | ||
|
@@ -1889,7 +1962,10 @@ export class Converter { | |
type: AST_NODE_TYPES.TemplateElement, | ||
tail: true, | ||
value: { | ||
cooked: node.text, | ||
cooked: | ||
this.#isInTaggedTemplate && !this.#isValidEscape(node.text) | ||
? null | ||
: node.text, | ||
raw: this.ast.text.slice( | ||
node.getStart(this.ast) + 1, | ||
node.end - 1, | ||
|
@@ -1924,19 +2000,24 @@ 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, | ||
), | ||
}); | ||
this.#isInTaggedTemplate = true; | ||
const result = 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, | ||
), | ||
}, | ||
); | ||
this.#isInTaggedTemplate = false; | ||
return result; | ||
} | ||
|
||
case SyntaxKind.TemplateHead: | ||
case SyntaxKind.TemplateMiddle: | ||
case SyntaxKind.TemplateTail: { | ||
|
@@ -1945,7 +2026,10 @@ export class Converter { | |
type: AST_NODE_TYPES.TemplateElement, | ||
tail, | ||
value: { | ||
cooked: node.text, | ||
cooked: | ||
this.#isInTaggedTemplate && !this.#isValidEscape(node.text) | ||
? null | ||
: node.text, | ||
raw: this.ast.text.slice( | ||
node.getStart(this.ast) + 1, | ||
node.end - (tail ? 1 : 2), | ||
|
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.