Skip to content

Commit

Permalink
Fix import type/typeof printing with no specifiers (#14309)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
The-x-Theorist and nicolo-ribaudo committed Feb 28, 2022
1 parent 2671c98 commit 4f80693
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
49 changes: 26 additions & 23 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -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();
Expand Down
@@ -0,0 +1 @@
import typeof U from "x"
@@ -0,0 +1 @@
import typeof U from "x";
@@ -0,0 +1,2 @@
import type {} from 'some-module';
export type {} from "some-module"
@@ -0,0 +1,2 @@
import type {} from 'some-module';
export type {} from "some-module";

0 comments on commit 4f80693

Please sign in to comment.