Skip to content

Commit 5523d9a

Browse files
Maxwell HeiberGitHub Enterprise
authored andcommitted
can parse private names (#1)
do a really rough, quick-and-dirty unblock-people job of letting #foo be parse-able. steps to test: npm run lint && npm test See https://bbgithub.dev.bloomberg.com/javascript-guild/typescript-private-fields/ quick start guide for seeing the AST CI testing coming soon
1 parent 43f2e36 commit 5523d9a

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ namespace ts {
13351335
if (token() !== SyntaxKind.Identifier) {
13361336
node.originalKeywordKind = token();
13371337
}
1338+
node.isPrivateName = !!(scanner.getTokenFlags() & TokenFlags.PrivateName);
13381339
node.escapedText = escapeLeadingUnderscores(internIdentifier(scanner.getTokenValue()));
13391340
nextToken();
13401341
return finishNode(node);

src/compiler/scanner.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,21 @@ namespace ts {
17231723
error(Diagnostics.Invalid_character);
17241724
pos++;
17251725
return token = SyntaxKind.Unknown;
1726+
case CharacterCodes.hash:
1727+
pos++;
1728+
if (isIdentifierStart(ch = text.charCodeAt(pos), languageVersion)) {
1729+
tokenFlags |= TokenFlags.PrivateName;
1730+
pos++;
1731+
while (pos < end && isIdentifierPart(ch = text.charCodeAt(pos), languageVersion)) pos++;
1732+
tokenValue = text.substring(tokenPos, pos);
1733+
if (ch === CharacterCodes.backslash) {
1734+
tokenValue += scanIdentifierParts();
1735+
}
1736+
return token = SyntaxKind.Identifier;
1737+
}
1738+
error(Diagnostics.Invalid_character);
1739+
pos++;
1740+
return token = SyntaxKind.Unknown;
17261741
default:
17271742
if (isIdentifierStart(ch, languageVersion)) {
17281743
pos++;

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ namespace ts {
680680
*/
681681
escapedText: __String;
682682
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
683+
isPrivateName: boolean; // is private name like `this.#foo`
683684
/*@internal*/ autoGenerateFlags?: GeneratedIdentifierFlags; // Specifies whether to auto-generate the text for an identifier.
684685
/*@internal*/ autoGenerateId?: number; // Ensures unique generated identifiers get unique names, but clones get the same name.
685686
isInJSDocNamespace?: boolean; // if the node is a member in a JSDoc namespace
@@ -1555,6 +1556,7 @@ namespace ts {
15551556
BinarySpecifier = 1 << 7, // e.g. `0b0110010000000000`
15561557
OctalSpecifier = 1 << 8, // e.g. `0o777`
15571558
ContainsSeparator = 1 << 9, // e.g. `0b1100_0101`
1559+
PrivateName = 1 << 10, // e.g. `#foo`
15581560
BinaryOrOctalSpecifier = BinarySpecifier | OctalSpecifier,
15591561
NumericLiteralFlags = Scientific | Octal | HexSpecifier | BinarySpecifier | OctalSpecifier | ContainsSeparator
15601562
}

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ namespace ts {
347347
public kind: SyntaxKind.Identifier;
348348
public escapedText: __String;
349349
public symbol: Symbol;
350+
public isPrivateName: boolean;
350351
public autoGenerateFlags: GeneratedIdentifierFlags;
351352
_primaryExpressionBrand: any;
352353
_memberExpressionBrand: any;

0 commit comments

Comments
 (0)