From 55ca90b3fce0980df0f059310210cb6acb2fa035 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Tue, 31 Jul 2018 12:57:57 -0500 Subject: [PATCH] Allow TSInterfaceDeclaration to be default export (#8408) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit | Q                       | A | ------------------------ | --- | Fixed Issues? | Fixes #7118 | Patch: Bug Fix? | Y | Major: Breaking Change? | N | Minor: New Feature? | N | Tests Added + Pass? | Yes | Documentation PR Link | | Any Dependency Changes? | | License | MIT --- .../babel-parser/src/plugins/typescript.js | 11 +++ .../typescript/interface/export/input.js | 2 +- .../typescript/interface/export/output.json | 93 ++++++++++++------- .../test/fixtures/exports/interface/input.mjs | 2 + 4 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/exports/interface/input.mjs 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 {}