fix: break container instead of expanding one of many object siblings (#641)#793
Open
todor-a wants to merge 2 commits into
Open
fix: break container instead of expanding one of many object siblings (#641)#793todor-a wants to merge 2 commits into
todor-a wants to merge 2 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #641. Aligns layout with prettier.
Problem
formatted as (one sibling expanded inline, others stay inline):
Prettier at the same width breaks the array fully.
Fix
gen_separated_values_with_resultpreviously setallow_inline_multi_line=trueper node based onallows_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
Array.from(x, () => { body })[{ … one long obj … }]Spec updates
Pinned old hugging shape; now pin new:
ArrayExpression_PreferSingleLine_True.txtissue0520.txt(large nested-JSON snapshot)old_repo/issue067.txtTupleType_PreferSingleLine_True.txtissue0312.txtis unchanged (arrow exclusion preserves it).New:
issue0641.txt— regression + preserved arrow-hugging + single-object-hugging + tuple/array-of-arrays variants. Patterns cross-referenced against prettiertests/format/js/arrays.cargo test --releasepasses.