Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve syntax error for class fields in ambient context (#12108)
* Improve error messages for ambient context class fields * Modify switching state.isDeclareContext for class fields with declare
- Loading branch information
1 parent
3d5da4c
commit 39a1267
Showing
14 changed files
with
237 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/class/declare-initializer/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare class C { | ||
field = "field"; | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/babel-parser/test/fixtures/typescript/class/declare-initializer/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: Initializers are not allowed in ambient contexts. (2:8)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ClassDeclaration", | ||
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"declare": true, | ||
"id": { | ||
"type": "Identifier", | ||
"start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"C"}, | ||
"name": "C" | ||
}, | ||
"superClass": null, | ||
"body": { | ||
"type": "ClassBody", | ||
"start":16,"end":38,"loc":{"start":{"line":1,"column":16},"end":{"line":3,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "ClassProperty", | ||
"start":20,"end":36,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":18}}, | ||
"static": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start":20,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7},"identifierName":"field"}, | ||
"name": "field" | ||
}, | ||
"computed": false, | ||
"value": { | ||
"type": "StringLiteral", | ||
"start":28,"end":35,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":17}}, | ||
"extra": { | ||
"rawValue": "field", | ||
"raw": "\"field\"" | ||
}, | ||
"value": "field" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-parser/test/fixtures/typescript/declare/module-class/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module m { | ||
class C { | ||
field = "field"; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/declare/module-class/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["typescript", "classProperties"] | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/babel-parser/test/fixtures/typescript/declare/module-class/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: Initializers are not allowed in ambient contexts. (3:10)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSModuleDeclaration", | ||
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"m"}, | ||
"name": "m" | ||
}, | ||
"body": { | ||
"type": "TSModuleBlock", | ||
"start":17,"end":57,"loc":{"start":{"line":1,"column":17},"end":{"line":5,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "ClassDeclaration", | ||
"start":21,"end":55,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":27,"end":28,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"C"}, | ||
"name": "C" | ||
}, | ||
"superClass": null, | ||
"body": { | ||
"type": "ClassBody", | ||
"start":29,"end":55,"loc":{"start":{"line":2,"column":10},"end":{"line":4,"column":3}}, | ||
"body": [ | ||
{ | ||
"type": "ClassProperty", | ||
"start":35,"end":51,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":20}}, | ||
"static": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start":35,"end":40,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":9},"identifierName":"field"}, | ||
"name": "field" | ||
}, | ||
"computed": false, | ||
"value": { | ||
"type": "StringLiteral", | ||
"start":43,"end":50,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":19}}, | ||
"extra": { | ||
"rawValue": "field", | ||
"raw": "\"field\"" | ||
}, | ||
"value": "field" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
"declare": true | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
packages/babel-parser/test/fixtures/typescript/declare/namespace-class/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare namespace m { | ||
class C { | ||
field = "field"; | ||
} | ||
} | ||
|
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/declare/namespace-class/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["typescript", "classProperties"] | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/babel-parser/test/fixtures/typescript/declare/namespace-class/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: Initializers are not allowed in ambient contexts. (3:10)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSModuleDeclaration", | ||
"start":0,"end":60,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19},"identifierName":"m"}, | ||
"name": "m" | ||
}, | ||
"body": { | ||
"type": "TSModuleBlock", | ||
"start":20,"end":60,"loc":{"start":{"line":1,"column":20},"end":{"line":5,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "ClassDeclaration", | ||
"start":24,"end":58,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":30,"end":31,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"C"}, | ||
"name": "C" | ||
}, | ||
"superClass": null, | ||
"body": { | ||
"type": "ClassBody", | ||
"start":32,"end":58,"loc":{"start":{"line":2,"column":10},"end":{"line":4,"column":3}}, | ||
"body": [ | ||
{ | ||
"type": "ClassProperty", | ||
"start":38,"end":54,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":20}}, | ||
"static": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start":38,"end":43,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":9},"identifierName":"field"}, | ||
"name": "field" | ||
}, | ||
"computed": false, | ||
"value": { | ||
"type": "StringLiteral", | ||
"start":46,"end":53,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":19}}, | ||
"extra": { | ||
"rawValue": "field", | ||
"raw": "\"field\"" | ||
}, | ||
"value": "field" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
}, | ||
"declare": true | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
File renamed without changes.
File renamed without changes.