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 3 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
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 (
this.isContextual("declare") ||
!this.shouldParseExportDeclaration()
fedeci marked this conversation as resolved.
Show resolved Hide resolved
) {
throw this.raise(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not throw here to make it recoverable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about how we could recover. What should the AST for export declare foo be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. Let's stick with throwing!

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)"
}