Skip to content

Commit 4f80693

Browse files
Fix import type/typeof printing with no specifiers (#14309)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 2671c98 commit 4f80693

5 files changed

Lines changed: 32 additions & 23 deletions

File tree

packages/babel-generator/src/generators/modules.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,38 +177,41 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
177177
this.word("import");
178178
this.space();
179179

180-
if (node.importKind === "type" || node.importKind === "typeof") {
180+
const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
181+
if (isTypeKind) {
181182
this.word(node.importKind);
182183
this.space();
183184
}
184185

185186
const specifiers = node.specifiers.slice(0);
186-
if (specifiers?.length) {
187-
// print "special" specifiers first
188-
for (;;) {
189-
const first = specifiers[0];
190-
if (
191-
isImportDefaultSpecifier(first) ||
192-
isImportNamespaceSpecifier(first)
193-
) {
194-
this.print(specifiers.shift(), node);
195-
if (specifiers.length) {
196-
this.token(",");
197-
this.space();
198-
}
199-
} else {
200-
break;
187+
const hasSpecifiers = !!specifiers.length;
188+
// print "special" specifiers first. The loop condition is constant,
189+
// but there is a "break" in the body.
190+
while (hasSpecifiers) {
191+
const first = specifiers[0];
192+
if (isImportDefaultSpecifier(first) || isImportNamespaceSpecifier(first)) {
193+
this.print(specifiers.shift(), node);
194+
if (specifiers.length) {
195+
this.token(",");
196+
this.space();
201197
}
198+
} else {
199+
break;
202200
}
201+
}
203202

204-
if (specifiers.length) {
205-
this.token("{");
206-
this.space();
207-
this.printList(specifiers, node);
208-
this.space();
209-
this.token("}");
210-
}
203+
if (specifiers.length) {
204+
this.token("{");
205+
this.space();
206+
this.printList(specifiers, node);
207+
this.space();
208+
this.token("}");
209+
} else if (isTypeKind && !hasSpecifiers) {
210+
this.token("{");
211+
this.token("}");
212+
}
211213

214+
if (hasSpecifiers || isTypeKind) {
212215
this.space();
213216
this.word("from");
214217
this.space();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import typeof U from "x"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import typeof U from "x";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import type {} from 'some-module';
2+
export type {} from "some-module"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import type {} from 'some-module';
2+
export type {} from "some-module";

0 commit comments

Comments
 (0)