Skip to content

fix: break container instead of expanding one of many object siblings (#641)#793

Open
todor-a wants to merge 2 commits into
dprint:mainfrom
todor-a:align-inline-multi-line-with-prettier
Open

fix: break container instead of expanding one of many object siblings (#641)#793
todor-a wants to merge 2 commits into
dprint:mainfrom
todor-a:align-inline-multi-line-with-prettier

Conversation

@todor-a

@todor-a todor-a commented May 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #641. Aligns layout with prettier.

Problem

const a = [{ name: "FPN" }, { name: "FPN Class" }, { name: "Type" }, { name: "Description" }];

formatted as (one sibling expanded inline, others stay inline):

const a = [{ name: "FPN" }, { name: "FPN Class" }, { name: "Type" }, {
  name: "Description",
}];

Prettier at the same width breaks the array fully.

Fix

gen_separated_values_with_result previously set allow_inline_multi_line=true per node based on allows_inline_multi_line. When every sibling qualified, the resolver picked one to expand inline.

Now: precompute the flags, count siblings that would compete for the slot — excluding arrow / function expressions (the canonical Array.from(x, () => { … }) hugging shape). If 2+ remain, none get the slot, so the container breaks.

Behavior

input before after
array/call/tuple with 2+ object siblings, too wide last expanded inline fully broken (prettier-aligned)
Array.from(x, () => { body }) hugged still hugs
[{ … one long obj … }] hugged still hugs

Spec updates

Pinned old hugging shape; now pin new:

  • ArrayExpression_PreferSingleLine_True.txt
  • issue0520.txt (large nested-JSON snapshot)
  • old_repo/issue067.txt
  • TupleType_PreferSingleLine_True.txt

issue0312.txt is unchanged (arrow exclusion preserves it).

New: issue0641.txt — regression + preserved arrow-hugging + single-object-hugging + tuple/array-of-arrays variants. Patterns cross-referenced against prettier tests/format/js/arrays.

cargo test --release passes.

todor-a added 2 commits May 28, 2026 15:43
…dprint#641)

Previously `const a = [{...}, {...}, {...}, {...}];` could format as

  const a = [{...}, {...}, {...}, {
    longest,
  }];

— a single sibling was promoted to inline multi-line while the others
stayed inline. dprint's resolver does this whenever every value passes
`allows_inline_multi_line` (issue dprint#641).

Adjust `gen_separated_values_with_result`: precompute the
inline-multi-line flags, then count siblings that would actually
compete for the slot — ignoring arrow/function-expression candidates,
which are the canonical prettier-style hugging shape
(`Array.from(x, () => { … })`, `foo(arg, () => {})`). If two or more
non-arrow siblings remain, none get inline-multi-line, so the resolver
breaks the container.

This aligns the array-of-objects layout with prettier:

  const a = [
    { name: "FPN" },
    { name: "FPN Class" },
    …
  ];

The change also applies to function-call arguments and tuple types,
keeping behavior consistent across all `gen_separated_values`
consumers.

Spec updates (existing snaps that pinned the old hugging shape):
- specs/expressions/ArrayExpression/ArrayExpression_PreferSingleLine_True.txt
- specs/issues/issue0520.txt  (large nested-JSON snapshot)
- specs/issues/old_repo/issue067.txt
- specs/types/TupleType/TupleType_PreferSingleLine_True.txt

Last-arg arrow hugging (issue0312) is preserved by the new
`is_function_hugging_candidate` helper that drops arrow/fn siblings
from the count.

New spec: specs/issues/issue0641.txt covering the regression, the
preserved arrow-hugging behavior, the single-object-hugging behavior,
and tuple-of-types / array-of-arrays variants. Patterns
cross-referenced against prettier's tests/format/js/arrays fixtures.
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.

Array of objects is not formatted nicely

1 participant