Skip to content

private-names (main branch) #1

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 1 commit 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
1 change: 1 addition & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ namespace ts {
if (token() !== SyntaxKind.Identifier) {
node.originalKeywordKind = token();
}
node.isPrivateName = !!(scanner.getTokenFlags() & TokenFlags.PrivateName);
node.escapedText = escapeLeadingUnderscores(internIdentifier(scanner.getTokenValue()));
nextToken();
return finishNode(node);
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,24 @@ namespace ts {
error(Diagnostics.Invalid_character);
pos++;
return token = SyntaxKind.Unknown;
case CharacterCodes.hash:
pos++;
if (
languageVersion === ScriptTarget.ESNext
&& 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pos": 2,
"end": 3,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"pos": 10,
"end": 11,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"end": 4,
"flags": "JSDoc",
"originalKeywordKind": "VarKeyword",
"isPrivateName": false,
"escapedText": "var"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
},
"parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"end": 13,
"flags": "JSDoc",
"originalKeywordKind": "NewKeyword",
"isPrivateName": false,
"escapedText": "new"
},
"type": {
Expand All @@ -32,13 +33,15 @@
"pos": 14,
"end": 15,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
},
"right": {
"kind": "Identifier",
"pos": 16,
"end": 17,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "b"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
},
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
}
},
Expand All @@ -27,6 +28,7 @@
"pos": 6,
"end": 10,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "bar"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
},
"type": {
Expand All @@ -33,6 +34,7 @@
"pos": 14,
"end": 18,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "bar"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
}
},
Expand All @@ -27,6 +28,7 @@
"pos": 6,
"end": 10,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "bar"
},
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 5,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "foo"
},
"type": {
Expand All @@ -33,6 +34,7 @@
"pos": 14,
"end": 18,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "bar"
},
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"end": 10,
"flags": "JSDoc",
"originalKeywordKind": "FunctionKeyword",
"isPrivateName": false,
"escapedText": "function"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"end": 14,
"flags": "JSDoc",
"originalKeywordKind": "ThisKeyword",
"isPrivateName": false,
"escapedText": "this"
},
"type": {
Expand All @@ -32,13 +33,15 @@
"pos": 15,
"end": 16,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
},
"right": {
"kind": "Identifier",
"pos": 17,
"end": 18,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "b"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"pos": 2,
"end": 3,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
},
"typeArguments": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"pos": 7,
"end": 9,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "M"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a",
"jsdocDotPos": 2
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a",
"jsdocDotPos": 2
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"pos": 1,
"end": 2,
"flags": "JSDoc",
"isPrivateName": false,
"escapedText": "a"
},
"right": {
Expand All @@ -21,6 +22,7 @@
"end": 11,
"flags": "JSDoc",
"originalKeywordKind": "FunctionKeyword",
"isPrivateName": false,
"escapedText": "function"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,9): error TS2304: Cannot find name 'a'.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,12): error TS1127: Invalid character.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,12): error TS1109: Expression expected.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,13): error TS1127: Invalid character.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,14): error TS1109: Expression expected.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(4,16): error TS2304: Cannot find name 'bar'.
tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts(5,12): error TS2304: Cannot find name 'bar'.


==== tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts (5 errors) ====
==== tests/cases/conformance/es6/memberFunctionDeclarations/MemberFunctionDeclaration8_es6.ts (6 errors) ====
class C {
foo() {
// Make sure we don't think of *bar as the start of a generator method.
if (a) # * bar;
~
!!! error TS2304: Cannot find name 'a'.

~
!!! error TS1109: Expression expected.

!!! error TS1127: Invalid character.
~
!!! error TS1109: Expression expected.
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ declare namespace ts {
*/
escapedText: __String;
originalKeywordKind?: SyntaxKind;
isPrivateName: boolean;
autoGenerateFlags?: GeneratedIdentifierFlags;
autoGenerateId?: number;
isInJSDocNamespace?: boolean;
Expand Down Expand Up @@ -1573,6 +1574,7 @@ declare namespace ts {
BinarySpecifier = 128,
OctalSpecifier = 256,
ContainsSeparator = 512,
PrivateName = 1024,
BinaryOrOctalSpecifier = 384,
NumericLiteralFlags = 1008
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ declare namespace ts {
*/
escapedText: __String;
originalKeywordKind?: SyntaxKind;
isPrivateName: boolean;
isInJSDocNamespace?: boolean;
}
interface TransientIdentifier extends Identifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
tests/cases/compiler/parseErrorInHeritageClause1.ts(1,17): error TS2304: Cannot find name 'A'.
tests/cases/compiler/parseErrorInHeritageClause1.ts(1,19): error TS1127: Invalid character.
tests/cases/compiler/parseErrorInHeritageClause1.ts(1,19): error TS1005: ',' expected.
tests/cases/compiler/parseErrorInHeritageClause1.ts(1,20): error TS1127: Invalid character.


==== tests/cases/compiler/parseErrorInHeritageClause1.ts (2 errors) ====
==== tests/cases/compiler/parseErrorInHeritageClause1.ts (3 errors) ====
class C extends A # {
~
!!! error TS2304: Cannot find name 'A'.

~
!!! error TS1005: ',' expected.

!!! error TS1127: Invalid character.
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block2.ts(2,5): error TS1127: Invalid character.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block2.ts(2,5): error TS1128: Declaration or statement expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block2.ts(2,6): error TS1127: Invalid character.


==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block2.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/Blocks/parserErrorRecovery_Block2.ts (2 errors) ====
function f() {
#

~
!!! error TS1128: Declaration or statement expected.

!!! error TS1127: Invalid character.
return;
}
Loading