Skip to content

Commit

Permalink
Add type checking for computed properties in object literals
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Jan 20, 2015
1 parent ef06879 commit 7192767
Show file tree
Hide file tree
Showing 25 changed files with 120 additions and 64 deletions.
18 changes: 17 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5395,6 +5395,17 @@ module ts {
return (+name).toString() === name;
}

function checkComputedPropertyName(node: ComputedPropertyName): void {
var computedNameType = checkExpression(node.expression);

// This will only allow types number, string, or any. Any types more complex will
// be disallowed, even union types like string | number. In the future, we might consider
// allowing types like that.
if ((computedNameType.flags & (TypeFlags.Number | TypeFlags.String | TypeFlags.Any)) === 0) {
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_or_any);
}
}

function checkObjectLiteral(node: ObjectLiteralExpression, contextualMapper?: TypeMapper): Type {
// Grammar checking
checkGrammarObjectLiteralExpression(node);
Expand Down Expand Up @@ -5443,7 +5454,12 @@ module ts {
checkAccessorDeclaration(<AccessorDeclaration>memberDecl);
}

properties[member.name] = member;
if (hasComputedNameButNotSymbol(memberDecl)) {
checkComputedPropertyName(<ComputedPropertyName>memberDecl.name);
}
else {
properties[member.name] = member;
}
}

var stringIndexType = getIndexType(IndexKind.String);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ module ts {
Type_0_is_not_an_array_type: { code: 2461, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type." },
A_rest_element_must_be_last_in_an_array_destructuring_pattern: { code: 2462, category: DiagnosticCategory.Error, key: "A rest element must be last in an array destructuring pattern" },
A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature: { code: 2463, category: DiagnosticCategory.Error, key: "A binding pattern parameter cannot be optional in an implementation signature." },
A_computed_property_name_must_be_of_type_string_number_or_any: { code: 2464, category: DiagnosticCategory.Error, key: "A computed property name must be of type 'string', 'number', or 'any'." },
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: 6 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@
},
"A parameter property may not be a binding pattern.": {
"category": "Error",
"code": 1187
"code": 1187
},

"Duplicate identifier '{0}'.": {
Expand Down Expand Up @@ -1288,7 +1288,11 @@
},
"A binding pattern parameter cannot be optional in an implementation signature.": {
"category": "Error",
"code": 2463
"code": 2463
},
"A computed property name must be of type 'string', 'number', or 'any'.": {
"category": "Error",
"code": 2464
},

"Import declaration '{0}' is using private name '{1}'.": {
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ module ts {
return node === (<TypeAssertion>parent).expression;
case SyntaxKind.TemplateSpan:
return node === (<TemplateSpan>parent).expression;
case SyntaxKind.ComputedPropertyName:
return node === (<ComputedPropertyName>parent).expression;
default:
if (isExpression(parent)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration8_es6.ts(1,11): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration8_es6.ts(1,12): error TS2304: Cannot find name 'yield'.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration8_es6.ts(1,20): error TS2304: Cannot find name 'foo'.


==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration8_es6.ts (2 errors) ====
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration8_es6.ts (3 errors) ====
var v = { [yield]: foo }
~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
~~~
!!! error TS2304: Cannot find name 'foo'.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration9_es6.ts(1,10): error TS9001: Generators are not currently supported.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration9_es6.ts(2,13): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration9_es6.ts(2,14): error TS9000: 'yield' expressions are not currently supported.


==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration9_es6.ts (2 errors) ====
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration9_es6.ts (3 errors) ====
function * foo() {
~
!!! error TS9001: Generators are not currently supported.
var v = { [yield]: foo }
~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~~~
!!! error TS9000: 'yield' expressions are not currently supported.
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
tests/cases/conformance/es6/functionPropertyAssignments/FunctionPropertyAssignments5_es6.ts(1,11): error TS9001: Generators are not currently supported.
tests/cases/conformance/es6/functionPropertyAssignments/FunctionPropertyAssignments5_es6.ts(1,12): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/es6/functionPropertyAssignments/FunctionPropertyAssignments5_es6.ts(1,13): error TS2304: Cannot find name 'foo'.


==== tests/cases/conformance/es6/functionPropertyAssignments/FunctionPropertyAssignments5_es6.ts (2 errors) ====
==== tests/cases/conformance/es6/functionPropertyAssignments/FunctionPropertyAssignments5_es6.ts (3 errors) ====
var v = { *[foo()]() { } }
~
!!! error TS9001: Generators are not currently supported.
~~~~~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~
!!! error TS2304: Cannot find name 'foo'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName1.ts(1,12): error TS2304: Cannot find name 'e'.
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName1.ts(1,15): error TS1005: ':' expected.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName1.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName1.ts (2 errors) ====
var v = { [e] };
~
!!! error TS2304: Cannot find name 'e'.
~
!!! error TS1005: ':' expected.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName17.ts(1,16): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName17.ts (1 errors) ====
var v = { set [e](v) { } }
~
!!! error TS2304: Cannot find name 'e'.
7 changes: 0 additions & 7 deletions tests/baselines/reference/parserComputedPropertyName17.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName2.ts(1,12): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName2.ts (1 errors) ====
var v = { [e]: 1 };
~
!!! error TS2304: Cannot find name 'e'.
6 changes: 0 additions & 6 deletions tests/baselines/reference/parserComputedPropertyName2.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName3.ts(1,12): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName3.ts (1 errors) ====
var v = { [e]() { } };
~
!!! error TS2304: Cannot find name 'e'.
6 changes: 0 additions & 6 deletions tests/baselines/reference/parserComputedPropertyName3.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName37.ts(2,6): error TS2304: Cannot find name 'public'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName37.ts (1 errors) ====
var v = {
[public]: 0
~~~~~~
!!! error TS2304: Cannot find name 'public'.
};
9 changes: 0 additions & 9 deletions tests/baselines/reference/parserComputedPropertyName37.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName4.ts(1,15): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName4.ts(1,16): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName4.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName4.ts (2 errors) ====
var v = { get [e]() { } };
~~~
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
~
!!! error TS2304: Cannot find name 'e'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName41.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName41.ts (1 errors) ====
var v = {
[0 in []]: true
~~~~~~~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'.
}
9 changes: 0 additions & 9 deletions tests/baselines/reference/parserComputedPropertyName41.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName5.ts(1,22): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName5.ts(1,23): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName5.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName5.ts (2 errors) ====
var v = { public get [e]() { } };
~~~
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
~
!!! error TS2304: Cannot find name 'e'.
13 changes: 13 additions & 0 deletions tests/baselines/reference/parserComputedPropertyName6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName6.ts(1,12): error TS2304: Cannot find name 'e'.
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName6.ts(1,20): error TS2304: Cannot find name 'e'.
tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName6.ts(1,24): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName6.ts (3 errors) ====
var v = { [e]: 1, [e + e]: 2 };
~
!!! error TS2304: Cannot find name 'e'.
~
!!! error TS2304: Cannot find name 'e'.
~
!!! error TS2304: Cannot find name 'e'.
9 changes: 0 additions & 9 deletions tests/baselines/reference/parserComputedPropertyName6.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName2.ts(1,11): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName2.ts(1,12): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName2.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName2.ts (2 errors) ====
var v = { [e]: 1 };
~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~
!!! error TS2304: Cannot find name 'e'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName3.ts(1,11): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName3.ts(1,12): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName3.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName3.ts (2 errors) ====
var v = { [e]() { } };
~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~
!!! error TS2304: Cannot find name 'e'.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts(1,15): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts(1,15): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts(1,16): error TS2304: Cannot find name 'e'.


==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts (2 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ComputedPropertyNames/parserES5ComputedPropertyName4.ts (3 errors) ====
var v = { get [e]() { } };
~~~
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher.
~~~
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
!!! error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
~
!!! error TS2304: Cannot find name 'e'.

0 comments on commit 7192767

Please sign in to comment.