Skip to content

Commit ae1bd0c

Browse files
Max Heibermheiber
authored andcommitted
Private name parse errors
1 parent c2d1821 commit ae1bd0c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,5 +4470,9 @@
44704470
"Add all missing enum members": {
44714471
"category": "Message",
44724472
"code": 95064
4473+
},
4474+
"Private names are not allowed outside of class bodies": {
4475+
"category": "Error",
4476+
"code": 95065
44734477
}
44744478
}

src/compiler/parser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,16 +1320,22 @@ namespace ts {
13201320
// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues
13211321
// with magic property names like '__proto__'. The 'identifiers' object is used to share a single string instance for
13221322
// each identifier in order to reduce memory consumption.
1323-
function createIdentifier(isIdentifier: boolean, diagnosticMessage?: DiagnosticMessage): Identifier {
1323+
function createIdentifier(isIdentifier: boolean, suggestedDiagnosticMessage?: DiagnosticMessage): Identifier {
13241324
identifierCount++;
1325-
if (isIdentifier) {
1325+
let diagnosticMessage = suggestedDiagnosticMessage;
1326+
const isPrivateName = !!(scanner.getTokenFlags() & TokenFlags.PrivateName);
1327+
const privateNameIsAllowed = !!(parsingContext & (1 << ParsingContext.ClassMembers));
1328+
if (isPrivateName && !privateNameIsAllowed) {
1329+
diagnosticMessage = Diagnostics.Private_names_are_not_allowed_outside_of_class_bodies;
1330+
}
1331+
else if (isIdentifier && (!isPrivateName || privateNameIsAllowed)) {
13261332
const node = <Identifier>createNode(SyntaxKind.Identifier);
13271333

13281334
// Store original token kind if it is not just an Identifier so we can report appropriate error later in type checker
13291335
if (token() !== SyntaxKind.Identifier) {
13301336
node.originalKeywordKind = token();
13311337
}
1332-
node.isPrivateName = !!(scanner.getTokenFlags() & TokenFlags.PrivateName);
1338+
node.isPrivateName = isPrivateName;
13331339
node.escapedText = escapeLeadingUnderscores(internIdentifier(scanner.getTokenValue()));
13341340
nextToken();
13351341
return finishNode(node);

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ namespace ts {
17361736
return token = SyntaxKind.Identifier;
17371737
}
17381738
error(Diagnostics.Invalid_character);
1739-
pos++;
1739+
// no `pos++` because already advanced at beginning of this `case` statement
17401740
return token = SyntaxKind.Unknown;
17411741
default:
17421742
if (isIdentifierStart(ch, languageVersion)) {

0 commit comments

Comments
 (0)