Repository navigation
feat: contract small arrays into a single line - #416
helenanova wants to merge 1 commit into
Conversation
Arrays whose items are all primitive values now render on a single line when they fit within 80 columns, matching the default line width of Prettier and Biome. This stops the infinite diff loop when sort-package-json is used alongside Biome (or any formatter that keeps short arrays on one line). Closes keithamus#356
|
@keithamus Thanks for the ping. I think the original problem is no longer relevant. Biome v2 now formats Contracting the arrays by default would therefore recreate the original problem, just in the opposite direction. |
|
Hm. That does raise into question the motivation for this feature then. I think once principle we want to stick to is that transient use of tools like package managers and such shouldn't cause thrashing with sort-package-json... |
|
Thanks both for the pushback. Some context on the motivation: this PR implements the heuristic @keithamus proposed on #356 ("adding heuristics for contracting small arrays into a single line... PRs welcome") - I picked it up directly from that comment. On thrashing: the rule is deliberately narrow. It only contracts arrays of primitives that fit within 80 columns; anything containing objects, nested arrays, or a longer line serializes byte-identically to today. So it only touches small keyword-style arrays (keywords, files, workspaces globs), never dependency lists or scripts. 80 columns matches the default print width of Prettier and Biome, so for the arrays it contracts, a formatter on default settings produces the same one-line shape rather than fighting it. That said, if @yoriiis's point means the original loop is resolved for current Biome users and this no longer earns its complexity, I'm happy to rework it as opt-in behind a flag, or close the PR - whichever direction you prefer. |
|
As already noted, this conflicts with Prettier >cat package.json
{"workspaces": ["./packages/*"]}
>npx prettier package.json
{
"workspaces": [
"./packages/*"
]
}And there is no way to opt out, so I'm against this change unless we provide an option.
I don't think this is a good idea. |
|
It is worth mentioning that this not only conflicts with Prettier, but also with package managers. |
|
Let's close this. Perhaps also we should close #356 unless anyone can reproduce, but it sounds like biome v2 doesn't repro, and I'm not sure we should go to the lengths required to specifically just do this for v1. |
|
Also I really appreciate all the effort you put into this @helenanova. I'd be happy to review new PRs if you wanted to find another issue to work on! |
Closes #356.
@keithamus on #356: "I am open to adding heuristics for contracting small arrays into a single line. PRs welcome to resolve that precise issue." This implements exactly that.
What it does
editStringJSONpreviously serialized withJSON.stringify(value, null, indent), which always expands arrays across lines. It now uses a small recursive serializer that is byte-identical toJSON.stringify(…, null, indent)except for one heuristic:null) is contracted onto a single line when the resulting line fits within 80 columns, matching the default line width of Prettier and Biome.So the issue's repro now converges instead of looping:
One repo-tooling note
Prettier special-cases files named
package.json(thejson-stringifyparser always expands arrays), which would fight this heuristic in this repo's own lint chain. The PR adds a one-entry Prettier override sopackage.jsonis formatted with the regularjsonparser, which preserves the contracted form. The repo'spackage.jsonitself is reformatted by the new rule, so you can see the effect in the diff.Testing
tests/inline-arrays.jswith 9 tests: contraction, the exact 80-column boundary (65 chars fits, 66 doesn't), non-primitive arrays never contracted, nested arrays, empty arrays, tab indent, CRLF, missing trailing newline, compact input.ava271 passed (262 existing + 9 new),tstyche7 assertions passed,eslint .clean,npm run lint(eslint + prettier check +cli.js --check) passes.