Skip to content

Commit

Permalink
fix(parser): throw error with wrong typescript 'export declare' (#12684)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jan 27, 2021
1 parent 9907bd8 commit fbfd1b2
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -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:
Expand Down Expand Up @@ -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)) {
Expand Down
@@ -0,0 +1 @@
export declare foo;
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'export declare' must be followed by an ambient declaration. (1:15)"
}
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": []
Expand Down
@@ -0,0 +1 @@
export declare declare var name;
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "'export declare' must be followed by an ambient declaration. (1:15)"
}

0 comments on commit fbfd1b2

Please sign in to comment.