Skip to content

Commit e2a20b6

Browse files
fix(formatter): add space after commas in import attributes (#22274)
Fixes #22273 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 299d6f8 commit e2a20b6

3 files changed

Lines changed: 65 additions & 5 deletions

File tree

crates/oxc_formatter/src/print/import_declaration.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ImportAttribute<'a>>> {
231231
let trailing_separator =
232232
FormatTrailingCommas::ES5.trailing_separator(f.options());
233233

234-
f.join_with(soft_line_break()).entries_with_trailing_separator(
235-
self.iter(),
236-
",",
237-
trailing_separator,
238-
);
234+
f.join_with(soft_line_break_or_space())
235+
.entries_with_trailing_separator(
236+
self.iter(),
237+
",",
238+
trailing_separator,
239+
);
239240
},),
240241
should_insert_space_around_brackets
241242
)]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Issue #22273 - Should have a space after commas in import attributes
2+
import myEmbeddedDb from './my.db' with { type: 'sqlite', embed: 'true' }
3+
import myEmbeddedDb2 from './my.db' with {type: 'sqlite',embed: 'true'}
4+
import myEmbeddedDb3 from './my.db' with { embed: 'true', type: 'sqlite' }
5+
6+
// Should be spread over multiple lines
7+
import myEmbeddedDb4 from './my.db' with { type: 'sqlite', embed: 'true', veryLongAttribute: 'true' }
8+
import myEmbeddedDb5 from './my.db' with { veryVeryVeryVeryVeryVeryVeryVeryVeryLongAttribute: 'true' }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
// Issue #22273 - Should have a space after commas in import attributes
6+
import myEmbeddedDb from './my.db' with { type: 'sqlite', embed: 'true' }
7+
import myEmbeddedDb2 from './my.db' with {type: 'sqlite',embed: 'true'}
8+
import myEmbeddedDb3 from './my.db' with { embed: 'true', type: 'sqlite' }
9+
10+
// Should be spread over multiple lines
11+
import myEmbeddedDb4 from './my.db' with { type: 'sqlite', embed: 'true', veryLongAttribute: 'true' }
12+
import myEmbeddedDb5 from './my.db' with { veryVeryVeryVeryVeryVeryVeryVeryVeryLongAttribute: 'true' }
13+
14+
==================== Output ====================
15+
------------------
16+
{ printWidth: 80 }
17+
------------------
18+
// Issue #22273 - Should have a space after commas in import attributes
19+
import myEmbeddedDb from "./my.db" with { type: "sqlite", embed: "true" };
20+
import myEmbeddedDb2 from "./my.db" with { type: "sqlite", embed: "true" };
21+
import myEmbeddedDb3 from "./my.db" with { embed: "true", type: "sqlite" };
22+
23+
// Should be spread over multiple lines
24+
import myEmbeddedDb4 from "./my.db" with {
25+
type: "sqlite",
26+
embed: "true",
27+
veryLongAttribute: "true",
28+
};
29+
import myEmbeddedDb5 from "./my.db" with {
30+
veryVeryVeryVeryVeryVeryVeryVeryVeryLongAttribute: "true",
31+
};
32+
33+
-------------------
34+
{ printWidth: 100 }
35+
-------------------
36+
// Issue #22273 - Should have a space after commas in import attributes
37+
import myEmbeddedDb from "./my.db" with { type: "sqlite", embed: "true" };
38+
import myEmbeddedDb2 from "./my.db" with { type: "sqlite", embed: "true" };
39+
import myEmbeddedDb3 from "./my.db" with { embed: "true", type: "sqlite" };
40+
41+
// Should be spread over multiple lines
42+
import myEmbeddedDb4 from "./my.db" with {
43+
type: "sqlite",
44+
embed: "true",
45+
veryLongAttribute: "true",
46+
};
47+
import myEmbeddedDb5 from "./my.db" with {
48+
veryVeryVeryVeryVeryVeryVeryVeryVeryLongAttribute: "true",
49+
};
50+
51+
===================== End =====================

0 commit comments

Comments
 (0)