Skip to content

feat: contract small arrays into a single line - #416

Closed
helenanova wants to merge 1 commit into
keithamus:mainfrom
helenanova:feat-inline-small-arrays
Closed

helenanova wants to merge 1 commit into
keithamus:mainfrom
helenanova:feat-inline-small-arrays

Conversation

@helenanova

Copy link
Copy Markdown

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

editStringJSON previously serialized with JSON.stringify(value, null, indent), which always expands arrays across lines. It now uses a small recursive serializer that is byte-identical to JSON.stringify(…, null, indent) except for one heuristic:

  • An array whose items are all primitive values (strings, numbers, booleans, null) is contracted onto a single line when the resulting line fits within 80 columns, matching the default line width of Prettier and Biome.
  • Arrays containing objects or other arrays, and arrays that would exceed 80 columns, render expanded exactly as before.
  • Indent detection (spaces/tabs), CRLF preservation, trailing-newline handling, and compact (unindented) input are all unchanged.

So the issue's repro now converges instead of looping:

"workspaces": ["./packages/*"]

One repo-tooling note

Prettier special-cases files named package.json (the json-stringify parser always expands arrays), which would fight this heuristic in this repo's own lint chain. The PR adds a one-entry Prettier override so package.json is formatted with the regular json parser, which preserves the contracted form. The repo's package.json itself is reformatted by the new rule, so you can see the effect in the diff.

Testing

  • Added tests/inline-arrays.js with 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.
  • Local run on Node 22: ava 271 passed (262 existing + 9 new), tstyche 7 assertions passed, eslint . clean, npm run lint (eslint + prettier check + cli.js --check) passes.

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 keithamus left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quite reasonable. @fisker thoughts?

@yoriiis as the OP of #356 I'm curious what you think about this?

@yoriiis

yoriiis commented Oct 17, 2026

Copy link
Copy Markdown

@keithamus Thanks for the ping.

I think the original problem is no longer relevant. Biome v2 now formats package.json like Prettier (arrays expanded by default), so with Biome's defaults my original loop is already gone — I no longer need the lineWidth: 1 workaround.

Contracting the arrays by default would therefore recreate the original problem, just in the opposite direction.

@keithamus

Copy link
Copy Markdown
Owner

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...

@helenanova

Copy link
Copy Markdown
Author

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.

@fisker

fisker commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

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.

override so package.json is formatted with the regular json parser

I don't think this is a good idea.

@fisker

fisker commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

It is worth mentioning that this not only conflicts with Prettier, but also with package managers.

>cat package.json
{
  "name": "x",
  "workspaces": ["./packages/*"]
}

>yarn
➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 67ms

>cat package.json
{
  "name": "x",
  "workspaces": [
    "./packages/*"
  ]
}

@fisker fisker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keithamus

Copy link
Copy Markdown
Owner

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.

@keithamus keithamus closed this Oct 22, 2026
@keithamus

Copy link
Copy Markdown
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conflict with Biome formatter: array formatting in package.json creates persistent diffs

4 participants