Skip to content

Commit 6fccba5

Browse files
Check for leading escape sequences in PrivateIdentifiers.
1 parent fec0977 commit 6fccba5

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/compiler/scanner.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,17 +2052,38 @@ namespace ts {
20522052
return token = SyntaxKind.Unknown;
20532053
}
20542054

2055-
if (isIdentifierStart(codePointAt(text, pos + 1), languageVersion)) {
2055+
const charAfterHash = codePointAt(text, pos + 1);
2056+
if (charAfterHash === CharacterCodes.backslash) {
2057+
pos++
2058+
const extendedCookedChar = peekExtendedUnicodeEscape();
2059+
if (extendedCookedChar >= 0 && isIdentifierStart(extendedCookedChar, languageVersion)) {
2060+
pos += 3;
2061+
tokenFlags |= TokenFlags.ExtendedUnicodeEscape;
2062+
tokenValue = "#" + scanExtendedUnicodeEscape() + scanIdentifierParts();
2063+
return token = SyntaxKind.PrivateIdentifier;
2064+
}
2065+
2066+
const cookedChar = peekUnicodeEscape();
2067+
if (cookedChar >= 0 && isIdentifierStart(cookedChar, languageVersion)) {
2068+
pos += 6;
2069+
tokenFlags |= TokenFlags.UnicodeEscape;
2070+
tokenValue = "#" + String.fromCharCode(cookedChar) + scanIdentifierParts();
2071+
return token = SyntaxKind.PrivateIdentifier;
2072+
}
2073+
pos--;
2074+
}
2075+
2076+
if (isIdentifierStart(charAfterHash, languageVersion)) {
20562077
pos++;
20572078
// We're relying on scanIdentifier's behavior and adjusting the token kind after the fact.
20582079
// Notably absent from this block is the fact that calling a function named "scanIdentifier",
20592080
// but identifiers don't include '#', and that function doesn't deal with it at all.
20602081
// This works because 'scanIdentifier' tries to reuse source characters and builds up substrings;
20612082
// however, it starts at the 'tokenPos' which includes the '#', and will "accidentally" prepend the '#' for us.
2062-
scanIdentifier(codePointAt(text, pos), languageVersion);
2083+
scanIdentifier(charAfterHash, languageVersion);
20632084
}
20642085
else {
2065-
tokenValue = String.fromCharCode(codePointAt(text, pos));
2086+
tokenValue = "#";
20662087
error(Diagnostics.Invalid_character, pos++, charSize(ch));
20672088
}
20682089
return token = SyntaxKind.PrivateIdentifier;

0 commit comments

Comments
 (0)