Skip to content

Commit 7eb0aa1

Browse files
committed
Merge pull request #1752 from Microsoft/computedProperties
Computed properties (but not known symbols)
2 parents 29776f4 + bd29ca8 commit 7eb0aa1

File tree

286 files changed

+4994
-438
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

286 files changed

+4994
-438
lines changed

src/compiler/binder.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ module ts {
5050
}
5151

5252
/**
53-
* Returns false if any of the following are true:
54-
* 1. declaration has no name
55-
* 2. declaration has a literal name (not computed)
56-
* 3. declaration has a computed property name that is a known symbol
53+
* A declaration has a dynamic name if both of the following are true:
54+
* 1. The declaration has a computed property name
55+
* 2. The computed name is *not* expressed as Symbol.<name>, where name
56+
* is a property of the Symbol constructor that denotes a built in
57+
* Symbol.
5758
*/
58-
export function hasComputedNameButNotSymbol(declaration: Declaration): boolean {
59+
export function hasDynamicName(declaration: Declaration): boolean {
5960
return declaration.name && declaration.name.kind === SyntaxKind.ComputedPropertyName;
6061
}
6162

@@ -96,7 +97,7 @@ module ts {
9697
if (node.kind === SyntaxKind.ModuleDeclaration && node.name.kind === SyntaxKind.StringLiteral) {
9798
return '"' + (<LiteralExpression>node.name).text + '"';
9899
}
99-
Debug.assert(!hasComputedNameButNotSymbol(node));
100+
Debug.assert(!hasDynamicName(node));
100101
return (<Identifier | LiteralExpression>node.name).text;
101102
}
102103
switch (node.kind) {
@@ -118,11 +119,7 @@ module ts {
118119
}
119120

120121
function declareSymbol(symbols: SymbolTable, parent: Symbol, node: Declaration, includes: SymbolFlags, excludes: SymbolFlags): Symbol {
121-
// Nodes with computed property names will not get symbols, because the type checker
122-
// does not make properties for them.
123-
if (hasComputedNameButNotSymbol(node)) {
124-
return undefined;
125-
}
122+
Debug.assert(!hasDynamicName(node));
126123

127124
var name = getDeclarationName(node);
128125
if (name !== undefined) {
@@ -395,14 +392,14 @@ module ts {
395392
break;
396393
case SyntaxKind.PropertyDeclaration:
397394
case SyntaxKind.PropertySignature:
398-
bindDeclaration(<Declaration>node, SymbolFlags.Property | ((<PropertyDeclaration>node).questionToken ? SymbolFlags.Optional : 0), SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
395+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.Property | ((<PropertyDeclaration>node).questionToken ? SymbolFlags.Optional : 0), SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
399396
break;
400397
case SyntaxKind.PropertyAssignment:
401398
case SyntaxKind.ShorthandPropertyAssignment:
402-
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
399+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
403400
break;
404401
case SyntaxKind.EnumMember:
405-
bindDeclaration(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes, /*isBlockScopeContainer*/ false);
402+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes, /*isBlockScopeContainer*/ false);
406403
break;
407404
case SyntaxKind.CallSignature:
408405
case SyntaxKind.ConstructSignature:
@@ -415,7 +412,7 @@ module ts {
415412
// as other properties in the object literal. So we use SymbolFlags.PropertyExcludes
416413
// so that it will conflict with any other object literal members with the same
417414
// name.
418-
bindDeclaration(<Declaration>node, SymbolFlags.Method | ((<MethodDeclaration>node).questionToken ? SymbolFlags.Optional : 0),
415+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.Method | ((<MethodDeclaration>node).questionToken ? SymbolFlags.Optional : 0),
419416
isObjectLiteralMethod(node) ? SymbolFlags.PropertyExcludes : SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
420417
break;
421418
case SyntaxKind.FunctionDeclaration:
@@ -425,10 +422,10 @@ module ts {
425422
bindDeclaration(<Declaration>node, SymbolFlags.Constructor, /*symbolExcludes:*/ 0, /*isBlockScopeContainer:*/ true);
426423
break;
427424
case SyntaxKind.GetAccessor:
428-
bindDeclaration(<Declaration>node, SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes, /*isBlockScopeContainer*/ true);
425+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes, /*isBlockScopeContainer*/ true);
429426
break;
430427
case SyntaxKind.SetAccessor:
431-
bindDeclaration(<Declaration>node, SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes, /*isBlockScopeContainer*/ true);
428+
bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes, /*isBlockScopeContainer*/ true);
432429
break;
433430

434431
case SyntaxKind.FunctionType:
@@ -510,5 +507,14 @@ module ts {
510507
declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
511508
}
512509
}
510+
511+
function bindPropertyOrMethodOrAccessor(node: Declaration, symbolKind: SymbolFlags, symbolExcludes: SymbolFlags, isBlockScopeContainer: boolean) {
512+
if (hasDynamicName(node)) {
513+
bindAnonymousDeclaration(node, symbolKind, "__computed", isBlockScopeContainer);
514+
}
515+
else {
516+
bindDeclaration(node, symbolKind, symbolExcludes, isBlockScopeContainer);
517+
}
518+
}
513519
}
514520
}

0 commit comments

Comments
 (0)