Skip to content

Commit

Permalink
Check that Symbol properties are proper, and support downlevel type c…
Browse files Browse the repository at this point in the history
…hecking
  • Loading branch information
JsonFreeman committed Feb 7, 2015
1 parent 3834edd commit 4c09ccd
Show file tree
Hide file tree
Showing 32 changed files with 457 additions and 27 deletions.
75 changes: 48 additions & 27 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ module ts {
var globalRegExpType: ObjectType;
var globalTemplateStringsArrayType: ObjectType;
var globalESSymbolType: ObjectType;
var globalESSymbolConstructorType: ObjectType;

var anyArrayType: Type;

Expand Down Expand Up @@ -3032,6 +3031,10 @@ module ts {
return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), 0);
}

function getGlobalESSymbolConstructorSymbol() {
return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"));
}

function createArrayType(elementType: Type): Type {
// globalArrayType will be undefined if we get here during creation of the Array type. This for example happens if
// user code augments the Array type with call or construct signatures that have an array type as the return type.
Expand Down Expand Up @@ -5541,12 +5544,7 @@ module ts {
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
}
else if (isWellKnownSymbolSyntactically(node.expression)) {
// If it's not ES6, error
// Check that Symbol corresponds to global symbol, and that property exists, and that it's a symbol
}
else {
// Some syntactic forms require that the property name be a well known symbol.
// Will move the grammar checks here.
checkSymbolNameIsProperSymbolReference(<PropertyAccessExpression>node.expression, links.resolvedType, /*reportError*/ true);
}
}

Expand Down Expand Up @@ -5795,7 +5793,7 @@ module ts {

// See if we can index as a property.
if (node.argumentExpression) {
var name = getPropertyNameForIndexedAccess(node.argumentExpression);
var name = getPropertyNameForIndexedAccess(node.argumentExpression, indexType);
if (name !== undefined) {
var prop = getPropertyOfType(objectType, name);
if (prop) {
Expand Down Expand Up @@ -5846,12 +5844,12 @@ module ts {
* to this symbol, as long as it is a proper symbol reference.
* Otherwise, returns undefined.
*/
function getPropertyNameForIndexedAccess(indexArgumentExpression: Expression): string {
function getPropertyNameForIndexedAccess(indexArgumentExpression: Expression, indexArgumentType: Type): string {
if (indexArgumentExpression.kind === SyntaxKind.StringLiteral || indexArgumentExpression.kind === SyntaxKind.NumericLiteral) {
return (<LiteralExpression>indexArgumentExpression).text;
}
if (languageVersion >= ScriptTarget.ES6 && isWellKnownSymbolSyntactically(indexArgumentExpression)) {
if (checkSymbolNameIsProperSymbolReference(<PropertyAccessExpression>indexArgumentExpression, /*reportError*/ false)) {
if (isWellKnownSymbolSyntactically(indexArgumentExpression)) {
if (checkSymbolNameIsProperSymbolReference(<PropertyAccessExpression>indexArgumentExpression, indexArgumentType, /*reportError*/ false)) {
var rightHandSideName = (<Identifier>(<PropertyAccessExpression>indexArgumentExpression).name).text;
return getPropertyNameForKnownSymbolName(rightHandSideName);
}
Expand All @@ -5862,31 +5860,52 @@ module ts {

/**
* A proper symbol reference requires the following:
* 1. The language version is at least ES6
* 2. The expression is of the form Symbol.<identifier>
* 3. Symbol in this context resolves to the global Symbol object
* 4. The property access denotes a property that is present on the global Symbol object
* 5. The property on the global Symbol object is of the primitive type symbol.
* 1. The expression is of the form Symbol.<identifier>
* 2. Symbol in this context resolves to the global Symbol object
* 3. The property access denotes a property that is present on the global Symbol object
* 4. The property on the global Symbol object is of the primitive type symbol.
*/
function checkSymbolNameIsProperSymbolReference(wellKnownSymbolName: PropertyAccessExpression, reportError: boolean): boolean {
if (languageVersion < ScriptTarget.ES6) {
function checkSymbolNameIsProperSymbolReference(wellKnownSymbolName: PropertyAccessExpression, propertyNameType: Type, reportError: boolean): boolean {
if (propertyNameType === unknownType) {
// There is already an error, so no need to report one.
return false;
}

Debug.assert(isWellKnownSymbolSyntactically(wellKnownSymbolName));

// Make sure the property type is the primitive symbol type
if ((propertyNameType.flags & TypeFlags.ESSymbol) === 0) {
if (reportError) {
error(wellKnownSymbolName, Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, getTextOfNode(wellKnownSymbolName));
}
return false;
}

// The name is Symbol.<someName>, so make sure Symbol actually resolves to the
// global Symbol object
var leftHandSide = (<PropertyAccessExpression>wellKnownSymbolName).expression;
// Look up the global symbol, but don't report an error, since checking the actual expression
// would have reported an error.
var leftHandSideSymbol = resolveName(wellKnownSymbolName, (<Identifier>leftHandSide).text,
SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
if (leftHandSideSymbol !== globalESSymbolConstructorSymbol) {
if (!leftHandSideSymbol) {
return false;
}

// Make sure the property type is the primitive symbol type
var rightHandSideName = (<Identifier>(<PropertyAccessExpression>wellKnownSymbolName).name).text;
var esSymbolConstructorPropertyType = getTypeOfPropertyOfType(globalESSymbolConstructorType, rightHandSideName);
return !!(esSymbolConstructorPropertyType && esSymbolConstructorPropertyType.flags & TypeFlags.ESSymbol);
var globalESSymbol = getGlobalESSymbolConstructorSymbol();
if (!globalESSymbol) {
// Already errored when we tried to look up the symbol
return false;
}

if (leftHandSideSymbol !== globalESSymbol) {
if (reportError) {
error(leftHandSide, Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object);
}
return false;
}

return true;
}

function resolveUntypedCall(node: CallLikeExpression): Signature {
Expand Down Expand Up @@ -10391,13 +10410,15 @@ module ts {
globalTemplateStringsArrayType = getGlobalType("TemplateStringsArray");
globalESSymbolType = getGlobalType("Symbol");
globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol");
globalESSymbolConstructorType = getTypeOfSymbol(globalESSymbolConstructorSymbol);
}
else {
globalTemplateStringsArrayType = unknownType;
globalESSymbolType = unknownType;
globalESSymbolConstructorSymbol = unknownSymbol;
globalESSymbolConstructorType = unknownType;

// Consider putting Symbol interface in lib.d.ts. On the plus side, putting it in lib.d.ts would make it
// extensible for Polyfilling Symbols. But putting it into lib.d.ts could also break users that have
// a global Symbol already, particularly if it is a class.
globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
globalESSymbolConstructorSymbol = undefined;
}

anyArrayType = createArrayType(anyType);
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ module ts {
A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type: { code: 2467, category: DiagnosticCategory.Error, key: "A computed property name cannot reference a type parameter from its containing type." },
Cannot_find_global_value_0: { code: 2468, category: DiagnosticCategory.Error, key: "Cannot find global value '{0}'." },
The_0_operator_cannot_be_applied_to_a_value_of_type_symbol: { code: 2469, category: DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to a value of type 'symbol'." },
Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." },
A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,14 @@
"category": "Error",
"code": 2469
},
"'Symbol' reference does not refer to the global Symbol constructor object.": {
"category": "Error",
"code": 2470
},
"A computed property name of the form '{0}' must be of type 'symbol'.": {
"category": "Error",
"code": 2471
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tests/cases/conformance/Symbols/ES5SymbolProperty1.ts(7,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty1.ts(7,6): error TS2471: A computed property name of the form 'Symbol.foo' must be of type 'symbol'.


==== tests/cases/conformance/Symbols/ES5SymbolProperty1.ts (2 errors) ====
interface SymbolConstructor {
foo: string;
}
var Symbol: SymbolConstructor;

var obj = {
[Symbol.foo]: 0
~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~~~~~~
!!! error TS2471: A computed property name of the form 'Symbol.foo' must be of type 'symbol'.

This comment has been minimized.

Copy link
@yuit

yuit Feb 10, 2015

Contributor

Why this error still get reported? I thought the above error from grammar check will stop error from propagating down

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Feb 10, 2015

Author Contributor

@DanielRosenwasser is adding downlevel support for computed properties, so I didn't think it was worth it to prevent cascading errors.

}

obj[Symbol.foo];
18 changes: 18 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [ES5SymbolProperty1.ts]
interface SymbolConstructor {
foo: string;
}
var Symbol: SymbolConstructor;

var obj = {
[Symbol.foo]: 0
}

obj[Symbol.foo];

//// [ES5SymbolProperty1.js]
var Symbol;
var obj = {
[Symbol.foo]: 0
};
obj[Symbol.foo];
22 changes: 22 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(5,9): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(5,10): error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.
tests/cases/conformance/Symbols/ES5SymbolProperty2.ts(10,11): error TS2304: Cannot find name 'Symbol'.


==== tests/cases/conformance/Symbols/ES5SymbolProperty2.ts (3 errors) ====
module M {
var Symbol;

export class C {
[Symbol.iterator]() { }
~~~~~~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~~~~~~~~~~~
!!! error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.
}
(new C)[Symbol.iterator];
}

(new M.C)[Symbol.iterator];
~~~~~~
!!! error TS2304: Cannot find name 'Symbol'.
27 changes: 27 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [ES5SymbolProperty2.ts]
module M {
var Symbol;

export class C {
[Symbol.iterator]() { }
}
(new C)[Symbol.iterator];
}

(new M.C)[Symbol.iterator];

//// [ES5SymbolProperty2.js]
var M;
(function (M) {
var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () {
};
return C;
})();
M.C = C;
(new C)[Symbol.iterator];
})(M || (M = {}));
(new M.C)[Symbol.iterator];
16 changes: 16 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/conformance/Symbols/ES5SymbolProperty3.ts(4,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty3.ts(4,6): error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.


==== tests/cases/conformance/Symbols/ES5SymbolProperty3.ts (2 errors) ====
var Symbol;

class C {
[Symbol.iterator]() { }
~~~~~~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~~~~~~~~~~~
!!! error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.
}

(new C)[Symbol.iterator]
19 changes: 19 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [ES5SymbolProperty3.ts]
var Symbol;

class C {
[Symbol.iterator]() { }
}

(new C)[Symbol.iterator]

//// [ES5SymbolProperty3.js]
var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () {
};
return C;
})();
(new C)[Symbol.iterator];
16 changes: 16 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/conformance/Symbols/ES5SymbolProperty4.ts(4,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty4.ts(4,6): error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.


==== tests/cases/conformance/Symbols/ES5SymbolProperty4.ts (2 errors) ====
var Symbol: { iterator: string };

class C {
[Symbol.iterator]() { }
~~~~~~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~~~~~~~~~~~
!!! error TS2471: A computed property name of the form 'Symbol.iterator' must be of type 'symbol'.
}

(new C)[Symbol.iterator]
19 changes: 19 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [ES5SymbolProperty4.ts]
var Symbol: { iterator: string };

class C {
[Symbol.iterator]() { }
}

(new C)[Symbol.iterator]

//// [ES5SymbolProperty4.js]
var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () {
};
return C;
})();
(new C)[Symbol.iterator];
16 changes: 16 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty5.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/conformance/Symbols/ES5SymbolProperty5.ts(4,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty5.ts(7,1): error TS2346: Supplied parameters do not match any signature of call target.


==== tests/cases/conformance/Symbols/ES5SymbolProperty5.ts (2 errors) ====
var Symbol: { iterator: symbol };

class C {
[Symbol.iterator]() { }
~~~~~~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
}

(new C)[Symbol.iterator](0) // Should error
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2346: Supplied parameters do not match any signature of call target.
19 changes: 19 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [ES5SymbolProperty5.ts]
var Symbol: { iterator: symbol };

class C {
[Symbol.iterator]() { }
}

(new C)[Symbol.iterator](0) // Should error

//// [ES5SymbolProperty5.js]
var Symbol;
var C = (function () {
function C() {
}
C.prototype[Symbol.iterator] = function () {
};
return C;
})();
(new C)[Symbol.iterator](0); // Should error
17 changes: 17 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(2,6): error TS2304: Cannot find name 'Symbol'.
tests/cases/conformance/Symbols/ES5SymbolProperty6.ts(5,9): error TS2304: Cannot find name 'Symbol'.


==== tests/cases/conformance/Symbols/ES5SymbolProperty6.ts (3 errors) ====
class C {
[Symbol.iterator]() { }
~~~~~~~~~~~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~~
!!! error TS2304: Cannot find name 'Symbol'.
}

(new C)[Symbol.iterator]
~~~~~~
!!! error TS2304: Cannot find name 'Symbol'.
Loading

0 comments on commit 4c09ccd

Please sign in to comment.