Skip to content

Commit 4d9ef2d

Browse files
authored
fix: preserve comments on import attribute values (#2167)
1 parent ea6a3dc commit 4d9ef2d

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

internal/printer/printer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,8 +3802,12 @@ func (p *Printer) emitImportAttribute(node *ast.ImportAttribute) {
38023802
p.emitImportAttributeName(node.Name())
38033803
p.writePunctuation(":")
38043804
p.writeSpace()
3805-
/// !!! emit trailing comments of value
3806-
p.emitExpression(node.Value, ast.OperatorPrecedenceDisallowComma)
3805+
value := node.Value
3806+
if p.emitContext.EmitFlags(node.Value)&EFNoLeadingComments == 0 {
3807+
commentRange := getCommentRange(value)
3808+
p.emitTrailingComments(commentRange.Pos(), commentSeparatorAfter)
3809+
}
3810+
p.emitExpression(value, ast.OperatorPrecedenceDisallowComma)
38073811
p.exitNode(node.AsNode(), state)
38083812
}
38093813

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/importAttributesWithValueComments.ts] ////
2+
3+
//// [a.ts]
4+
export default {
5+
a: "a",
6+
b: "b",
7+
1: "1",
8+
}
9+
10+
//// [b.ts]
11+
import a from "./a" with { a: /* a */ "a", "b": /* b */ "b" };
12+
a;
13+
14+
15+
//// [a.js]
16+
export default {
17+
a: "a",
18+
b: "b",
19+
1: "1",
20+
};
21+
//// [b.js]
22+
import a from "./a" with { a: /* a */ "a", "b": /* b */ "b" };
23+
a;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/importAttributesWithValueComments.ts] ////
2+
3+
=== /a.ts ===
4+
export default {
5+
a: "a",
6+
>a : Symbol(a, Decl(a.ts, 0, 16))
7+
8+
b: "b",
9+
>b : Symbol(b, Decl(a.ts, 1, 11))
10+
11+
1: "1",
12+
>1 : Symbol(1, Decl(a.ts, 2, 11))
13+
}
14+
15+
=== /b.ts ===
16+
import a from "./a" with { a: /* a */ "a", "b": /* b */ "b" };
17+
>a : Symbol(a, Decl(b.ts, 0, 6))
18+
19+
a;
20+
>a : Symbol(a, Decl(b.ts, 0, 6))
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/importAttributesWithValueComments.ts] ////
2+
3+
=== /a.ts ===
4+
export default {
5+
>{ a: "a", b: "b", 1: "1",} : { a: string; b: string; 1: string; }
6+
7+
a: "a",
8+
>a : string
9+
>"a" : "a"
10+
11+
b: "b",
12+
>b : string
13+
>"b" : "b"
14+
15+
1: "1",
16+
>1 : string
17+
>"1" : "1"
18+
}
19+
20+
=== /b.ts ===
21+
import a from "./a" with { a: /* a */ "a", "b": /* b */ "b" };
22+
>a : { a: string; b: string; 1: string; }
23+
>a : any
24+
25+
a;
26+
>a : { a: string; b: string; 1: string; }
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @target: esnext
2+
// @module: esnext
3+
4+
// @filename: /a.ts
5+
export default {
6+
a: "a",
7+
b: "b",
8+
1: "1",
9+
}
10+
11+
// @filename: /b.ts
12+
import a from "./a" with { a: /* a */ "a", "b": /* b */ "b" };
13+
a;

0 commit comments

Comments
 (0)