mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Add error message for keywords with escapes in them (#32718)
* Add error message for keywords with escapes in them * Move check into parser during advance to next token to utilize context for contextual keywords * git add . * Add tests for extended escapes * Better error courtesy of @DanielRossenwaser * Add test of browser-inconsistent case and alter condition to match spec * Merge adjacent conditions * Use seperate functions for checking keywords vs not * Use flags to track unicode escape presence * Adjust error text
This commit is contained in:
1 parent
51411c1f8b
commit
4ab85bbf35
14 files changed
+341
-12
No files matched your search
@@ -851,6 +851,10 @@
|
||||
"category": "Error",
|
||||
"code": 1259
|
||||
},
|
||||
"Keywords cannot contain escape characters.": {
|
||||
"category": "Error",
|
||||
"code": 1260
|
||||
},
|
||||
"'with' statements are not allowed in an async function block.": {
|
||||
"category": "Error",
|
||||
"code": 1300
|
||||
|
||||
+11
-2
@@ -1086,10 +1086,19 @@ namespace ts {
|
||||
return currentToken;
|
||||
}
|
||||
|
||||
function nextToken(): SyntaxKind {
|
||||
function nextTokenWithoutCheck() {
|
||||
return currentToken = scanner.scan();
|
||||
}
|
||||
|
||||
function nextToken(): SyntaxKind {
|
||||
// if the keyword had an escape
|
||||
if (isKeyword(currentToken) && (scanner.hasUnicodeEscape() || scanner.hasExtendedUnicodeEscape())) {
|
||||
// issue a parse error for the escape
|
||||
parseErrorAt(scanner.getTokenPos(), scanner.getTextPos(), Diagnostics.Keywords_cannot_contain_escape_characters);
|
||||
}
|
||||
return nextTokenWithoutCheck();
|
||||
}
|
||||
|
||||
function nextTokenJSDoc(): JSDocSyntaxKind {
|
||||
return currentToken = scanner.scanJsDocToken();
|
||||
}
|
||||
@@ -1380,7 +1389,7 @@ namespace ts {
|
||||
node.originalKeywordKind = token();
|
||||
}
|
||||
node.escapedText = escapeLeadingUnderscores(internIdentifier(scanner.getTokenValue()));
|
||||
nextToken();
|
||||
nextTokenWithoutCheck();
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace ts {
|
||||
getTokenPos(): number;
|
||||
getTokenText(): string;
|
||||
getTokenValue(): string;
|
||||
hasUnicodeEscape(): boolean;
|
||||
hasExtendedUnicodeEscape(): boolean;
|
||||
hasPrecedingLineBreak(): boolean;
|
||||
isIdentifier(): boolean;
|
||||
@@ -884,6 +885,7 @@ namespace ts {
|
||||
getTokenPos: () => tokenPos,
|
||||
getTokenText: () => text.substring(tokenPos, pos),
|
||||
getTokenValue: () => tokenValue,
|
||||
hasUnicodeEscape: () => (tokenFlags & TokenFlags.UnicodeEscape) !== 0,
|
||||
hasExtendedUnicodeEscape: () => (tokenFlags & TokenFlags.ExtendedUnicodeEscape) !== 0,
|
||||
hasPrecedingLineBreak: () => (tokenFlags & TokenFlags.PrecedingLineBreak) !== 0,
|
||||
isIdentifier: () => token === SyntaxKind.Identifier || token > SyntaxKind.LastReservedWord,
|
||||
@@ -1245,6 +1247,7 @@ namespace ts {
|
||||
return scanExtendedUnicodeEscape();
|
||||
}
|
||||
|
||||
tokenFlags |= TokenFlags.UnicodeEscape;
|
||||
// '\uDDDD'
|
||||
return scanHexadecimalEscape(/*numDigits*/ 4);
|
||||
|
||||
@@ -1376,6 +1379,7 @@ namespace ts {
|
||||
if (!(ch >= 0 && isIdentifierPart(ch, languageVersion))) {
|
||||
break;
|
||||
}
|
||||
tokenFlags |= TokenFlags.UnicodeEscape;
|
||||
result += text.substring(start, pos);
|
||||
result += utf16EncodeAsString(ch);
|
||||
// Valid Unicode escape is always six characters
|
||||
@@ -1868,6 +1872,7 @@ namespace ts {
|
||||
const cookedChar = peekUnicodeEscape();
|
||||
if (cookedChar >= 0 && isIdentifierStart(cookedChar, languageVersion)) {
|
||||
pos += 6;
|
||||
tokenFlags |= TokenFlags.UnicodeEscape;
|
||||
tokenValue = String.fromCharCode(cookedChar) + scanIdentifierParts();
|
||||
return token = getIdentifierToken();
|
||||
}
|
||||
@@ -2156,6 +2161,7 @@ namespace ts {
|
||||
const cookedChar = peekUnicodeEscape();
|
||||
if (cookedChar >= 0 && isIdentifierStart(cookedChar, languageVersion)) {
|
||||
pos += 6;
|
||||
tokenFlags |= TokenFlags.UnicodeEscape;
|
||||
tokenValue = String.fromCharCode(cookedChar) + scanIdentifierParts();
|
||||
return token = getIdentifierToken();
|
||||
}
|
||||
|
||||
@@ -1679,6 +1679,8 @@ namespace ts {
|
||||
/* @internal */
|
||||
ContainsSeparator = 1 << 9, // e.g. `0b1100_0101`
|
||||
/* @internal */
|
||||
UnicodeEscape = 1 << 10,
|
||||
/* @internal */
|
||||
BinaryOrOctalSpecifier = BinarySpecifier | OctalSpecifier,
|
||||
/* @internal */
|
||||
NumericLiteralFlags = Scientific | Octal | HexSpecifier | BinaryOrOctalSpecifier | ContainsSeparator
|
||||
|
||||
@@ -2682,6 +2682,10 @@ namespace ts {
|
||||
return isKeyword(token) && !isContextualKeyword(token);
|
||||
}
|
||||
|
||||
export function isFutureReservedKeyword(token: SyntaxKind): boolean {
|
||||
return SyntaxKind.FirstFutureReservedWord <= token && token <= SyntaxKind.LastFutureReservedWord;
|
||||
}
|
||||
|
||||
export function isStringANonContextualKeyword(name: string) {
|
||||
const token = stringToToken(name);
|
||||
return token !== undefined && isNonContextualKeyword(token);
|
||||
|
||||
Reference in new issue
Block a user