Skip to content

Commit 11d75ef

Browse files
committed
Allow Symbol indexer in ES6
1 parent ff31b96 commit 11d75ef

31 files changed

+195
-28
lines changed

Diff for: src/compiler/checker.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -10472,25 +10472,33 @@ module ts {
1047210472
return grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_exactly_one_parameter);
1047310473
}
1047410474
}
10475-
else if (parameter.dotDotDotToken) {
10475+
if (parameter.dotDotDotToken) {
1047610476
return grammarErrorOnNode(parameter.dotDotDotToken, Diagnostics.An_index_signature_cannot_have_a_rest_parameter);
1047710477
}
10478-
else if (parameter.flags & NodeFlags.Modifier) {
10478+
if (parameter.flags & NodeFlags.Modifier) {
1047910479
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier);
1048010480
}
10481-
else if (parameter.questionToken) {
10481+
if (parameter.questionToken) {
1048210482
return grammarErrorOnNode(parameter.questionToken, Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark);
1048310483
}
10484-
else if (parameter.initializer) {
10484+
if (parameter.initializer) {
1048510485
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_an_initializer);
1048610486
}
10487-
else if (!parameter.type) {
10487+
if (!parameter.type) {
1048810488
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation);
1048910489
}
10490-
else if (parameter.type.kind !== SyntaxKind.StringKeyword && parameter.type.kind !== SyntaxKind.NumberKeyword) {
10491-
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_or_number);
10490+
if (parameter.type.kind !== SyntaxKind.StringKeyword && parameter.type.kind !== SyntaxKind.NumberKeyword) {
10491+
if (isESSymbolTypeNode(parameter.type)) {
10492+
if (languageVersion < ScriptTarget.ES6) {
10493+
return grammarErrorOnNode(parameter.type, Diagnostics.Symbol_indexers_are_only_available_when_targeting_ECMAScript_6_and_higher);
10494+
}
10495+
// No error for parameter type
10496+
}
10497+
else {
10498+
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_number_or_Symbol);
10499+
}
1049210500
}
10493-
else if (!node.type) {
10501+
if (!node.type) {
1049410502
return grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_a_type_annotation);
1049510503
}
1049610504
}

Diff for: src/compiler/diagnosticInformationMap.generated.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module ts {
1919
An_index_signature_parameter_cannot_have_an_initializer: { code: 1020, category: DiagnosticCategory.Error, key: "An index signature parameter cannot have an initializer." },
2020
An_index_signature_must_have_a_type_annotation: { code: 1021, category: DiagnosticCategory.Error, key: "An index signature must have a type annotation." },
2121
An_index_signature_parameter_must_have_a_type_annotation: { code: 1022, category: DiagnosticCategory.Error, key: "An index signature parameter must have a type annotation." },
22-
An_index_signature_parameter_type_must_be_string_or_number: { code: 1023, category: DiagnosticCategory.Error, key: "An index signature parameter type must be 'string' or 'number'." },
22+
An_index_signature_parameter_type_must_be_string_number_or_Symbol: { code: 1023, category: DiagnosticCategory.Error, key: "An index signature parameter type must be 'string', 'number', or 'Symbol'." },
2323
A_class_or_interface_declaration_can_only_have_one_extends_clause: { code: 1024, category: DiagnosticCategory.Error, key: "A class or interface declaration can only have one 'extends' clause." },
2424
An_extends_clause_must_precede_an_implements_clause: { code: 1025, category: DiagnosticCategory.Error, key: "An 'extends' clause must precede an 'implements' clause." },
2525
A_class_can_only_extend_a_single_class: { code: 1026, category: DiagnosticCategory.Error, key: "A class can only extend a single class." },
@@ -147,6 +147,7 @@ module ts {
147147
Merge_conflict_marker_encountered: { code: 1185, category: DiagnosticCategory.Error, key: "Merge conflict marker encountered." },
148148
A_rest_element_cannot_have_an_initializer: { code: 1186, category: DiagnosticCategory.Error, key: "A rest element cannot have an initializer." },
149149
A_parameter_property_may_not_be_a_binding_pattern: { code: 1187, category: DiagnosticCategory.Error, key: "A parameter property may not be a binding pattern." },
150+
Symbol_indexers_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1188, category: DiagnosticCategory.Error, key: "'Symbol' indexers are only available when targeting ECMAScript 6 and higher.", isEarly: true },
150151
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
151152
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
152153
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

Diff for: src/compiler/diagnosticMessages.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"category": "Error",
6868
"code": 1022
6969
},
70-
"An index signature parameter type must be 'string' or 'number'.": {
70+
"An index signature parameter type must be 'string', 'number', or 'Symbol'.": {
7171
"category": "Error",
7272
"code": 1023
7373
},
@@ -579,6 +579,11 @@
579579
"category": "Error",
580580
"code": 1187
581581
},
582+
"'Symbol' indexers are only available when targeting ECMAScript 6 and higher.": {
583+
"category": "Error",
584+
"code": 1188,
585+
"isEarly": true
586+
},
582587

583588
"Duplicate identifier '{0}'.": {
584589
"category": "Error",

Diff for: src/compiler/utilities.ts

+7
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,13 @@ module ts {
835835
return SyntaxKind.FirstTriviaToken <= token && token <= SyntaxKind.LastTriviaToken;
836836
}
837837

838+
export function isESSymbolTypeNode(node: Node): boolean {
839+
return node.kind === SyntaxKind.TypeReference &&
840+
(<TypeReferenceNode>node).typeArguments === undefined &&
841+
(<TypeReferenceNode>node).typeName.kind === SyntaxKind.Identifier &&
842+
(<Identifier>(<TypeReferenceNode>node).typeName).text === "Symbol";
843+
}
844+
838845
export function isModifier(token: SyntaxKind): boolean {
839846
switch (token) {
840847
case SyntaxKind.PublicKeyword:

Diff for: tests/baselines/reference/arraySigChecking.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/arraySigChecking.ts(11,17): error TS1023: An index signature parameter type must be 'string' or 'number'.
1+
tests/cases/compiler/arraySigChecking.ts(11,17): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
22
tests/cases/compiler/arraySigChecking.ts(18,5): error TS2322: Type 'void[]' is not assignable to type 'string[]'.
33
Type 'void' is not assignable to type 'string'.
44
tests/cases/compiler/arraySigChecking.ts(22,1): error TS2322: Type 'number[][]' is not assignable to type 'number[][][]'.
@@ -20,7 +20,7 @@ tests/cases/compiler/arraySigChecking.ts(22,1): error TS2322: Type 'number[][]'
2020

2121
var foo: { [index: any]; }; // expect an error here
2222
~~~~~
23-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
23+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
2424
}
2525

2626
interface myInt {

Diff for: tests/baselines/reference/genericTypeReferenceWithoutTypeArgument.d.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
22
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(10,21): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
33
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
44
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
5-
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,19): error TS1023: An index signature parameter type must be 'string' or 'number'.
5+
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,19): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
66
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
77
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
88
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(14,23): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
@@ -36,7 +36,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
3636
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
3737
declare var d: { [x: C]: C };
3838
~
39-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
39+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
4040
~
4141
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
4242
~

Diff for: tests/baselines/reference/genericTypeReferenceWithoutTypeArgument.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
22
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(10,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
33
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
44
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
5-
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
5+
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,11): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
66
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
77
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
88
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(14,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
@@ -46,7 +46,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
4646
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
4747
var d: { [x: C]: C };
4848
~
49-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
49+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
5050
~
5151
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
5252
~

Diff for: tests/baselines/reference/genericTypeReferenceWithoutTypeArgument2.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
22
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(10,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
33
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
44
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
5-
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
5+
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,11): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
66
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
77
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
88
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(14,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
@@ -46,7 +46,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
4646
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
4747
var d: { [x: I]: I };
4848
~
49-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
49+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
5050
~
5151
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
5252
~

Diff for: tests/baselines/reference/genericTypeReferenceWithoutTypeArgument3.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
22
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(10,21): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
33
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(11,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
44
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(11,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
5-
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,19): error TS1023: An index signature parameter type must be 'string' or 'number'.
5+
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,19): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
66
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
77
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
88
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(14,23): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
@@ -36,7 +36,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
3636
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
3737
declare var d: { [x: C]: C };
3838
~
39-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
39+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
4040
~
4141
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
4242
~

Diff for: tests/baselines/reference/indexTypeCheck.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type '
44
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
55
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
66
tests/cases/compiler/indexTypeCheck.ts(32,3): error TS1096: An index signature must have exactly one parameter.
7-
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string' or 'number'.
7+
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
88
tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression argument must be of type 'string', 'number', or 'any'.
99

1010

@@ -58,7 +58,7 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression
5858
interface Magenta {
5959
[p:Purple]; // error
6060
~
61-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
61+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
6262
}
6363

6464
var yellow: Yellow;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts(2,9): error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
2+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts(2,9): error TS2304: Cannot find name 'Symbol'.
3+
4+
5+
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts (2 errors) ====
6+
interface I {
7+
[s: Symbol]: string;
8+
~~~~~~
9+
!!! error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
10+
~~~~~~
11+
!!! error TS2304: Cannot find name 'Symbol'.
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts(2,9): error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
2+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts(2,9): error TS2304: Cannot find name 'Symbol'.
3+
4+
5+
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts (2 errors) ====
6+
class C {
7+
[s: Symbol]: string;
8+
~~~~~~
9+
!!! error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
10+
~~~~~~
11+
!!! error TS2304: Cannot find name 'Symbol'.
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer3.ts(2,9): error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
2+
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer3.ts(2,9): error TS2304: Cannot find name 'Symbol'.
3+
4+
5+
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer3.ts (2 errors) ====
6+
var x: {
7+
[s: Symbol]: string;
8+
~~~~~~
9+
!!! error TS1188: 'Symbol' indexers are only available when targeting ECMAScript 6 and higher.
10+
~~~~~~
11+
!!! error TS2304: Cannot find name 'Symbol'.
12+
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature6.ts(2,4): error TS1023: An index signature parameter type must be 'string' or 'number'.
1+
tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature6.ts(2,4): error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
22

33

44
==== tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature6.ts (1 errors) ====
55
interface I {
66
[a:boolean]
77
~
8-
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
8+
!!! error TS1023: An index signature parameter type must be 'string', 'number', or 'Symbol'.
99
}

0 commit comments

Comments
 (0)