Skip to content

Commit 09b1db0

Browse files
Elide empty named imports next to a default import under verbatimModuleSyntax (#64578)
Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
1 parent 59f5b02 commit 09b1db0

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎tsc/internal/transformers/tstransforms/typeeraser.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
316316
}
317317
name := n.Name()
318318
namedBindings := tx.Visitor().VisitNode(n.NamedBindings)
319+
// Empty {} due to type-only import erasure can be skipped if there is also a default import
320+
if name != nil && namedBindings != nil && ast.IsNamedImports(namedBindings) &&
321+
len(namedBindings.AsNamedImports().Elements.Nodes) == 0 &&
322+
len(n.NamedBindings.AsNamedImports().Elements.Nodes) != 0 {
323+
// the default binding keeps the import; a source-written {} is left as is
324+
namedBindings = nil
325+
}
319326
if name == nil && namedBindings == nil {
320327
// all import bindings were elided
321328
return nil
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [tests/cases/compiler/verbatimModuleSyntaxDefaultImportTypeOnlyNamed.ts] ////
2+
3+
//// [a.ts]
4+
export default class A {}
5+
export type T = number;
6+
export const v = 0;
7+
8+
//// [b.ts]
9+
import A1, { type T } from "./a";
10+
import A2, { type T as T2, v } from "./a";
11+
import { type T as T3 } from "./a";
12+
import A4, {} from "./a";
13+
import A5, { /* comment */ type T as T5 } from "./a";
14+
import A6, {
15+
type T as T6,
16+
} from "./a";
17+
18+
export { A1, A2, A4, A5, A6, v };
19+
20+
21+
//// [a.js]
22+
export default class A {
23+
}
24+
export const v = 0;
25+
//// [b.js]
26+
import A1 from "./a";
27+
import A2, { v } from "./a";
28+
import {} from "./a";
29+
import A4, {} from "./a";
30+
import A5 from "./a";
31+
import A6 from "./a";
32+
export { A1, A2, A4, A5, A6, v };
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/compiler/verbatimModuleSyntaxDefaultImportTypeOnlyNamed.ts] ////
2+
3+
=== /a.ts ===
4+
export default class A {}
5+
>A : Symbol(A, Decl(a.ts, 0, 0))
6+
7+
export type T = number;
8+
>T : Symbol(T, Decl(a.ts, 0, 25))
9+
10+
export const v = 0;
11+
>v : Symbol(v, Decl(a.ts, 2, 12))
12+
13+
=== /b.ts ===
14+
import A1, { type T } from "./a";
15+
>A1 : Symbol(A1, Decl(b.ts, 0, 6))
16+
>T : Symbol(T, Decl(b.ts, 0, 12))
17+
18+
import A2, { type T as T2, v } from "./a";
19+
>A2 : Symbol(A2, Decl(b.ts, 1, 6))
20+
>T : Symbol(T, Decl(a.ts, 0, 25))
21+
>T2 : Symbol(T2, Decl(b.ts, 1, 12))
22+
>v : Symbol(v, Decl(b.ts, 1, 26))
23+
24+
import { type T as T3 } from "./a";
25+
>T : Symbol(T, Decl(a.ts, 0, 25))
26+
>T3 : Symbol(T3, Decl(b.ts, 2, 8))
27+
28+
import A4, {} from "./a";
29+
>A4 : Symbol(A4, Decl(b.ts, 3, 6))
30+
31+
import A5, { /* comment */ type T as T5 } from "./a";
32+
>A5 : Symbol(A5, Decl(b.ts, 4, 6))
33+
>T : Symbol(T, Decl(a.ts, 0, 25))
34+
>T5 : Symbol(T5, Decl(b.ts, 4, 12))
35+
36+
import A6, {
37+
>A6 : Symbol(A6, Decl(b.ts, 5, 6))
38+
39+
type T as T6,
40+
>T : Symbol(T, Decl(a.ts, 0, 25))
41+
>T6 : Symbol(T6, Decl(b.ts, 5, 12))
42+
43+
} from "./a";
44+
45+
export { A1, A2, A4, A5, A6, v };
46+
>A1 : Symbol(A1, Decl(b.ts, 9, 8))
47+
>A2 : Symbol(A2, Decl(b.ts, 9, 12))
48+
>A4 : Symbol(A4, Decl(b.ts, 9, 16))
49+
>A5 : Symbol(A5, Decl(b.ts, 9, 20))
50+
>A6 : Symbol(A6, Decl(b.ts, 9, 24))
51+
>v : Symbol(v, Decl(b.ts, 9, 28))
52+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/compiler/verbatimModuleSyntaxDefaultImportTypeOnlyNamed.ts] ////
2+
3+
=== /a.ts ===
4+
export default class A {}
5+
>A : A
6+
7+
export type T = number;
8+
>T : number
9+
10+
export const v = 0;
11+
>v : 0
12+
>0 : 0
13+
14+
=== /b.ts ===
15+
import A1, { type T } from "./a";
16+
>A1 : typeof A1
17+
>T : any
18+
19+
import A2, { type T as T2, v } from "./a";
20+
>A2 : typeof A1
21+
>T : any
22+
>T2 : any
23+
>v : 0
24+
25+
import { type T as T3 } from "./a";
26+
>T : any
27+
>T3 : any
28+
29+
import A4, {} from "./a";
30+
>A4 : typeof A1
31+
32+
import A5, { /* comment */ type T as T5 } from "./a";
33+
>A5 : typeof A1
34+
>T : any
35+
>T5 : any
36+
37+
import A6, {
38+
>A6 : typeof A1
39+
40+
type T as T6,
41+
>T : any
42+
>T6 : any
43+
44+
} from "./a";
45+
46+
export { A1, A2, A4, A5, A6, v };
47+
>A1 : typeof A1
48+
>A2 : typeof A1
49+
>A4 : typeof A1
50+
>A5 : typeof A1
51+
>A6 : typeof A1
52+
>v : 0
53+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @target: esnext
2+
// @module: esnext
3+
// @moduleResolution: bundler
4+
// @verbatimModuleSyntax: true
5+
6+
// @Filename: /a.ts
7+
export default class A {}
8+
export type T = number;
9+
export const v = 0;
10+
11+
// @Filename: /b.ts
12+
import A1, { type T } from "./a";
13+
import A2, { type T as T2, v } from "./a";
14+
import { type T as T3 } from "./a";
15+
import A4, {} from "./a";
16+
import A5, { /* comment */ type T as T5 } from "./a";
17+
import A6, {
18+
type T as T6,
19+
} from "./a";
20+
21+
export { A1, A2, A4, A5, A6, v };

0 commit comments

Comments
 (0)