Skip to content

Commit

Permalink
Add named property symbol for known Symbol properties
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Feb 7, 2015
1 parent 07f3641 commit f344654
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ module ts {
if (symbolKind & SymbolFlags.Value && !symbol.valueDeclaration) symbol.valueDeclaration = node;
}

// Should not be called on a declaration with a computed property name.
// Should not be called on a declaration with a computed property name,
// unless it is a well known Symbol.
function getDeclarationName(node: Declaration): string {
if (node.name) {
if (node.kind === SyntaxKind.ModuleDeclaration && node.name.kind === SyntaxKind.StringLiteral) {
return '"' + (<LiteralExpression>node.name).text + '"';
}
Debug.assert(!hasDynamicName(node));
if (node.name.kind === SyntaxKind.ComputedPropertyName) {
var nameExpression = (<ComputedPropertyName>node.name).expression;
Debug.assert(isWellKnownSymbolSyntactically(nameExpression));
return "__@" + (<PropertyAccessExpression>nameExpression).name.text;
}
return (<Identifier | LiteralExpression>node.name).text;
}
switch (node.kind) {
Expand Down
10 changes: 8 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,15 @@ module ts {
return type;
}

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

function getNamedMembers(members: SymbolTable): Symbol[] {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ module ts {
!isWellKnownSymbolSyntactically((<ComputedPropertyName>declaration.name).expression);
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/parserSymbolProperty8.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty8.ts ===
var x: {
>x : {}
>x : { [Symbol.toPrimitive](): string; }

[Symbol.toPrimitive](): string
>Symbol.toPrimitive : Symbol
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/parserSymbolProperty9.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty9.ts ===
var x: {
>x : {}
>x : { [Symbol.toPrimitive]: string; }

[Symbol.toPrimitive]: string
>Symbol.toPrimitive : Symbol
Expand Down

1 comment on commit f344654

@yuit
Copy link
Contributor

@yuit yuit commented on f344654 Feb 10, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.