From ad34665c8cfdeeab4b2ead77724dc3c21e7247a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 30 Jan 2023 12:19:27 +0100 Subject: [PATCH 1/4] [ts] Support `export type * from` --- .../typescript/export-type-star-from/input.ts | 1 + .../export-type-star-from/output.js | 1 + .../src/plugins/typescript/index.ts | 16 +++++------ .../export/export-type-star-from/input.ts | 1 + .../export/export-type-star-from/output.json | 27 +++++++++++++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts create mode 100644 packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts new file mode 100644 index 000000000000..1d5ff10b29a2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts @@ -0,0 +1 @@ +export type * from './mod'; diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js new file mode 100644 index 000000000000..06cdd297087c --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js @@ -0,0 +1 @@ +export type * from './mod'; \ No newline at end of file diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 5ef8e86129ad..1a920cbd0121 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -2716,14 +2716,14 @@ export default (superClass: ClassWithMixin) => this.semicolon(); return this.finishNode(decl, "TSNamespaceExportDeclaration"); } else { - if ( - this.isContextual(tt._type) && - this.lookahead().type === tt.braceL - ) { - this.next(); - node.exportKind = "type"; - } else { - node.exportKind = "value"; + node.exportKind = "value"; + + if (this.isContextual(tt._type)) { + const ch = this.lookaheadCharCode(); + if (ch === charCodes.leftCurlyBrace || ch === charCodes.asterisk) { + this.next(); + node.exportKind = "type"; + } } return super.parseExport( diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts new file mode 100644 index 000000000000..1d5ff10b29a2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts @@ -0,0 +1 @@ +export type * from './mod'; diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json new file mode 100644 index 000000000000..865dd87d6251 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json @@ -0,0 +1,27 @@ +{ + "type": "File", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "program": { + "type": "Program", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportAllDeclaration", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "exportKind": "type", + "source": { + "type": "StringLiteral", + "start":19,"end":26,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "./mod", + "raw": "'./mod'" + }, + "value": "./mod" + } + } + ], + "directives": [] + } +} From 99fc530c724125516a89cec6314b57a49e2d8c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 30 Jan 2023 12:21:55 +0100 Subject: [PATCH 2/4] Transform --- packages/babel-plugin-transform-typescript/src/index.ts | 4 ++++ .../test/fixtures/exports/export-type-star-from/input.ts | 2 ++ .../test/fixtures/exports/export-type-star-from/output.mjs | 1 + 3 files changed, 7 insertions(+) create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index b8e0b1b3dee9..1aa42d08e00f 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -444,6 +444,10 @@ export default declare((api, opts: Options) => { NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false); }, + ExportAllDeclaration(path) { + if (path.node.exportKind === "type") path.remove(); + }, + ExportSpecifier(path) { // remove type exports type Parent = t.ExportDeclaration & { source?: t.StringLiteral }; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts new file mode 100644 index 000000000000..c4f859c5bfba --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts @@ -0,0 +1,2 @@ +export type * from './mod'; +; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs new file mode 100644 index 000000000000..092bc2b04126 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs @@ -0,0 +1 @@ +; From d1b7a376507b786754066adb3e7d79a0cf04b921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 30 Jan 2023 17:47:54 +0100 Subject: [PATCH 3/4] Add `export type * as` tests --- .../typescript/export-type-star-from/input.ts | 4 + .../export-type-star-from/output.js | 6 +- .../export/export-type-star-from/input.ts | 4 + .../export/export-type-star-from/output.json | 96 ++++++++++++++++++- .../exports/export-type-star-from/input.ts | 5 + .../exports/export-type-star-from/output.mjs | 4 + 6 files changed, 115 insertions(+), 4 deletions(-) diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts index 1d5ff10b29a2..7089aaf314cb 100644 --- a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts @@ -1 +1,5 @@ export type * from './mod'; +export type * as ns from './mod'; +// Note: TSC doesn't support string module specifiers yet, +// but it's easier for us to support it that not. +export type * as "ns2" from './mod'; diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js index 06cdd297087c..9acd5219f513 100644 --- a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js @@ -1 +1,5 @@ -export type * from './mod'; \ No newline at end of file +export type * from './mod'; +export type * as ns from './mod'; +// Note: TSC doesn't support string module specifiers yet, +// but it's easier for us to support it that not. +export type * as "ns2" from './mod'; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts index 1d5ff10b29a2..7089aaf314cb 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts @@ -1 +1,5 @@ export type * from './mod'; +export type * as ns from './mod'; +// Note: TSC doesn't support string module specifiers yet, +// but it's easier for us to support it that not. +export type * as "ns2" from './mod'; diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json index 865dd87d6251..8728a7f58d03 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json @@ -1,9 +1,9 @@ { "type": "File", - "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "start":0,"end":207,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":207}}, "program": { "type": "Program", - "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "start":0,"end":207,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":207}}, "sourceType": "module", "interpreter": null, "body": [ @@ -20,8 +20,98 @@ }, "value": "./mod" } + }, + { + "type": "ExportNamedDeclaration", + "start":28,"end":61,"loc":{"start":{"line":2,"column":0,"index":28},"end":{"line":2,"column":33,"index":61}}, + "exportKind": "type", + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "start":40,"end":47,"loc":{"start":{"line":2,"column":12,"index":40},"end":{"line":2,"column":19,"index":47}}, + "exported": { + "type": "Identifier", + "start":45,"end":47,"loc":{"start":{"line":2,"column":17,"index":45},"end":{"line":2,"column":19,"index":47},"identifierName":"ns"}, + "name": "ns" + } + } + ], + "source": { + "type": "StringLiteral", + "start":53,"end":60,"loc":{"start":{"line":2,"column":25,"index":53},"end":{"line":2,"column":32,"index":60}}, + "extra": { + "rawValue": "./mod", + "raw": "'./mod'" + }, + "value": "./mod" + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " Note: TSC doesn't support string module specifiers yet,", + "start":62,"end":120,"loc":{"start":{"line":3,"column":0,"index":62},"end":{"line":3,"column":58,"index":120}} + }, + { + "type": "CommentLine", + "value": " but it's easier for us to support it that not.", + "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + } + ] + }, + { + "type": "ExportNamedDeclaration", + "start":171,"end":207,"loc":{"start":{"line":5,"column":0,"index":171},"end":{"line":5,"column":36,"index":207}}, + "exportKind": "type", + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "start":183,"end":193,"loc":{"start":{"line":5,"column":12,"index":183},"end":{"line":5,"column":22,"index":193}}, + "exported": { + "type": "StringLiteral", + "start":188,"end":193,"loc":{"start":{"line":5,"column":17,"index":188},"end":{"line":5,"column":22,"index":193}}, + "extra": { + "rawValue": "ns2", + "raw": "\"ns2\"" + }, + "value": "ns2" + } + } + ], + "source": { + "type": "StringLiteral", + "start":199,"end":206,"loc":{"start":{"line":5,"column":28,"index":199},"end":{"line":5,"column":35,"index":206}}, + "extra": { + "rawValue": "./mod", + "raw": "'./mod'" + }, + "value": "./mod" + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Note: TSC doesn't support string module specifiers yet,", + "start":62,"end":120,"loc":{"start":{"line":3,"column":0,"index":62},"end":{"line":3,"column":58,"index":120}} + }, + { + "type": "CommentLine", + "value": " but it's easier for us to support it that not.", + "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + } + ] } ], "directives": [] - } + }, + "comments": [ + { + "type": "CommentLine", + "value": " Note: TSC doesn't support string module specifiers yet,", + "start":62,"end":120,"loc":{"start":{"line":3,"column":0,"index":62},"end":{"line":3,"column":58,"index":120}} + }, + { + "type": "CommentLine", + "value": " but it's easier for us to support it that not.", + "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + } + ] } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts index c4f859c5bfba..4c8d59dd5e13 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts @@ -1,2 +1,7 @@ export type * from './mod'; +export type * as ns from './mod'; +// Note: TSC doesn't support string module specifiers yet, +// but it's easier for us to support it that not. +export type * as "ns2" from './mod'; + ; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs index 092bc2b04126..9e210346b27e 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs @@ -1 +1,5 @@ +// Note: TSC doesn't support string module specifiers yet, +// but it's easier for us to support it that not. + ; +export {}; From 602cc247a1ca8a69d01ba5486b373f6aeed4c52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 30 Jan 2023 18:45:45 +0100 Subject: [PATCH 4/4] typo --- .../typescript/export-type-star-from/input.ts | 2 +- .../export-type-star-from/output.js | 2 +- .../export/export-type-star-from/input.ts | 2 +- .../export/export-type-star-from/output.json | 24 +++++++++---------- .../exports/export-type-star-from/input.ts | 2 +- .../exports/export-type-star-from/output.mjs | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts index 7089aaf314cb..3d2ab4466447 100644 --- a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/input.ts @@ -1,5 +1,5 @@ export type * from './mod'; export type * as ns from './mod'; // Note: TSC doesn't support string module specifiers yet, -// but it's easier for us to support it that not. +// but it's easier for us to support them than not. export type * as "ns2" from './mod'; diff --git a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js index 9acd5219f513..0da6405b3160 100644 --- a/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js +++ b/packages/babel-generator/test/fixtures/typescript/export-type-star-from/output.js @@ -1,5 +1,5 @@ export type * from './mod'; export type * as ns from './mod'; // Note: TSC doesn't support string module specifiers yet, -// but it's easier for us to support it that not. +// but it's easier for us to support them than not. export type * as "ns2" from './mod'; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts index 7089aaf314cb..3d2ab4466447 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/input.ts @@ -1,5 +1,5 @@ export type * from './mod'; export type * as ns from './mod'; // Note: TSC doesn't support string module specifiers yet, -// but it's easier for us to support it that not. +// but it's easier for us to support them than not. export type * as "ns2" from './mod'; diff --git a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json index 8728a7f58d03..39370b77e60d 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json +++ b/packages/babel-parser/test/fixtures/typescript/export/export-type-star-from/output.json @@ -1,9 +1,9 @@ { "type": "File", - "start":0,"end":207,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":207}}, + "start":0,"end":209,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":209}}, "program": { "type": "Program", - "start":0,"end":207,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":207}}, + "start":0,"end":209,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":36,"index":209}}, "sourceType": "module", "interpreter": null, "body": [ @@ -53,22 +53,22 @@ }, { "type": "CommentLine", - "value": " but it's easier for us to support it that not.", - "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + "value": " but it's easier for us to support them than not.", + "start":121,"end":172,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":51,"index":172}} } ] }, { "type": "ExportNamedDeclaration", - "start":171,"end":207,"loc":{"start":{"line":5,"column":0,"index":171},"end":{"line":5,"column":36,"index":207}}, + "start":173,"end":209,"loc":{"start":{"line":5,"column":0,"index":173},"end":{"line":5,"column":36,"index":209}}, "exportKind": "type", "specifiers": [ { "type": "ExportNamespaceSpecifier", - "start":183,"end":193,"loc":{"start":{"line":5,"column":12,"index":183},"end":{"line":5,"column":22,"index":193}}, + "start":185,"end":195,"loc":{"start":{"line":5,"column":12,"index":185},"end":{"line":5,"column":22,"index":195}}, "exported": { "type": "StringLiteral", - "start":188,"end":193,"loc":{"start":{"line":5,"column":17,"index":188},"end":{"line":5,"column":22,"index":193}}, + "start":190,"end":195,"loc":{"start":{"line":5,"column":17,"index":190},"end":{"line":5,"column":22,"index":195}}, "extra": { "rawValue": "ns2", "raw": "\"ns2\"" @@ -79,7 +79,7 @@ ], "source": { "type": "StringLiteral", - "start":199,"end":206,"loc":{"start":{"line":5,"column":28,"index":199},"end":{"line":5,"column":35,"index":206}}, + "start":201,"end":208,"loc":{"start":{"line":5,"column":28,"index":201},"end":{"line":5,"column":35,"index":208}}, "extra": { "rawValue": "./mod", "raw": "'./mod'" @@ -94,8 +94,8 @@ }, { "type": "CommentLine", - "value": " but it's easier for us to support it that not.", - "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + "value": " but it's easier for us to support them than not.", + "start":121,"end":172,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":51,"index":172}} } ] } @@ -110,8 +110,8 @@ }, { "type": "CommentLine", - "value": " but it's easier for us to support it that not.", - "start":121,"end":170,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":49,"index":170}} + "value": " but it's easier for us to support them than not.", + "start":121,"end":172,"loc":{"start":{"line":4,"column":0,"index":121},"end":{"line":4,"column":51,"index":172}} } ] } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts index 4c8d59dd5e13..d5d6cfc6b825 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/input.ts @@ -1,7 +1,7 @@ export type * from './mod'; export type * as ns from './mod'; // Note: TSC doesn't support string module specifiers yet, -// but it's easier for us to support it that not. +// but it's easier for us to support them than not. export type * as "ns2" from './mod'; ; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs index 9e210346b27e..85ed612456c0 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs +++ b/packages/babel-plugin-transform-typescript/test/fixtures/exports/export-type-star-from/output.mjs @@ -1,5 +1,5 @@ // Note: TSC doesn't support string module specifiers yet, -// but it's easier for us to support it that not. +// but it's easier for us to support them than not. ; export {};