Skip to content

Commit

Permalink
fix(ts): raise error for export default interface {} (#13622)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Aug 2, 2021
1 parent 86fae72 commit b3ab476
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
29 changes: 17 additions & 12 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const TSErrors = makeErrorTemplates(
InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
InvalidTupleMemberLabel:
"Tuple members must be labeled with a simple identifier.",
MissingInterfaceName:
"'interface' declarations must be followed by an identifier.",
MixedLabeledAndUnlabeledElements:
"Tuple members must all have names or all not have names.",
NonAbstractClassHasAbstractMethod:
Expand Down Expand Up @@ -1419,12 +1421,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tsParseInterfaceDeclaration(
node: N.TsInterfaceDeclaration,
): N.TsInterfaceDeclaration {
node.id = this.parseIdentifier();
this.checkLVal(
node.id,
"typescript interface declaration",
BIND_TS_INTERFACE,
);
if (this.match(tt.name)) {
node.id = this.parseIdentifier();
this.checkLVal(
node.id,
"typescript interface declaration",
BIND_TS_INTERFACE,
);
} else {
node.id = null;
this.raise(this.state.start, TSErrors.MissingInterfaceName);
}

node.typeParameters = this.tsTryParseTypeParameters();
if (this.eat(tt._extends)) {
node.extends = this.tsParseHeritageClause("extends");
Expand Down Expand Up @@ -2305,12 +2313,9 @@ 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();
this.next();
const result = this.tsParseInterfaceDeclaration(interfaceNode);
if (result) return result;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ export type TsImportType = TsTypeBase & {

export type TsInterfaceDeclaration = DeclarationBase & {
type: "TSInterfaceDeclaration",
id: Identifier,
id: ?Identifier,
typeParameters: ?TsTypeParameterDeclaration,
// TS uses "heritageClauses", but want this to resemble ClassBase.
extends?: $ReadOnlyArray<TsExpressionWithTypeArguments>,
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,31 @@
{
"type": "File",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"errors": [
"SyntaxError: 'interface' declarations 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": "TSInterfaceDeclaration",
"start":15,"end":27,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":27}},
"id": null,
"body": {
"type": "TSInterfaceBody",
"start":25,"end":27,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":27}},
"body": []
}
}
}
],
"directives": []
}
}

0 comments on commit b3ab476

Please sign in to comment.