diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index 16cd4bd08aef..f71745f73b51 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -177,38 +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); - 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 (specifiers.length) { + this.token("{"); + this.space(); + this.printList(specifiers, node); + this.space(); + this.token("}"); + } else if (isTypeKind && !hasSpecifiers) { + this.token("{"); + this.token("}"); + } + if (hasSpecifiers || isTypeKind) { 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