Skip to content

Commit 3855369

Browse files
Fixed hasInvalidEscape implementation (#55373)
Co-authored-by: Damien Engels <[email protected]>
1 parent 3c6c557 commit 3855369

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

src/compiler/utilities.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ import {
509509
TaggedTemplateExpression,
510510
TemplateLiteral,
511511
TemplateLiteralLikeNode,
512+
TemplateLiteralToken,
512513
TemplateLiteralTypeSpan,
513514
TemplateSpan,
514515
TextRange,
@@ -5801,11 +5802,15 @@ function escapeTemplateSubstitution(str: string): string {
58015802
return str.replace(templateSubstitutionRegExp, "\\${");
58025803
}
58035804

5805+
function containsInvalidEscapeFlag(node: TemplateLiteralToken): boolean {
5806+
return !!((node.templateFlags || 0) & TokenFlags.ContainsInvalidEscape);
5807+
}
5808+
58045809
/** @internal */
58055810
export function hasInvalidEscape(template: TemplateLiteral): boolean {
58065811
return template && !!(isNoSubstitutionTemplateLiteral(template)
5807-
? template.templateFlags
5808-
: (template.head.templateFlags || some(template.templateSpans, span => !!span.literal.templateFlags)));
5812+
? containsInvalidEscapeFlag(template)
5813+
: (containsInvalidEscapeFlag(template.head) || some(template.templateSpans, span => containsInvalidEscapeFlag(span.literal))));
58095814
}
58105815

58115816
// This consists of the first 19 unprintable ASCII characters, canonical escapes, lineSeparator,

tests/baselines/reference/invalidTaggedTemplateEscapeSequences(target=es2015).js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ const a3 = tag(__makeTemplateObject(["", void 0], ["", "\\u"]), 100); // \\u
4646
const a4 = tag(__makeTemplateObject(["", void 0], ["", "\\u0"]), 100); // \\u0
4747
const a5 = tag(__makeTemplateObject(["", void 0], ["", "\\u00"]), 100); // \\u00
4848
const a6 = tag(__makeTemplateObject(["", void 0], ["", "\\u000"]), 100); // \\u000
49-
const a7 = tag(__makeTemplateObject(["", "\0"], ["", "\\u0000"]), 100); // \u0000
49+
const a7 = tag `${100}\u0000`; // \u0000
5050
const a8 = tag(__makeTemplateObject(["", void 0], ["", "\\u{"]), 100); // \\u{
51-
const a9 = tag(__makeTemplateObject(["", "\uDBFF\uDFFF"], ["", "\\u{10FFFF}"]), 100); // \\u{10FFFF
51+
const a9 = tag `${100}\u{10FFFF}`; // \\u{10FFFF
5252
const a10 = tag(__makeTemplateObject(["", void 0], ["", "\\u{1f622"]), 100); // \\u{1f622
53-
const a11 = tag(__makeTemplateObject(["", "\uD83D\uDE22"], ["", "\\u{1f622}"]), 100); // \u{1f622}
53+
const a11 = tag `${100}\u{1f622}`; // \u{1f622}
5454
const a12 = tag(__makeTemplateObject(["", void 0], ["", "\\x"]), 100); // \\x
5555
const a13 = tag(__makeTemplateObject(["", void 0], ["", "\\x0"]), 100); // \\x0
56-
const a14 = tag(__makeTemplateObject(["", "\0"], ["", "\\x00"]), 100); // \x00
56+
const a14 = tag `${100}\x00`; // \x00

tests/baselines/reference/taggedTemplateStringsHexadecimalEscapesES6.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ function f(...args: any[]) {
77
f `\x0D${ "Interrupted CRLF" }\x0A`;
88

99
//// [taggedTemplateStringsHexadecimalEscapesES6.js]
10-
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
11-
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
12-
return cooked;
13-
};
1410
function f(...args) {
1511
}
16-
f(__makeTemplateObject(["\r", "\n"], ["\\x0D", "\\x0A"]), "Interrupted CRLF");
12+
f `\x0D${"Interrupted CRLF"}\x0A`;

tests/baselines/reference/taggedTemplateStringsWithUnicodeEscapesES6.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ function f(...args: any[]) {
77
f `'\u{1f4a9}'${ " should be converted to " }'\uD83D\uDCA9'`;
88

99
//// [taggedTemplateStringsWithUnicodeEscapesES6.js]
10-
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
11-
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
12-
return cooked;
13-
};
1410
function f(...args) {
1511
}
16-
f(__makeTemplateObject(["'\uD83D\uDCA9'", "'\uD83D\uDCA9'"], ["'\\u{1f4a9}'", "'\\uD83D\\uDCA9'"]), " should be converted to ");
12+
f `'\u{1f4a9}'${" should be converted to "}'\uD83D\uDCA9'`;

0 commit comments

Comments
 (0)