Skip to content

Commit efc3d05

Browse files
fiskerkachkaev
andauthored
Avoid line breaks in import attributes (#16349)
Co-authored-by: Alexander Kachkaev <[email protected]>
1 parent 4a5b26f commit efc3d05

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#### Avoid line breaks in import attributes (#16349 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type: "json" };
7+
8+
// Prettier stable
9+
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type:
10+
"json" };
11+
12+
// Prettier main
13+
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type: "json" };
14+
```

src/language-js/print/assignment.js

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) {
138138
}
139139

140140
if (
141+
node.type === "ImportAttribute" ||
141142
(rightNode.type === "CallExpression" &&
142143
rightNode.callee.name === "require") ||
143144
// do not put values on a separate line from the key in json

tests/format/js/import-attributes/__snapshots__/format.test.js.snap

+91
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,97 @@ export * from "./test.json" /* with */ /* with */ assert { type: "json" };
159159
================================================================================
160160
`;
161161

162+
exports[`long-sources.js [acorn] format 1`] = `
163+
"Unexpected token (1:37)
164+
> 1 | import a10 from "./aaaaaaaaaa.json" with {
165+
| ^
166+
2 | type: "json" };
167+
3 | import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
168+
4 | import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
169+
Cause: Unexpected token (1:36)"
170+
`;
171+
172+
exports[`long-sources.js [espree] format 1`] = `
173+
"Unexpected token with (1:37)
174+
> 1 | import a10 from "./aaaaaaaaaa.json" with {
175+
| ^
176+
2 | type: "json" };
177+
3 | import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
178+
4 | import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
179+
Cause: Unexpected token with"
180+
`;
181+
182+
exports[`long-sources.js [meriyah] format 1`] = `
183+
"Unexpected token: 'with' (1:40)
184+
> 1 | import a10 from "./aaaaaaaaaa.json" with {
185+
| ^
186+
2 | type: "json" };
187+
3 | import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
188+
4 | import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
189+
Cause: [1:40]: Unexpected token: 'with'"
190+
`;
191+
192+
exports[`long-sources.js format 1`] = `
193+
====================================options=====================================
194+
parsers: ["babel", "typescript"]
195+
printWidth: 80
196+
| printWidth
197+
=====================================input======================================
198+
import a10 from "./aaaaaaaaaa.json" with {
199+
type: "json" };
200+
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
201+
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
202+
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
203+
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
204+
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
205+
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with {
206+
type: "json" };
207+
import a80
208+
from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
209+
210+
import("./aaaaaaaaaa.json", {with: { type: "json" }})
211+
import("./aaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
212+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
213+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
214+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
215+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
216+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
217+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
218+
219+
=====================================output=====================================
220+
import a10 from "./aaaaaaaaaa.json" with { type: "json" };
221+
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
222+
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
223+
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
224+
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
225+
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
226+
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
227+
import a80 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
228+
229+
import("./aaaaaaaaaa.json", { with: { type: "json" } });
230+
import("./aaaaaaaaaaaaaaaaaaaa.json", { with: { type: "json" } });
231+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", { with: { type: "json" } });
232+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
233+
with: { type: "json" },
234+
});
235+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
236+
with: { type: "json" },
237+
});
238+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
239+
with: { type: "json" },
240+
});
241+
import(
242+
"./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
243+
{ with: { type: "json" } }
244+
);
245+
import(
246+
"./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
247+
{ with: { type: "json" } }
248+
);
249+
250+
================================================================================
251+
`;
252+
162253
exports[`multi-types.js [acorn] format 1`] = `
163254
"Unexpected token (1:31)
164255
> 1 | import json from "./foo.json" with { type: "json", type: "bar" };

tests/format/js/import-attributes/format.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const errors = {
88
"without-from.js",
99
"non-type.js",
1010
"keyword-detect.js",
11+
"long-sources.js",
1112
],
1213
espree: [
1314
"dynamic-import.js",
@@ -18,6 +19,7 @@ const errors = {
1819
"without-from.js",
1920
"non-type.js",
2021
"keyword-detect.js",
22+
"long-sources.js",
2123
],
2224
meriyah: [
2325
"dynamic-import.js",
@@ -28,6 +30,7 @@ const errors = {
2830
"without-from.js",
2931
"non-type.js",
3032
"keyword-detect.js",
33+
"long-sources.js",
3134
],
3235
};
3336
runFormatTest(import.meta, ["babel", "typescript"], { errors });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import a10 from "./aaaaaaaaaa.json" with {
2+
type: "json" };
3+
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
4+
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
5+
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
6+
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
7+
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
8+
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with {
9+
type: "json" };
10+
import a80
11+
from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
12+
13+
import("./aaaaaaaaaa.json", {with: { type: "json" }})
14+
import("./aaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
15+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
16+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
17+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
18+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
19+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
20+
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})

0 commit comments

Comments
 (0)