From adbc6519d661e33a1089e9d9ec25b3c8e3d3bc98 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 25 Feb 2022 16:14:45 +0530 Subject: [PATCH 1/2] Fix: no empty with braces with zero specifiers --- packages/babel-generator/src/generators/modules.ts | 13 +++++++++++++ .../test/fixtures/flow/import-typeof/input.ts | 1 + .../test/fixtures/flow/import-typeof/output.js | 1 + .../typescript/import-type-empty-object/input.ts | 2 ++ .../typescript/import-type-empty-object/output.js | 2 ++ 5 files changed, 19 insertions(+) create mode 100644 packages/babel-generator/test/fixtures/flow/import-typeof/input.ts create mode 100644 packages/babel-generator/test/fixtures/flow/import-typeof/output.js create mode 100644 packages/babel-generator/test/fixtures/typescript/import-type-empty-object/input.ts create mode 100644 packages/babel-generator/test/fixtures/typescript/import-type-empty-object/output.js diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 16cd4bd08aef..61b4eb31e877 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -183,6 +183,7 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { } const specifiers = node.specifiers.slice(0); + const specifiersLength = specifiers.length; if (specifiers?.length) { // print "special" specifiers first for (;;) { @@ -209,6 +210,18 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { this.token("}"); } + if (node.importKind !== "type" && node.importKind !== "typeof") { + this.space(); + this.word("from"); + this.space(); + } + } + + if (node.importKind === "type" || node.importKind === "typeof") { + if (!specifiersLength) { + this.token("{"); + this.token("}"); + } this.space(); this.word("from"); this.space(); diff --git a/packages/babel-generator/test/fixtures/flow/import-typeof/input.ts b/packages/babel-generator/test/fixtures/flow/import-typeof/input.ts new file mode 100644 index 000000000000..415854f50adc --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/import-typeof/input.ts @@ -0,0 +1 @@ +import typeof U from "x" diff --git a/packages/babel-generator/test/fixtures/flow/import-typeof/output.js b/packages/babel-generator/test/fixtures/flow/import-typeof/output.js new file mode 100644 index 000000000000..719b06a9e072 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/import-typeof/output.js @@ -0,0 +1 @@ +import typeof U from "x"; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/input.ts b/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/input.ts new file mode 100644 index 000000000000..9c817dfb276d --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/input.ts @@ -0,0 +1,2 @@ +import type {} from 'some-module'; +export type {} from "some-module" \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/output.js b/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/output.js new file mode 100644 index 000000000000..f8469f614bdd --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/import-type-empty-object/output.js @@ -0,0 +1,2 @@ +import type {} from 'some-module'; +export type {} from "some-module"; \ No newline at end of file From 4fd02b846950a6b8705767b44f4670d35e0f3fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 27 Feb 2022 02:04:17 +0100 Subject: [PATCH 2/2] Deduplicate some code --- .../babel-generator/src/generators/modules.ts | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 61b4eb31e877..f71745f73b51 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -177,51 +177,41 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { this.word("import"); this.space(); - if (node.importKind === "type" || node.importKind === "typeof") { + const isTypeKind = node.importKind === "type" || node.importKind === "typeof"; + if (isTypeKind) { this.word(node.importKind); this.space(); } const specifiers = node.specifiers.slice(0); - const specifiersLength = specifiers.length; - if (specifiers?.length) { - // print "special" specifiers first - for (;;) { - const first = specifiers[0]; - if ( - isImportDefaultSpecifier(first) || - isImportNamespaceSpecifier(first) - ) { - this.print(specifiers.shift(), node); - if (specifiers.length) { - this.token(","); - this.space(); - } - } else { - break; + const hasSpecifiers = !!specifiers.length; + // print "special" specifiers first. The loop condition is constant, + // but there is a "break" in the body. + while (hasSpecifiers) { + const first = specifiers[0]; + if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) { + this.print(specifiers.shift(), node); + if (specifiers.length) { + this.token(","); + this.space(); } + } else { + break; } + } - if (specifiers.length) { - this.token("{"); - this.space(); - this.printList(specifiers, node); - this.space(); - this.token("}"); - } - - if (node.importKind !== "type" && node.importKind !== "typeof") { - this.space(); - this.word("from"); - this.space(); - } + if (specifiers.length) { + this.token("{"); + this.space(); + this.printList(specifiers, node); + this.space(); + this.token("}"); + } else if (isTypeKind && !hasSpecifiers) { + this.token("{"); + this.token("}"); } - if (node.importKind === "type" || node.importKind === "typeof") { - if (!specifiersLength) { - this.token("{"); - this.token("}"); - } + if (hasSpecifiers || isTypeKind) { this.space(); this.word("from"); this.space();