Skip to content

Commit 5076c90

Browse files
committed
Throw a syntax error for index signature with declare modifier
1 parent af612db commit 5076c90

3 files changed

Lines changed: 55 additions & 20 deletions

File tree

packages/babel-parser/src/plugins/typescript/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const TSErrors = Object.freeze({
8080
IndexSignatureHasAccessibility:
8181
"Index signatures cannot have an accessibility modifier ('%0')",
8282
IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier",
83+
IndexSignatureHasDeclare:
84+
"Index signatures cannot have the 'declare' modifier",
8385
InvalidTupleMemberLabel:
8486
"Tuple members must be labeled with a simple identifier.",
8587
MixedLabeledAndUnlabeledElements:
@@ -2108,6 +2110,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
21082110
(member: any).accessibility,
21092111
);
21102112
}
2113+
if ((member: any).declare) {
2114+
this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
2115+
}
21112116

21122117
return;
21132118
}

packages/babel-parser/test/fixtures/typescript/class/modifiers-index-signatures/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class C {
22
abstract [key: string]: string;
33
static [key: string]: string;
4+
declare [key: string]: string;
45
private [key: string]: string;
56
public [key: string]: string;
67
protected [key: string]: string;

packages/babel-parser/test/fixtures/typescript/class/modifiers-index-signatures/output.json

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
{
22
"type": "File",
3-
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
3+
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
44
"errors": [
55
"SyntaxError: Index signatures cannot have the 'abstract' modifier (2:2)",
66
"SyntaxError: Index signatures cannot have the 'static' modifier (3:2)",
7-
"SyntaxError: Index signatures cannot have an accessibility modifier ('private') (4:2)",
8-
"SyntaxError: Index signatures cannot have an accessibility modifier ('public') (5:2)",
9-
"SyntaxError: Index signatures cannot have an accessibility modifier ('protected') (6:2)"
7+
"SyntaxError: Index signatures cannot have the 'declare' modifier (4:2)",
8+
"SyntaxError: Index signatures cannot have an accessibility modifier ('private') (5:2)",
9+
"SyntaxError: Index signatures cannot have an accessibility modifier ('public') (6:2)",
10+
"SyntaxError: Index signatures cannot have an accessibility modifier ('protected') (7:2)"
1011
],
1112
"program": {
1213
"type": "Program",
13-
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
14+
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
1415
"sourceType": "module",
1516
"interpreter": null,
1617
"body": [
1718
{
1819
"type": "ClassDeclaration",
19-
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
20+
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
2021
"id": {
2122
"type": "Identifier",
2223
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
@@ -25,7 +26,7 @@
2526
"superClass": null,
2627
"body": {
2728
"type": "ClassBody",
28-
"start":8,"end":177,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"column":1}},
29+
"start":8,"end":210,"loc":{"start":{"line":1,"column":8},"end":{"line":8,"column":1}},
2930
"body": [
3031
{
3132
"type": "TSIndexSignature",
@@ -85,7 +86,7 @@
8586
{
8687
"type": "TSIndexSignature",
8788
"start":78,"end":108,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":32}},
88-
"accessibility": "private",
89+
"declare": true,
8990
"parameters": [
9091
{
9192
"type": "Identifier",
@@ -112,57 +113,85 @@
112113
},
113114
{
114115
"type": "TSIndexSignature",
115-
"start":111,"end":140,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":31}},
116+
"start":111,"end":141,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":32}},
117+
"accessibility": "private",
118+
"parameters": [
119+
{
120+
"type": "Identifier",
121+
"start":120,"end":131,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":22},"identifierName":"key"},
122+
"name": "key",
123+
"typeAnnotation": {
124+
"type": "TSTypeAnnotation",
125+
"start":123,"end":131,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":22}},
126+
"typeAnnotation": {
127+
"type": "TSStringKeyword",
128+
"start":125,"end":131,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":22}}
129+
}
130+
}
131+
}
132+
],
133+
"typeAnnotation": {
134+
"type": "TSTypeAnnotation",
135+
"start":132,"end":140,"loc":{"start":{"line":5,"column":23},"end":{"line":5,"column":31}},
136+
"typeAnnotation": {
137+
"type": "TSStringKeyword",
138+
"start":134,"end":140,"loc":{"start":{"line":5,"column":25},"end":{"line":5,"column":31}}
139+
}
140+
}
141+
},
142+
{
143+
"type": "TSIndexSignature",
144+
"start":144,"end":173,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":31}},
116145
"accessibility": "public",
117146
"parameters": [
118147
{
119148
"type": "Identifier",
120-
"start":119,"end":130,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":21},"identifierName":"key"},
149+
"start":152,"end":163,"loc":{"start":{"line":6,"column":10},"end":{"line":6,"column":21},"identifierName":"key"},
121150
"name": "key",
122151
"typeAnnotation": {
123152
"type": "TSTypeAnnotation",
124-
"start":122,"end":130,"loc":{"start":{"line":5,"column":13},"end":{"line":5,"column":21}},
153+
"start":155,"end":163,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":21}},
125154
"typeAnnotation": {
126155
"type": "TSStringKeyword",
127-
"start":124,"end":130,"loc":{"start":{"line":5,"column":15},"end":{"line":5,"column":21}}
156+
"start":157,"end":163,"loc":{"start":{"line":6,"column":15},"end":{"line":6,"column":21}}
128157
}
129158
}
130159
}
131160
],
132161
"typeAnnotation": {
133162
"type": "TSTypeAnnotation",
134-
"start":131,"end":139,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":30}},
163+
"start":164,"end":172,"loc":{"start":{"line":6,"column":22},"end":{"line":6,"column":30}},
135164
"typeAnnotation": {
136165
"type": "TSStringKeyword",
137-
"start":133,"end":139,"loc":{"start":{"line":5,"column":24},"end":{"line":5,"column":30}}
166+
"start":166,"end":172,"loc":{"start":{"line":6,"column":24},"end":{"line":6,"column":30}}
138167
}
139168
}
140169
},
141170
{
142171
"type": "TSIndexSignature",
143-
"start":143,"end":175,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":34}},
172+
"start":176,"end":208,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":34}},
144173
"accessibility": "protected",
145174
"parameters": [
146175
{
147176
"type": "Identifier",
148-
"start":154,"end":165,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":24},"identifierName":"key"},
177+
"start":187,"end":198,"loc":{"start":{"line":7,"column":13},"end":{"line":7,"column":24},"identifierName":"key"},
149178
"name": "key",
150179
"typeAnnotation": {
151180
"type": "TSTypeAnnotation",
152-
"start":157,"end":165,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":24}},
181+
"start":190,"end":198,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":24}},
153182
"typeAnnotation": {
154183
"type": "TSStringKeyword",
155-
"start":159,"end":165,"loc":{"start":{"line":6,"column":18},"end":{"line":6,"column":24}}
184+
"start":192,"end":198,"loc":{"start":{"line":7,"column":18},"end":{"line":7,"column":24}}
156185
}
157186
}
158187
}
159188
],
160189
"typeAnnotation": {
161190
"type": "TSTypeAnnotation",
162-
"start":166,"end":174,"loc":{"start":{"line":6,"column":25},"end":{"line":6,"column":33}},
191+
"start":199,"end":207,"loc":{"start":{"line":7,"column":25},"end":{"line":7,"column":33}},
163192
"typeAnnotation": {
164193
"type": "TSStringKeyword",
165-
"start":168,"end":174,"loc":{"start":{"line":6,"column":27},"end":{"line":6,"column":33}}
194+
"start":201,"end":207,"loc":{"start":{"line":7,"column":27},"end":{"line":7,"column":33}}
166195
}
167196
}
168197
}

0 commit comments

Comments
 (0)