Skip to content

fix: break up an array that's the body of an assigned arrow function instead of moving the arrow function to the next line - #912

Merged
dsherret merged 1 commit into
mainfrom
fix-arrow-array-body-stability
Nov 10, 2026
Merged

dsherret merged 1 commit into
mainfrom
fix-arrow-array-body-stability

Conversation

@dsherret

Copy link
Copy Markdown
Member

Fixes output that changed when formatted a second time. At lineWidth: 40:

const f = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

was formatted as:

const f =
    () => [
        1,
        2,
        // ...
    ];

and formatting that again gave:

const f = () => [
    1,
    2,
    // ...
];

The arrow function's expression body is in a new line group, so when the array didn't fit, the arrow function was moved to the line after the = before the array was broken up. The array then keeps its line breaks, so the next time const f = () => [ fits on the first line. Now the array is broken up in place the first time, the same as const f = [...], const f = () => ({...}) and a block body. This applies to everything that's generated as an assignment (declarators, assignments, properties, default values), including curried arrow functions.

It's not done when arrayExpression.preferSingleLine is true or the array prefers hanging, because those arrays don't keep their line breaks.

Changes to output that was stable

  • An array that fits on the line after the = is now broken up in place:

    const f =
        () => [1, 2, 3, 4, 5, 6, 7, 8, 9];

    becomes const f = () => [ with an element per line.

  • The array is broken up before the parameters: const f = (\n a,\n b,\n) => [...] becomes const f = (a, b) => [ with the array expanded.

When the code up to the [ doesn't fit on the line of the =, the arrow function still moves to the next line.

…instead of moving the arrow function to the next line
@dsherret
dsherret merged commit 8d9ba53 into main Nov 10, 2026
1 check passed
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.

1 participant