Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts] Throw a syntax error for index signature with declare #12111

Merged
merged 1 commit into from Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -80,6 +80,8 @@ const TSErrors = Object.freeze({
IndexSignatureHasAccessibility:
"Index signatures cannot have an accessibility modifier ('%0')",
IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier",
IndexSignatureHasDeclare:
"Index signatures cannot have the 'declare' modifier",
InvalidTupleMemberLabel:
"Tuple members must be labeled with a simple identifier.",
MixedLabeledAndUnlabeledElements:
Expand Down Expand Up @@ -2108,6 +2110,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
(member: any).accessibility,
);
}
if ((member: any).declare) {
this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
}

return;
}
Expand Down
@@ -1,6 +1,7 @@
class C {
abstract [key: string]: string;
static [key: string]: string;
declare [key: string]: string;
private [key: string]: string;
public [key: string]: string;
protected [key: string]: string;
Expand Down
@@ -1,22 +1,23 @@
{
"type": "File",
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
"errors": [
"SyntaxError: Index signatures cannot have the 'abstract' modifier (2:2)",
"SyntaxError: Index signatures cannot have the 'static' modifier (3:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('private') (4:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('public') (5:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('protected') (6:2)"
"SyntaxError: Index signatures cannot have the 'declare' modifier (4:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('private') (5:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('public') (6:2)",
"SyntaxError: Index signatures cannot have an accessibility modifier ('protected') (7:2)"
],
"program": {
"type": "Program",
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":177,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"start":0,"end":210,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
Expand All @@ -25,7 +26,7 @@
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":177,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"column":1}},
"start":8,"end":210,"loc":{"start":{"line":1,"column":8},"end":{"line":8,"column":1}},
"body": [
{
"type": "TSIndexSignature",
Expand Down Expand Up @@ -85,7 +86,7 @@
{
"type": "TSIndexSignature",
"start":78,"end":108,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":32}},
"accessibility": "private",
"declare": true,
"parameters": [
{
"type": "Identifier",
Expand All @@ -112,57 +113,85 @@
},
{
"type": "TSIndexSignature",
"start":111,"end":140,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":31}},
"start":111,"end":141,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":32}},
"accessibility": "private",
"parameters": [
{
"type": "Identifier",
"start":120,"end":131,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":22},"identifierName":"key"},
"name": "key",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":123,"end":131,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":22}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":125,"end":131,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":22}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":132,"end":140,"loc":{"start":{"line":5,"column":23},"end":{"line":5,"column":31}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":134,"end":140,"loc":{"start":{"line":5,"column":25},"end":{"line":5,"column":31}}
}
}
},
{
"type": "TSIndexSignature",
"start":144,"end":173,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":31}},
"accessibility": "public",
"parameters": [
{
"type": "Identifier",
"start":119,"end":130,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":21},"identifierName":"key"},
"start":152,"end":163,"loc":{"start":{"line":6,"column":10},"end":{"line":6,"column":21},"identifierName":"key"},
"name": "key",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":122,"end":130,"loc":{"start":{"line":5,"column":13},"end":{"line":5,"column":21}},
"start":155,"end":163,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":21}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":124,"end":130,"loc":{"start":{"line":5,"column":15},"end":{"line":5,"column":21}}
"start":157,"end":163,"loc":{"start":{"line":6,"column":15},"end":{"line":6,"column":21}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":131,"end":139,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":30}},
"start":164,"end":172,"loc":{"start":{"line":6,"column":22},"end":{"line":6,"column":30}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":133,"end":139,"loc":{"start":{"line":5,"column":24},"end":{"line":5,"column":30}}
"start":166,"end":172,"loc":{"start":{"line":6,"column":24},"end":{"line":6,"column":30}}
}
}
},
{
"type": "TSIndexSignature",
"start":143,"end":175,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":34}},
"start":176,"end":208,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":34}},
"accessibility": "protected",
"parameters": [
{
"type": "Identifier",
"start":154,"end":165,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":24},"identifierName":"key"},
"start":187,"end":198,"loc":{"start":{"line":7,"column":13},"end":{"line":7,"column":24},"identifierName":"key"},
"name": "key",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":157,"end":165,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":24}},
"start":190,"end":198,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":24}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":159,"end":165,"loc":{"start":{"line":6,"column":18},"end":{"line":6,"column":24}}
"start":192,"end":198,"loc":{"start":{"line":7,"column":18},"end":{"line":7,"column":24}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":166,"end":174,"loc":{"start":{"line":6,"column":25},"end":{"line":6,"column":33}},
"start":199,"end":207,"loc":{"start":{"line":7,"column":25},"end":{"line":7,"column":33}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":168,"end":174,"loc":{"start":{"line":6,"column":27},"end":{"line":6,"column":33}}
"start":201,"end":207,"loc":{"start":{"line":7,"column":27},"end":{"line":7,"column":33}}
}
}
}
Expand Down