Skip to content
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

Remove errors for invalid escape sequences in tagged template literals #41030

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 4 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,10 @@
"category": "Error",
"code": 1432
},

"Octal escape sequences are not allowed in template strings.": {
"category": "Error",
"code": 1433
},
"The types of '{0}' are incompatible between these types.": {
"category": "Error",
"code": 2200
Expand Down
26 changes: 15 additions & 11 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1418,8 +1418,8 @@ namespace ts {
return currentToken = scanner.reScanTemplateToken(isTaggedTemplate);
}

function reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind {
return currentToken = scanner.reScanTemplateHeadOrNoSubstitutionTemplate();
function reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate: boolean): SyntaxKind {
return currentToken = scanner.reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate);
}

function reScanLessThanToken(): SyntaxKind {
Expand Down Expand Up @@ -2631,11 +2631,11 @@ namespace ts {
return createNodeArray(list, pos);
}

function parseTemplateExpression(isTaggedTemplate: boolean): TemplateExpression {
function parseTemplateExpression(isTaggedTemplate: boolean, containsInvalidEscape: boolean): TemplateExpression {
const pos = getNodePos();
return finishNode(
factory.createTemplateExpression(
parseTemplateHead(isTaggedTemplate),
parseTemplateHead(isTaggedTemplate, containsInvalidEscape),
parseTemplateSpans(isTaggedTemplate)
),
pos
Expand All @@ -2646,7 +2646,7 @@ namespace ts {
const pos = getNodePos();
return finishNode(
factory.createTemplateLiteralType(
parseTemplateHead(/*isTaggedTemplate*/ false),
parseTemplateHead(/*isTaggedTemplate*/ false, /* containsInvalidEscape */ false),
parseTemplateTypeSpans()
),
pos
Expand Down Expand Up @@ -2702,9 +2702,9 @@ namespace ts {
return <LiteralExpression>parseLiteralLikeNode(token());
}

function parseTemplateHead(isTaggedTemplate: boolean): TemplateHead {
if (isTaggedTemplate) {
reScanTemplateHeadOrNoSubstitutionTemplate();
function parseTemplateHead(isTaggedTemplate: boolean, containsInvalidEscape: boolean): TemplateHead {
if (isTaggedTemplate || containsInvalidEscape) {
reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate);
}
const fragment = parseLiteralLikeNode(token());
Debug.assert(fragment.kind === SyntaxKind.TemplateHead, "Template head has wrong token kind");
Expand Down Expand Up @@ -5239,8 +5239,8 @@ namespace ts {
tag,
typeArguments,
token() === SyntaxKind.NoSubstitutionTemplateLiteral ?
(reScanTemplateHeadOrNoSubstitutionTemplate(), parseLiteralNode() as NoSubstitutionTemplateLiteral) :
parseTemplateExpression(/*isTaggedTemplate*/ true)
(reScanTemplateHeadOrNoSubstitutionTemplate(/*isTaggedTemplate*/ true), parseLiteralNode() as NoSubstitutionTemplateLiteral) :
parseTemplateExpression(/*isTaggedTemplate*/ true, !!(scanner.getTokenFlags() & TokenFlags.ContainsInvalidEscape))
);
if (questionDotToken || tag.flags & NodeFlags.OptionalChain) {
(tagExpression as Mutable<Node>).flags |= NodeFlags.OptionalChain;
Expand Down Expand Up @@ -5372,7 +5372,11 @@ namespace ts {
case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.StringLiteral:
return parseLiteralNode();
case SyntaxKind.NoSubstitutionTemplateLiteral:
if (scanner.getTokenFlags() & TokenFlags.ContainsInvalidEscape) {
reScanTemplateHeadOrNoSubstitutionTemplate(/* isTaggedTemplate */ false);
}
return parseLiteralNode();
case SyntaxKind.ThisKeyword:
case SyntaxKind.SuperKeyword:
Expand Down Expand Up @@ -5408,7 +5412,7 @@ namespace ts {
}
break;
case SyntaxKind.TemplateHead:
return parseTemplateExpression(/* isTaggedTemplate */ false);
return parseTemplateExpression(/* isTaggedTemplate */ false, !!(scanner.getTokenFlags() & TokenFlags.ContainsInvalidEscape));
}

return parseIdentifier(Diagnostics.Expression_expected);
Expand Down
108 changes: 62 additions & 46 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ts {
reScanSlashToken(): SyntaxKind;
reScanAsteriskEqualsToken(): SyntaxKind;
reScanTemplateToken(isTaggedTemplate: boolean): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate?: boolean): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
Expand Down Expand Up @@ -1208,7 +1208,7 @@ namespace ts {
}
if (ch === CharacterCodes.backslash && !jsxAttributeString) {
result += text.substring(start, pos);
result += scanEscapeSequence();
result += scanEscapeSequence(/* isTaggedTemplate */ false, /* shouldEmitInvalidEscapeError */ true);
start = pos;
continue;
}
Expand All @@ -1227,7 +1227,7 @@ namespace ts {
* Sets the current 'tokenValue' and returns a NoSubstitutionTemplateLiteral or
* a literal component of a TemplateExpression.
*/
function scanTemplateAndSetTokenValue(isTaggedTemplate: boolean): SyntaxKind {
function scanTemplateAndSetTokenValue(isTaggedTemplate: boolean, shouldEmitInvalidEscapeError: boolean): SyntaxKind {
const startedWithBacktick = text.charCodeAt(pos) === CharacterCodes.backtick;

pos++;
Expand Down Expand Up @@ -1265,7 +1265,7 @@ namespace ts {
// Escape character
if (currChar === CharacterCodes.backslash) {
contents += text.substring(start, pos);
contents += scanEscapeSequence(isTaggedTemplate);
contents += scanEscapeSequence(isTaggedTemplate, shouldEmitInvalidEscapeError);
start = pos;
continue;
}
Expand Down Expand Up @@ -1294,7 +1294,7 @@ namespace ts {
return resultingToken;
}

function scanEscapeSequence(isTaggedTemplate?: boolean): string {
function scanEscapeSequence(isTaggedTemplate: boolean, shouldEmitInvalidEscapeError: boolean): string {
const start = pos;
pos++;
if (pos >= end) {
Expand All @@ -1306,10 +1306,15 @@ namespace ts {
switch (ch) {
case CharacterCodes._0:
// '\01'
if (isTaggedTemplate && pos < end && isDigit(text.charCodeAt(pos))) {
pos++;
if (pos < end && isDigit(text.charCodeAt(pos))) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
return text.substring(start, pos);
if (isTaggedTemplate) {
pos++;
return text.substring(start, pos);
}
if (shouldEmitInvalidEscapeError) {
error(Diagnostics.Octal_escape_sequences_are_not_allowed_in_template_strings);
}
}
return "\0";
case CharacterCodes.b:
Expand All @@ -1329,62 +1334,65 @@ namespace ts {
case CharacterCodes.doubleQuote:
return "\"";
case CharacterCodes.u:
if (isTaggedTemplate) {
// '\u' or '\u0' or '\u00' or '\u000'
for (let escapePos = pos; escapePos < pos + 4; escapePos++) {
if (escapePos < end && !isHexDigit(text.charCodeAt(escapePos)) && text.charCodeAt(escapePos) !== CharacterCodes.openBrace) {
// '\u' or '\u0' or '\u00' or '\u000'
for (let escapePos = pos; escapePos < pos + 4; escapePos++) {
if (escapePos < end && !isHexDigit(text.charCodeAt(escapePos)) && text.charCodeAt(escapePos) !== CharacterCodes.openBrace) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (isTaggedTemplate) {
pos = escapePos;
tokenFlags |= TokenFlags.ContainsInvalidEscape;
return text.substring(start, pos);
}
break;
}
}
// '\u{DDDDDDDD}'
if (pos < end && text.charCodeAt(pos) === CharacterCodes.openBrace) {
pos++;

// '\u{'
if (isTaggedTemplate && !isHexDigit(text.charCodeAt(pos))) {
if (!isHexDigit(text.charCodeAt(pos))) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
return text.substring(start, pos);
if (isTaggedTemplate) {
return text.substring(start, pos);
}
}

if (isTaggedTemplate) {
const savePos = pos;
const escapedValueString = scanMinimumNumberOfHexDigits(1, /*canHaveSeparators*/ false);
const escapedValue = escapedValueString ? parseInt(escapedValueString, 16) : -1;
const savePos = pos;
const escapedValueString = scanMinimumNumberOfHexDigits(1, /*canHaveSeparators*/ false);
const escapedValue = escapedValueString ? parseInt(escapedValueString, 16) : -1;

// '\u{Not Code Point' or '\u{CodePoint'
if (!isCodePoint(escapedValue) || text.charCodeAt(pos) !== CharacterCodes.closeBrace) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
// '\u{Not Code Point' or '\u{CodePoint'
if (!isCodePoint(escapedValue) || text.charCodeAt(pos) !== CharacterCodes.closeBrace) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (isTaggedTemplate) {
return text.substring(start, pos);
}
else {
pos = savePos;
}
}
pos = savePos;
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
return scanExtendedUnicodeEscape();
return scanExtendedUnicodeEscape(shouldEmitInvalidEscapeError);
}

tokenFlags |= TokenFlags.UnicodeEscape;
// '\uDDDD'
return scanHexadecimalEscape(/*numDigits*/ 4);
return scanHexadecimalEscape(/*numDigits*/ 4, shouldEmitInvalidEscapeError);

case CharacterCodes.x:
if (isTaggedTemplate) {
if (!isHexDigit(text.charCodeAt(pos))) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (!isHexDigit(text.charCodeAt(pos))) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (isTaggedTemplate) {
return text.substring(start, pos);
}
else if (!isHexDigit(text.charCodeAt(pos + 1))) {
}
else if (!isHexDigit(text.charCodeAt(pos + 1))) {
tokenFlags |= TokenFlags.ContainsInvalidEscape;
if (isTaggedTemplate) {
pos++;
tokenFlags |= TokenFlags.ContainsInvalidEscape;
return text.substring(start, pos);
}
}
// '\xDD'
return scanHexadecimalEscape(/*numDigits*/ 2);
return scanHexadecimalEscape(/*numDigits*/ 2, shouldEmitInvalidEscapeError);

// when encountering a LineContinuation (i.e. a backslash and a line terminator sequence),
// the line terminator is interpreted to be "the empty code unit sequence".
Expand All @@ -1402,30 +1410,36 @@ namespace ts {
}
}

function scanHexadecimalEscape(numDigits: number): string {
function scanHexadecimalEscape(numDigits: number, shouldEmitInvalidEscapeError: boolean): string {
const escapedValue = scanExactNumberOfHexDigits(numDigits, /*canHaveSeparators*/ false);

if (escapedValue >= 0) {
return String.fromCharCode(escapedValue);
}
else {
error(Diagnostics.Hexadecimal_digit_expected);
if (shouldEmitInvalidEscapeError) {
error(Diagnostics.Hexadecimal_digit_expected);
}
return "";
}
}

function scanExtendedUnicodeEscape(): string {
function scanExtendedUnicodeEscape(shouldEmitInvalidEscapeError: boolean): string {
const escapedValueString = scanMinimumNumberOfHexDigits(1, /*canHaveSeparators*/ false);
const escapedValue = escapedValueString ? parseInt(escapedValueString, 16) : -1;
let isInvalidExtendedEscape = false;

// Validate the value of the digit
if (escapedValue < 0) {
error(Diagnostics.Hexadecimal_digit_expected);
if (shouldEmitInvalidEscapeError) {
error(Diagnostics.Hexadecimal_digit_expected);
}
isInvalidExtendedEscape = true;
}
else if (escapedValue > 0x10FFFF) {
error(Diagnostics.An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive);
if (shouldEmitInvalidEscapeError) {
error(Diagnostics.An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive);
}
isInvalidExtendedEscape = true;
}

Expand All @@ -1438,7 +1452,9 @@ namespace ts {
pos++;
}
else {
error(Diagnostics.Unterminated_Unicode_escape_sequence);
if (shouldEmitInvalidEscapeError) {
error(Diagnostics.Unterminated_Unicode_escape_sequence);
}
isInvalidExtendedEscape = true;
}

Expand Down Expand Up @@ -1488,7 +1504,7 @@ namespace ts {
if (ch >= 0 && isIdentifierPart(ch, languageVersion)) {
pos += 3;
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
result += scanExtendedUnicodeEscape();
result += scanExtendedUnicodeEscape(/* shouldEmitInvalidEscapeError */ true);
start = pos;
continue;
}
Expand Down Expand Up @@ -1673,7 +1689,7 @@ namespace ts {
tokenValue = scanString();
return token = SyntaxKind.StringLiteral;
case CharacterCodes.backtick:
return token = scanTemplateAndSetTokenValue(/* isTaggedTemplate */ false);
return token = scanTemplateAndSetTokenValue(/* isTaggedTemplate */ false, /* shouldEmitInvalidEscapeError */ false);
case CharacterCodes.percent:
if (text.charCodeAt(pos + 1) === CharacterCodes.equals) {
return pos += 2, token = SyntaxKind.PercentEqualsToken;
Expand Down Expand Up @@ -2008,7 +2024,7 @@ namespace ts {
if (extendedCookedChar >= 0 && isIdentifierStart(extendedCookedChar, languageVersion)) {
pos += 3;
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
tokenValue = scanExtendedUnicodeEscape() + scanIdentifierParts();
tokenValue = scanExtendedUnicodeEscape(/* shouldEmitInvalidEscapeError */ true) + scanIdentifierParts();
return token = getIdentifierToken();
}

Expand Down Expand Up @@ -2216,12 +2232,12 @@ namespace ts {
function reScanTemplateToken(isTaggedTemplate: boolean): SyntaxKind {
Debug.assert(token === SyntaxKind.CloseBraceToken, "'reScanTemplateToken' should only be called on a '}'");
pos = tokenPos;
return token = scanTemplateAndSetTokenValue(isTaggedTemplate);
return token = scanTemplateAndSetTokenValue(isTaggedTemplate, /* shouldEmitInvalidEscapeError */ true);
}

function reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind {
function reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate?: boolean): SyntaxKind {
pos = tokenPos;
return token = scanTemplateAndSetTokenValue(/* isTaggedTemplate */ true);
return token = scanTemplateAndSetTokenValue(isTaggedTemplate || false, /* shouldEmitInvalidEscapeError */ true);
}

function reScanJsxToken(allowMultilineJsxText = true): JsxTokenSyntaxKind {
Expand Down Expand Up @@ -2429,7 +2445,7 @@ namespace ts {
if (extendedCookedChar >= 0 && isIdentifierStart(extendedCookedChar, languageVersion)) {
pos += 3;
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
tokenValue = scanExtendedUnicodeEscape() + scanIdentifierParts();
tokenValue = scanExtendedUnicodeEscape(/* shouldEmitInvalidEscapeError */ true) + scanIdentifierParts();
return token = getIdentifierToken();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,7 @@ declare namespace ts {
reScanSlashToken(): SyntaxKind;
reScanAsteriskEqualsToken(): SyntaxKind;
reScanTemplateToken(isTaggedTemplate: boolean): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate?: boolean): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,7 @@ declare namespace ts {
reScanSlashToken(): SyntaxKind;
reScanAsteriskEqualsToken(): SyntaxKind;
reScanTemplateToken(isTaggedTemplate: boolean): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind;
reScanTemplateHeadOrNoSubstitutionTemplate(isTaggedTemplate?: boolean): SyntaxKind;
scanJsxIdentifier(): SyntaxKind;
scanJsxAttributeValue(): SyntaxKind;
reScanJsxAttributeValue(): SyntaxKind;
Expand Down
Loading