Skip to content

Parse private fields–work in progress #3

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

Closed
wants to merge 2 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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4470,5 +4470,9 @@
"Add all missing enum members": {
"category": "Message",
"code": 95064
},
"Private names are not allowed outside of class bodies": {
"category": "Error",
"code": 95065
}
}
11 changes: 9 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,15 +1320,22 @@ namespace ts {
// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues
// with magic property names like '__proto__'. The 'identifiers' object is used to share a single string instance for
// each identifier in order to reduce memory consumption.
function createIdentifier(isIdentifier: boolean, diagnosticMessage?: DiagnosticMessage): Identifier {
function createIdentifier(isIdentifier: boolean, suggestedDiagnosticMessage?: DiagnosticMessage): Identifier {
identifierCount++;
if (isIdentifier) {
let diagnosticMessage = suggestedDiagnosticMessage;
const isPrivateName = !!(scanner.getTokenFlags() & TokenFlags.PrivateName);
const privateNameIsAllowed = !!(parsingContext & (1 << ParsingContext.ClassMembers));
if (isPrivateName && !privateNameIsAllowed) {
diagnosticMessage = Diagnostics.Private_names_are_not_allowed_outside_of_class_bodies;
}
else if (isIdentifier && (!isPrivateName || privateNameIsAllowed)) {
const node = <Identifier>createNode(SyntaxKind.Identifier);

// Store original token kind if it is not just an Identifier so we can report appropriate error later in type checker
if (token() !== SyntaxKind.Identifier) {
node.originalKeywordKind = token();
}
node.isPrivateName = isPrivateName;
node.escapedText = escapeLeadingUnderscores(internIdentifier(scanner.getTokenValue()));
nextToken();
return finishNode(node);
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,21 @@ namespace ts {
error(Diagnostics.Invalid_character);
pos++;
return token = SyntaxKind.Unknown;
case CharacterCodes.hash:
pos++;
if (isIdentifierStart(ch = text.charCodeAt(pos), languageVersion)) {
tokenFlags |= TokenFlags.PrivateName;
pos++;
while (pos < end && isIdentifierPart(ch = text.charCodeAt(pos), languageVersion)) pos++;
tokenValue = text.substring(tokenPos, pos);
if (ch === CharacterCodes.backslash) {
tokenValue += scanIdentifierParts();
}
return token = SyntaxKind.Identifier;
}
error(Diagnostics.Invalid_character);
// no `pos++` because already advanced at beginning of this `case` statement
return token = SyntaxKind.Unknown;
default:
if (isIdentifierStart(ch, languageVersion)) {
pos++;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ namespace ts {
*/
escapedText: __String;
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
isPrivateName: boolean; // is private name like `this.#foo`
/*@internal*/ autoGenerateFlags?: GeneratedIdentifierFlags; // Specifies whether to auto-generate the text for an identifier.
/*@internal*/ autoGenerateId?: number; // Ensures unique generated identifiers get unique names, but clones get the same name.
isInJSDocNamespace?: boolean; // if the node is a member in a JSDoc namespace
Expand Down Expand Up @@ -1574,6 +1575,7 @@ namespace ts {
BinarySpecifier = 1 << 7, // e.g. `0b0110010000000000`
OctalSpecifier = 1 << 8, // e.g. `0o777`
ContainsSeparator = 1 << 9, // e.g. `0b1100_0101`
PrivateName = 1 << 10, // e.g. `#foo`
BinaryOrOctalSpecifier = BinarySpecifier | OctalSpecifier,
NumericLiteralFlags = Scientific | Octal | HexSpecifier | BinarySpecifier | OctalSpecifier | ContainsSeparator
}
Expand Down
1 change: 1 addition & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ namespace ts {
public kind: SyntaxKind.Identifier;
public escapedText: __String;
public symbol: Symbol;
public isPrivateName: boolean;
public autoGenerateFlags: GeneratedIdentifierFlags;
_primaryExpressionBrand: any;
_memberExpressionBrand: any;
Expand Down