Skip to content

Commit f344654

Browse files
committed
Add named property symbol for known Symbol properties
1 parent 07f3641 commit f344654

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

Diff for: src/compiler/binder.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ module ts {
8787
if (symbolKind & SymbolFlags.Value && !symbol.valueDeclaration) symbol.valueDeclaration = node;
8888
}
8989

90-
// Should not be called on a declaration with a computed property name.
90+
// Should not be called on a declaration with a computed property name,
91+
// unless it is a well known Symbol.
9192
function getDeclarationName(node: Declaration): string {
9293
if (node.name) {
9394
if (node.kind === SyntaxKind.ModuleDeclaration && node.name.kind === SyntaxKind.StringLiteral) {
9495
return '"' + (<LiteralExpression>node.name).text + '"';
9596
}
96-
Debug.assert(!hasDynamicName(node));
97+
if (node.name.kind === SyntaxKind.ComputedPropertyName) {
98+
var nameExpression = (<ComputedPropertyName>node.name).expression;
99+
Debug.assert(isWellKnownSymbolSyntactically(nameExpression));
100+
return "__@" + (<PropertyAccessExpression>nameExpression).name.text;
101+
}
97102
return (<Identifier | LiteralExpression>node.name).text;
98103
}
99104
switch (node.kind) {

Diff for: src/compiler/checker.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,15 @@ module ts {
706706
return type;
707707
}
708708

709-
// A reserved member name starts with two underscores followed by a non-underscore
709+
// A reserved member name starts with two underscores, but the third character cannot be an underscore
710+
// or the @ symbol. A third underscore indicates an escaped form of an identifer that started
711+
// with at least two underscores. The @ character indicates that the name is denoted by a well known ES
712+
// Symbol instance.
710713
function isReservedMemberName(name: string) {
711-
return name.charCodeAt(0) === CharacterCodes._ && name.charCodeAt(1) === CharacterCodes._ && name.charCodeAt(2) !== CharacterCodes._;
714+
return name.charCodeAt(0) === CharacterCodes._ &&
715+
name.charCodeAt(1) === CharacterCodes._ &&
716+
name.charCodeAt(2) !== CharacterCodes._ &&
717+
name.charCodeAt(2) !== CharacterCodes.at;
712718
}
713719

714720
function getNamedMembers(members: SymbolTable): Symbol[] {

Diff for: src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ module ts {
848848
!isWellKnownSymbolSyntactically((<ComputedPropertyName>declaration.name).expression);
849849
}
850850

851-
export function isWellKnownSymbolSyntactically(node: Node): boolean {
851+
export function isWellKnownSymbolSyntactically(node: Expression): boolean {
852852
return node.kind === SyntaxKind.PropertyAccessExpression && isESSymbolIdentifier((<PropertyAccessExpression>node).expression);
853853
}
854854

Diff for: tests/baselines/reference/parserSymbolProperty8.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty8.ts ===
22
var x: {
3-
>x : {}
3+
>x : { [Symbol.toPrimitive](): string; }
44

55
[Symbol.toPrimitive](): string
66
>Symbol.toPrimitive : Symbol

Diff for: tests/baselines/reference/parserSymbolProperty9.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty9.ts ===
22
var x: {
3-
>x : {}
3+
>x : { [Symbol.toPrimitive]: string; }
44

55
[Symbol.toPrimitive]: string
66
>Symbol.toPrimitive : Symbol

0 commit comments

Comments
 (0)