Skip to content

Commit

Permalink
Avoid line breaks in import attributes (#16349)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Kachkaev <[email protected]>
  • Loading branch information
fisker and kachkaev authored Jun 5, 2024
1 parent 4a5b26f commit efc3d05
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
14 changes: 14 additions & 0 deletions changelog_unreleased/javascript/16349.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#### Avoid line breaks in import attributes (#16349 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type: "json" };

// Prettier stable
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type:
"json" };

// Prettier main
import something from "./some-very-very-very-very-very-very-very-very-long-path.json" with { type: "json" };
```
1 change: 1 addition & 0 deletions src/language-js/print/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) {
}

if (
node.type === "ImportAttribute" ||
(rightNode.type === "CallExpression" &&
rightNode.callee.name === "require") ||
// do not put values on a separate line from the key in json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,97 @@ export * from "./test.json" /* with */ /* with */ assert { type: "json" };
================================================================================
`;

exports[`long-sources.js [acorn] format 1`] = `
"Unexpected token (1:37)
> 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" };
Cause: Unexpected token (1:36)"
`;

exports[`long-sources.js [espree] format 1`] = `
"Unexpected token with (1:37)
> 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" };
Cause: Unexpected token with"
`;

exports[`long-sources.js [meriyah] format 1`] = `
"Unexpected token: 'with' (1:40)
> 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" };
Cause: [1:40]: Unexpected token: 'with'"
`;

exports[`long-sources.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import a10 from "./aaaaaaaaaa.json" with {
type: "json" };
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with {
type: "json" };
import a80
from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import("./aaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
=====================================output=====================================
import a10 from "./aaaaaaaaaa.json" with { type: "json" };
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a80 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import("./aaaaaaaaaa.json", { with: { type: "json" } });
import("./aaaaaaaaaaaaaaaaaaaa.json", { with: { type: "json" } });
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", { with: { type: "json" } });
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
with: { type: "json" },
});
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
with: { type: "json" },
});
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {
with: { type: "json" },
});
import(
"./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
{ with: { type: "json" } }
);
import(
"./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
{ with: { type: "json" } }
);
================================================================================
`;

exports[`multi-types.js [acorn] format 1`] = `
"Unexpected token (1:31)
> 1 | import json from "./foo.json" with { type: "json", type: "bar" };
Expand Down
3 changes: 3 additions & 0 deletions tests/format/js/import-attributes/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const errors = {
"without-from.js",
"non-type.js",
"keyword-detect.js",
"long-sources.js",
],
espree: [
"dynamic-import.js",
Expand All @@ -18,6 +19,7 @@ const errors = {
"without-from.js",
"non-type.js",
"keyword-detect.js",
"long-sources.js",
],
meriyah: [
"dynamic-import.js",
Expand All @@ -28,6 +30,7 @@ const errors = {
"without-from.js",
"non-type.js",
"keyword-detect.js",
"long-sources.js",
],
};
runFormatTest(import.meta, ["babel", "typescript"], { errors });
20 changes: 20 additions & 0 deletions tests/format/js/import-attributes/long-sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import a10 from "./aaaaaaaaaa.json" with {
type: "json" };
import a20 from "./aaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a30 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a40 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a50 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a60 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };
import a70 from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with {
type: "json" };
import a80
from "./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" with { type: "json" };

import("./aaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})
import("./aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json", {with: { type: "json" }})

0 comments on commit efc3d05

Please sign in to comment.