fix(parser): throw error with wrong typescript 'export declare' (#12684)
This commit is contained in:
parent
9907bd86c9
commit
fbfd1b2aa6
@ -74,6 +74,8 @@ const TSErrors = Object.freeze({
|
||||
EmptyHeritageClauseType: "'%0' list cannot be empty.",
|
||||
EmptyTypeArguments: "Type argument list cannot be empty.",
|
||||
EmptyTypeParameters: "Type parameter list cannot be empty.",
|
||||
ExpectedAmbientAfterExportDeclare:
|
||||
"'export declare' must be followed by an ambient declaration.",
|
||||
IndexSignatureHasAbstract:
|
||||
"Index signatures cannot have the 'abstract' modifier",
|
||||
IndexSignatureHasAccessibility:
|
||||
@ -2281,6 +2283,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
// "export declare" is equivalent to just "export".
|
||||
const isDeclare = this.eatContextual("declare");
|
||||
|
||||
if (
|
||||
isDeclare &&
|
||||
(this.isContextual("declare") || !this.shouldParseExportDeclaration())
|
||||
) {
|
||||
throw this.raise(
|
||||
this.state.start,
|
||||
TSErrors.ExpectedAmbientAfterExportDeclare,
|
||||
);
|
||||
}
|
||||
|
||||
let declaration: ?N.Declaration;
|
||||
|
||||
if (this.match(tt.name)) {
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/export/declare-invalid/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/export/declare-invalid/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export declare foo;
|
||||
7
packages/babel-parser/test/fixtures/typescript/export/declare-invalid/options.json
vendored
Normal file
7
packages/babel-parser/test/fixtures/typescript/export/declare-invalid/options.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"typescript"
|
||||
],
|
||||
"throws": "'export declare' must be followed by an ambient declaration. (1:15)"
|
||||
}
|
||||
@ -6,3 +6,5 @@ export declare type T = number;
|
||||
export declare module M {}
|
||||
export declare namespace N {}
|
||||
export declare enum foo {}
|
||||
export declare var bar: string;
|
||||
export declare var _bar;
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":238,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":26}},
|
||||
"start":0,"end":295,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":24}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":238,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":26}},
|
||||
"start":0,"end":295,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":24}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
@ -195,6 +195,64 @@
|
||||
"members": [],
|
||||
"declare": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ExportNamedDeclaration",
|
||||
"start":239,"end":270,"loc":{"start":{"line":9,"column":0},"end":{"line":9,"column":31}},
|
||||
"exportKind": "type",
|
||||
"specifiers": [],
|
||||
"source": null,
|
||||
"declaration": {
|
||||
"type": "VariableDeclaration",
|
||||
"start":246,"end":270,"loc":{"start":{"line":9,"column":7},"end":{"line":9,"column":31}},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start":258,"end":269,"loc":{"start":{"line":9,"column":19},"end":{"line":9,"column":30}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":258,"end":269,"loc":{"start":{"line":9,"column":19},"end":{"line":9,"column":30},"identifierName":"bar"},
|
||||
"name": "bar",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":261,"end":269,"loc":{"start":{"line":9,"column":22},"end":{"line":9,"column":30}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSStringKeyword",
|
||||
"start":263,"end":269,"loc":{"start":{"line":9,"column":24},"end":{"line":9,"column":30}}
|
||||
}
|
||||
}
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "var",
|
||||
"declare": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ExportNamedDeclaration",
|
||||
"start":271,"end":295,"loc":{"start":{"line":10,"column":0},"end":{"line":10,"column":24}},
|
||||
"exportKind": "type",
|
||||
"specifiers": [],
|
||||
"source": null,
|
||||
"declaration": {
|
||||
"type": "VariableDeclaration",
|
||||
"start":278,"end":295,"loc":{"start":{"line":10,"column":7},"end":{"line":10,"column":24}},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start":290,"end":294,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":23}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":290,"end":294,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":23},"identifierName":"_bar"},
|
||||
"name": "_bar"
|
||||
},
|
||||
"init": null
|
||||
}
|
||||
],
|
||||
"kind": "var",
|
||||
"declare": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/export/double-declare/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/export/double-declare/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export declare declare var name;
|
||||
7
packages/babel-parser/test/fixtures/typescript/export/double-declare/options.json
vendored
Normal file
7
packages/babel-parser/test/fixtures/typescript/export/double-declare/options.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"typescript"
|
||||
],
|
||||
"throws": "'export declare' must be followed by an ambient declaration. (1:15)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user