From d9b4e196f56c7381dfa4fb22138dd7b678a3aa7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 26 Feb 2020 01:27:21 +0100 Subject: [PATCH] Add "exportKind: type" when needed with TS (#11157) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add "exportKind: type" to TS type export declratation Co-authored-by: Henry Zhu Co-authored-by: Huáng Jùnliàng Co-authored-by: Siddhant N Trivedi * Add test to @babel/generator Co-authored-by: Henry Zhu Co-authored-by: Huáng Jùnliàng Co-authored-by: Siddhant N Trivedi --- .../export-declaration-type/input.js | 2 + .../export-declaration-type/output.js | 2 + .../src/plugins/typescript/index.js | 8 + .../export-with-ts/output.json | 6 +- .../enum/export-declare-const/output.json | 2 +- .../typescript/export/declare/output.json | 16 +- .../export/export-type-declaration/input.ts | 4 + .../export-type-declaration/output.json | 370 ++++++++++++++++++ .../export/export-value-declaration/input.ts | 6 + .../export-value-declaration/output.json | 358 +++++++++++++++++ .../typescript/interface/export/output.json | 2 +- .../regression/is-default-export/output.json | 4 +- .../typescript/type-alias/export/output.json | 2 +- 13 files changed, 766 insertions(+), 16 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/export-declaration-type/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/export-declaration-type/output.js create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/output.json diff --git a/packages/babel-generator/test/fixtures/typescript/export-declaration-type/input.js b/packages/babel-generator/test/fixtures/typescript/export-declaration-type/input.js new file mode 100644 index 000000000000..1d10049665df --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/export-declaration-type/input.js @@ -0,0 +1,2 @@ +export interface A {} +export type B = string; diff --git a/packages/babel-generator/test/fixtures/typescript/export-declaration-type/output.js b/packages/babel-generator/test/fixtures/typescript/export-declaration-type/output.js new file mode 100644 index 000000000000..1d10049665df --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/export-declaration-type/output.js @@ -0,0 +1,2 @@ +export interface A {} +export type B = string; diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index f64fd6ca4c35..fba8a21cf4c1 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2134,6 +2134,14 @@ export default (superClass: Class): Class => if (!declaration) { declaration = super.parseExportDeclaration(node); } + if ( + declaration && + (declaration.type === "TSInterfaceDeclaration" || + declaration.type === "TSTypeAliasDeclaration" || + isDeclare) + ) { + node.exportKind = "type"; + } if (declaration && isDeclare) { // Reset location to include `declare` in range diff --git a/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json b/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json index efe8b515d611..740426fadfca 100644 --- a/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json +++ b/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json @@ -111,7 +111,7 @@ "column": 29 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -229,7 +229,7 @@ "column": 21 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -427,7 +427,7 @@ "column": 27 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { diff --git a/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json b/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json index 642d7f4d29c5..1e2557b5aa66 100644 --- a/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json +++ b/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json @@ -43,7 +43,7 @@ "column": 30 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { diff --git a/packages/babel-parser/test/fixtures/typescript/export/declare/output.json b/packages/babel-parser/test/fixtures/typescript/export/declare/output.json index 9112e4c162a5..43acb3b77b28 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/declare/output.json +++ b/packages/babel-parser/test/fixtures/typescript/export/declare/output.json @@ -43,7 +43,7 @@ "column": 31 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -143,7 +143,7 @@ "column": 34 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -227,7 +227,7 @@ "column": 25 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -295,7 +295,7 @@ "column": 29 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -362,7 +362,7 @@ "column": 31 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -428,7 +428,7 @@ "column": 26 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -495,7 +495,7 @@ "column": 29 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -562,7 +562,7 @@ "column": 26 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/input.ts b/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/input.ts new file mode 100644 index 000000000000..b8f23bbe7dd4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/input.ts @@ -0,0 +1,4 @@ +export type A = 2; +export interface B {} +export declare function a(): string; +export declare var b: string; diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/output.json b/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/output.json new file mode 100644 index 000000000000..1b6039be1525 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-declaration/output.json @@ -0,0 +1,370 @@ +{ + "type": "File", + "start": 0, + "end": 107, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 107, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "exportKind": "type", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 7, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "literal": { + "type": "NumericLiteral", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 19, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "exportKind": "type", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 26, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSInterfaceBody", + "start": 38, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 41, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "exportKind": "type", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSDeclareFunction", + "start": 48, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "start": 65, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 25 + }, + "identifierName": "a" + }, + "name": "a" + }, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 68, + "end": 76, + "loc": { + "start": { + "line": 3, + "column": 27 + }, + "end": { + "line": 3, + "column": 35 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 70, + "end": 76, + "loc": { + "start": { + "line": 3, + "column": 29 + }, + "end": { + "line": 3, + "column": 35 + } + } + } + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 78, + "end": 107, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "exportKind": "type", + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 85, + "end": 107, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 97, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "id": { + "type": "Identifier", + "start": 97, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 28 + }, + "identifierName": "b" + }, + "name": "b", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 98, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 100, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 22 + }, + "end": { + "line": 4, + "column": 28 + } + } + } + } + }, + "init": null + } + ], + "kind": "var", + "declare": true + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/input.ts b/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/input.ts new file mode 100644 index 000000000000..a6aa9c5f6372 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/input.ts @@ -0,0 +1,6 @@ +export var a; +export function b() {} +export class C {} + +export enum D {} +export namespace E {} diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/output.json b/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/output.json new file mode 100644 index 000000000000..9d7eae1f64e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-value-declaration/output.json @@ -0,0 +1,358 @@ +{ + "type": "File", + "start": 0, + "end": 94, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 94, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "a" + }, + "name": "a" + }, + "init": null + } + ], + "kind": "var" + } + }, + { + "type": "ExportNamedDeclaration", + "start": 14, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start": 21, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "b" + }, + "name": "b" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 34, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "body": [], + "directives": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 37, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 44, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 50, + "end": 51, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 14 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 52, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "body": [] + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 56, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 63, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 13 + }, + "identifierName": "D" + }, + "name": "D" + }, + "members": [] + } + }, + { + "type": "ExportNamedDeclaration", + "start": 73, + "end": 94, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "exportKind": "value", + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 80, + "end": 94, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 90, + "end": 91, + "loc": { + "start": { + "line": 6, + "column": 17 + }, + "end": { + "line": 6, + "column": 18 + }, + "identifierName": "E" + }, + "name": "E" + }, + "body": { + "type": "TSModuleBlock", + "start": 92, + "end": 94, + "loc": { + "start": { + "line": 6, + "column": 19 + }, + "end": { + "line": 6, + "column": 21 + } + }, + "body": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file 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 5a5a83277c72..f7fe8f0c03eb 100644 --- a/packages/babel-parser/test/fixtures/typescript/interface/export/output.json +++ b/packages/babel-parser/test/fixtures/typescript/interface/export/output.json @@ -43,7 +43,7 @@ "column": 21 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { diff --git a/packages/babel-parser/test/fixtures/typescript/regression/is-default-export/output.json b/packages/babel-parser/test/fixtures/typescript/regression/is-default-export/output.json index fa086ca6810d..c2d937d1bd58 100644 --- a/packages/babel-parser/test/fixtures/typescript/regression/is-default-export/output.json +++ b/packages/babel-parser/test/fixtures/typescript/regression/is-default-export/output.json @@ -43,7 +43,7 @@ "column": 23 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { @@ -159,7 +159,7 @@ "column": 21 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": { diff --git a/packages/babel-parser/test/fixtures/typescript/type-alias/export/output.json b/packages/babel-parser/test/fixtures/typescript/type-alias/export/output.json index edaea4e5fd66..7f9c1f9fd3b7 100644 --- a/packages/babel-parser/test/fixtures/typescript/type-alias/export/output.json +++ b/packages/babel-parser/test/fixtures/typescript/type-alias/export/output.json @@ -43,7 +43,7 @@ "column": 23 } }, - "exportKind": "value", + "exportKind": "type", "specifiers": [], "source": null, "declaration": {