diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index bc24dfccd7..59a0befe1a 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -1535,6 +1535,17 @@ export default (superClass: Class): Class => cls.abstract = true; return cls; } + + // export default interface allowed in: + // https://github.com/Microsoft/TypeScript/pull/16040 + if (this.state.value === "interface") { + return this.tsParseDeclaration( + this.startNode(), + this.state.value, + true, + ); + } + return super.parseExportDefaultExpression(); } diff --git a/packages/babel-parser/test/fixtures/typescript/interface/export/input.js b/packages/babel-parser/test/fixtures/typescript/interface/export/input.js index 509789ea6f..7049da8742 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/export/input.js +++ b/packages/babel-parser/test/fixtures/typescript/interface/export/input.js @@ -1,2 +1,2 @@ export interface I {} -// `export default` does not work with interfaces +export default interface A {} diff --git a/packages/babel-parser/test/fixtures/typescript/interface/export/output.json b/packages/babel-parser/test/fixtures/typescript/interface/export/output.json index 5d74c76817..1ad5c8ab4d 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/export/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/export/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 71, + "end": 51, "loc": { "start": { "line": 1, @@ -9,13 +9,13 @@ }, "end": { "line": 2, - "column": 49 + "column": 29 } }, "program": { "type": "Program", "start": 0, - "end": 71, + "end": 51, "loc": { "start": { "line": 1, @@ -23,7 +23,7 @@ }, "end": { "line": 2, - "column": 49 + "column": 29 } }, "sourceType": "module", @@ -92,45 +92,72 @@ }, "body": [] } + } + }, + { + "type": "ExportDefaultDeclaration", + "start": 22, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 29 + } }, - "trailingComments": [ - { - "type": "CommentLine", - "value": " `export default` does not work with interfaces", - "start": 22, - "end": 71, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 37, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 47, + "end": 48, "loc": { "start": { "line": 2, - "column": 0 + "column": 25 }, "end": { "line": 2, - "column": 49 + "column": 26 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 49, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 29 } - } + }, + "body": [] } - ] + } } ], "directives": [] - }, - "comments": [ - { - "type": "CommentLine", - "value": " `export default` does not work with interfaces", - "start": 22, - "end": 71, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 49 - } - } - } - ] + } } \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/interface/input.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/exports/interface/input.mjs new file mode 100644 index 0000000000..7049da8742 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/interface/input.mjs @@ -0,0 +1,2 @@ +export interface I {} +export default interface A {}