Skip to content

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/ast-spec/src/special/TemplateElement/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface TemplateElement extends BaseNode {
type: AST_NODE_TYPES.TemplateElement;
tail: boolean;
value: {
cooked: string;
cooked: string | null;
raw: string;
};
}
1 change: 1 addition & 0 deletions packages/ast-spec/tests/fixtures-with-differences-ast.shot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 60 additions & 15 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function isEntityNameExpression(
}

export class Converter {
#isInTaggedTemplate = false;
private allowPattern = false;
private readonly ast: ts.SourceFile;
private readonly esTreeNodeToTSNodeMap = new WeakMap();
Expand Down Expand Up @@ -401,6 +402,38 @@ export class Converter {
}
}

#isValidEscape(arg: string): boolean {
const unicode = /\\u([0-9a-fA-F]{4})/g;
const unicodeBracket = /\\u\{([0-9a-fA-F]+)\}/g; // supports ES6+
const hex = /\\x([0-9a-fA-F]{2})/g;
const validShort = /\\[nrtbfv0\\'"]/g;

const allEscapes =
/\\(u\{[^}]*\}|u[0-9a-fA-F]{0,4}|x[0-9a-fA-F]{0,2}|[^ux])/g;

let match: RegExpExecArray | null;
while ((match = allEscapes.exec(arg)) != null) {
const escape = match[0];

if (
unicode.test(escape) ||
(unicodeBracket.test(escape) &&
(() => {
const cp = parseInt(escape.match(unicodeBracket)![1], 16);
return cp <= 0x10ffff;
})()) ||
hex.test(escape) ||
validShort.test(escape)
) {
continue;
}

return false;
}

return true;
}

#throwError(node: number | ts.Node, message: string): asserts node is never {
let start;
let end;
Expand Down Expand Up @@ -1889,7 +1922,10 @@ export class Converter {
type: AST_NODE_TYPES.TemplateElement,
tail: true,
value: {
cooked: node.text,
cooked:
this.#isValidEscape(node.text) && this.#isInTaggedTemplate
? node.text
: null,
raw: this.ast.text.slice(
node.getStart(this.ast) + 1,
node.end - 1,
Expand Down Expand Up @@ -1917,19 +1953,25 @@ export class Converter {
return result;
}

case SyntaxKind.TaggedTemplateExpression:
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,
),
});

case SyntaxKind.TaggedTemplateExpression: {
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: {
Expand All @@ -1938,7 +1980,10 @@ export class Converter {
type: AST_NODE_TYPES.TemplateElement,
tail,
value: {
cooked: node.text,
cooked:
this.#isValidEscape(node.text) && this.#isInTaggedTemplate
? node.text
: null,
raw: this.ast.text.slice(
node.getStart(this.ast) + 1,
node.end - (tail ? 1 : 2),
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy