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

fix(parser): throw error with wrong typescript 'export declare' #12684

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2281,6 +2281,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// "export declare" is equivalent to just "export".
const isDeclare = this.eatContextual("declare");

if (
this.isContextual("declare") ||
!this.shouldParseExportDeclaration()
fedeci marked this conversation as resolved.
Show resolved Hide resolved
) {
super.unexpected(this.state.start, this.state.value);
fedeci marked this conversation as resolved.
Show resolved Hide resolved
}

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": "foo (1:15)"
fedeci marked this conversation as resolved.
Show resolved Hide resolved
}
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": "declare (1:15)"
}