@@ -5540,6 +5540,14 @@ module ts {
5540
5540
if (!isTypeOfKind(links.resolvedType, TypeFlags.Any | TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol)) {
5541
5541
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
5542
5542
}
5543
+ else if (isWellKnownSymbolSyntactically(node.expression)) {
5544
+ // If it's not ES6, error
5545
+ // Check that Symbol corresponds to global symbol, and that property exists, and that it's a symbol
5546
+ }
5547
+ else {
5548
+ // Some syntactic forms require that the property name be a well known symbol.
5549
+ // Will move the grammar checks here.
5550
+ }
5543
5551
}
5544
5552
5545
5553
return links.resolvedType;
@@ -5835,33 +5843,52 @@ module ts {
5835
5843
/**
5836
5844
* If indexArgumentExpression is a string literal or number literal, returns its text.
5837
5845
* If indexArgumentExpression is a well known symbol, returns the property name corresponding
5838
- * to this symbol.
5846
+ * to this symbol, as long as it is a proper symbol reference .
5839
5847
* Otherwise, returns undefined.
5840
5848
*/
5841
- function getPropertyNameForIndexedAccess(indexArgumentExpression: Expression) {
5849
+ function getPropertyNameForIndexedAccess(indexArgumentExpression: Expression): string {
5842
5850
if (indexArgumentExpression.kind === SyntaxKind.StringLiteral || indexArgumentExpression.kind === SyntaxKind.NumericLiteral) {
5843
5851
return (<LiteralExpression>indexArgumentExpression).text;
5844
5852
}
5845
- if (isWellKnownSymbolSyntactically(indexArgumentExpression)) {
5846
- var leftHandSide = (<PropertyAccessExpression>indexArgumentExpression).expression;
5847
- Debug.assert((<Identifier>leftHandSide).text === "Symbol");
5848
- // The name is Symbol.<someName>, so make sure Symbol actually resolves to the
5849
- // global Symbol object
5850
- var leftHandSideSymbol = resolveName(indexArgumentExpression, (<Identifier>leftHandSide).text,
5851
- SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
5852
- if (leftHandSideSymbol === globalESSymbolConstructorSymbol) {
5853
- // Make sure the property type is the primitive symbol type
5853
+ if (languageVersion >= ScriptTarget.ES6 && isWellKnownSymbolSyntactically(indexArgumentExpression)) {
5854
+ if (checkSymbolNameIsProperSymbolReference(<PropertyAccessExpression>indexArgumentExpression, /*reportError*/ false)) {
5854
5855
var rightHandSideName = (<Identifier>(<PropertyAccessExpression>indexArgumentExpression).name).text;
5855
- var esSymbolConstructorPropertyType = getTypeOfPropertyOfType(globalESSymbolConstructorType, rightHandSideName);
5856
- if (esSymbolConstructorPropertyType && esSymbolConstructorPropertyType.flags & TypeFlags.ESSymbol) {
5857
- return getPropertyNameForKnownSymbolName(rightHandSideName);
5858
- }
5856
+ return getPropertyNameForKnownSymbolName(rightHandSideName);
5859
5857
}
5860
5858
}
5861
5859
5862
5860
return undefined;
5863
5861
}
5864
5862
5863
+ /**
5864
+ * A proper symbol reference requires the following:
5865
+ * 1. The language version is at least ES6
5866
+ * 2. The expression is of the form Symbol.<identifier>
5867
+ * 3. Symbol in this context resolves to the global Symbol object
5868
+ * 4. The property access denotes a property that is present on the global Symbol object
5869
+ * 5. The property on the global Symbol object is of the primitive type symbol.
5870
+ */
5871
+ function checkSymbolNameIsProperSymbolReference(wellKnownSymbolName: PropertyAccessExpression, reportError: boolean): boolean {
5872
+ if (languageVersion < ScriptTarget.ES6) {
5873
+ return false;
5874
+ }
5875
+
5876
+ Debug.assert(isWellKnownSymbolSyntactically(wellKnownSymbolName));
5877
+ // The name is Symbol.<someName>, so make sure Symbol actually resolves to the
5878
+ // global Symbol object
5879
+ var leftHandSide = (<PropertyAccessExpression>wellKnownSymbolName).expression;
5880
+ var leftHandSideSymbol = resolveName(wellKnownSymbolName, (<Identifier>leftHandSide).text,
5881
+ SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
5882
+ if (leftHandSideSymbol !== globalESSymbolConstructorSymbol) {
5883
+ return false;
5884
+ }
5885
+
5886
+ // Make sure the property type is the primitive symbol type
5887
+ var rightHandSideName = (<Identifier>(<PropertyAccessExpression>wellKnownSymbolName).name).text;
5888
+ var esSymbolConstructorPropertyType = getTypeOfPropertyOfType(globalESSymbolConstructorType, rightHandSideName);
5889
+ return !!(esSymbolConstructorPropertyType && esSymbolConstructorPropertyType.flags & TypeFlags.ESSymbol);
5890
+ }
5891
+
5865
5892
function resolveUntypedCall(node: CallLikeExpression): Signature {
5866
5893
if (node.kind === SyntaxKind.TaggedTemplateExpression) {
5867
5894
checkExpression((<TaggedTemplateExpression>node).template);
0 commit comments