Skip to content

Commit

Permalink
fix(13520): disallow default exporting interface without a name
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Jul 31, 2021
1 parent d3a7cd5 commit f837612
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ const TSErrors = makeErrorTemplates(
"'override' modifier cannot appear on an index signature.",
IndexSignatureHasStatic:
"Index signatures cannot have the 'static' modifier.",
InvalidInterfaceDeclarationInExportDefault:
"'export default interface' must be followed by an identifier.",
InvalidModifierOnTypeMember:
"'%0' modifier cannot appear on a type member.",
InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
Expand Down Expand Up @@ -2305,13 +2307,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// export default interface allowed in:
// https://github.com/Microsoft/TypeScript/pull/16040
if (this.state.value === "interface") {
const result = this.tsParseDeclaration(
this.startNode(),
this.state.value,
true,
);
const interfaceNode = this.startNode();
const prevValue = this.state.value;
this.next();

if (result) return result;
if (this.state.type === tt.name) {
const result = this.tsParseDeclaration(
interfaceNode,
prevValue,
false,
);
if (result) return result;
} else {
this.raise(
this.state.start,
TSErrors.InvalidInterfaceDeclarationInExportDefault,
);
}
}

return super.parseExportDefaultExpression();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default interface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "File",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"errors": [
"SyntaxError: 'export default interface' must be followed by an identifier. (1:25)"
],
"program": {
"type": "Program",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"exportKind": "value",
"declaration": {
"type": "ObjectExpression",
"start":25,"end":27,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":27}},
"properties": []
}
}
],
"directives": []
}
}

0 comments on commit f837612

Please sign in to comment.