Skip to content

fix: keep hugging object siblings when they're all multi-line - #893

Merged
dsherret merged 1 commit into
mainfrom
fix-hug-multi-line-object-siblings
Nov 5, 2026
Merged

dsherret merged 1 commit into
mainfrom
fix-hug-multi-line-object-siblings

Conversation

@dsherret

@dsherret dsherret commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #793.

#793 fixed #641, where one object in an array of short objects was expanded in the middle of the others:

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

It did that by not letting any object or array hug when it has a sibling of the same kind, which also broke up arrays where every object is multi-line and the hugging is uniform:

const columns = [{
  title: "Name",
  key: "name",
}, {
  title: "Size",
  key: "size",
}];

That now formats as before #793. The container only breaks when the siblings are mixed. If every object-like or array-like value that has a sibling of the same kind is multi-line as written (a newline after its { or [, using the same check its own formatting uses, so its preferSingleLine is respected), they can hug as before. Otherwise the container breaks like #793 does, including the #641 case and mixes like [{ a: 1 }, {\n b: 2,\n}, { c: 3 }].

When a hugged container has mixed siblings, the multi-line ones were usually only split because of the old hugging, so they now go on one line when they fit:

// input
<Nav items={[{ label: <a href="/home">Home</a> }, {
  label: <a href="/settings/profile">Profile</a>,
}, { label: "Billing and invoices" }]} />;

// output
<Nav
  items={[
    { label: <a href="/home">Home</a> },
    { label: <a href="/settings/profile">Profile</a> },
    { label: "Billing and invoices" },
  ]}
/>;

A sibling that doesn't fit, or has a comment on another line, stays multi-line. If everything fits after that, the container stays on one line too.

A container that's already broken out (a newline after its [, ( or <) is left as written, including siblings split over several lines.

In the regression corpus I use, one file changed: an array of objects that each have a comment on its own line now hugs with preferSingleLine: true. The comment keeps each object multi-line, so they're all multi-line.

Tests

Only break the container for same-kind siblings (#641) when they're mixed. When every value with a same-kind neighbor is multi-line because of how it's written (a newline after its open brace or bracket with that kind's preferSingleLine off), they stay hugged like before.
@dsherret
dsherret merged commit 0f2dd0b into main Nov 5, 2026
1 check passed
@dsherret
dsherret deleted the fix-hug-multi-line-object-siblings branch November 5, 2026 00:17
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