Skip to content

feat: add arrayExpression.linePerElement option - #739

Merged
dsherret merged 7 commits into
dprint:mainfrom
xseman:array-expression-max-width
Nov 10, 2026
Merged

dsherret merged 7 commits into
dprint:mainfrom
xseman:array-expression-max-width

Conversation

@xseman

@xseman xseman commented Sep 25, 2025 •

Copy link
Copy Markdown
Contributor

Adds arrayExpression.linePerElement (default true, so existing output doesn't change). When false, an array expression whose elements are all literals, identifiers or unary expressions of them keeps the line breaks as written and fits as many elements on each line as lineWidth allows. Any other array (objects, calls, functions, holes, spreads) formats as with true.

// lineWidth: 50, arrayExpression.linePerElement: false
const a = [0x90, 0x94, 0x19, 0x21, 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d];

// becomes
const a = [
    0x90, 0x94, 0x19, 0x21, 0x72, 0x61, 0x6e,
    0x64, 0x6f, 0x6d,
];

This replaces the earlier arrayExpression.maxWidth version, following the review: nothing is measured from the source text anymore. The elements are generated normally, and gen_array_like_nodes sets maintain_line_breaks on the multi-line options. That's the same mechanism binaryExpression.linePerExpression: false uses, so the printer does the wrapping with the formatted widths, comments and blank lines included. The change in generate.rs is about 20 lines.

Validation:

  • Added specs, which also run through the format-twice check.
  • Formatted about 1,600 real files (deno std, material-web) at lineWidth 120, 80 and 40. The default output is byte-identical to main, and with false no file becomes unstable or unparseable.
  • Compared the wrapping of 300 generated number arrays with prettier's fill.

Known limitation (in dprint-core): if the array wraps but all its elements would fit on one indented line, forcedNewLineIfNoNewLine in ir_helpers/gen_separated_values.rs moves the last element to its own line ([\n 1, 2,\n 3,\n]). Skipping that condition when newline_at_start is set fixes it. With that one-line change, the wrapping matches prettier on all 300 arrays and all the specs here still pass. I can open a dprint PR for it if you'd like.

Closes #677
Closes #910

@xseman

xseman commented Dec 29, 2025

Copy link
Copy Markdown
Contributor Author

@dsherret I know you may be busy. This PR has been open for a while. Any feedback would help. Thanks

Comment thread src/generation/generate.rs Resolved
@xseman
xseman force-pushed the array-expression-max-width branch from 9d04925 to 2d2a262 Compare November 4, 2026 21:37
@xseman xseman changed the title feat: add support for arrayExpression.maxWidth configuration feat: add arrayExpression.linePerElement option Nov 4, 2026
@xseman
xseman force-pushed the array-expression-max-width branch from 2d2a262 to 3f77aea Compare November 5, 2026 13:32
When `false`, an array expression whose elements are all literals,
identifiers or unary expressions of them maintains the line breaks as
written and fits as many elements on each line as the line width allows.
Any other array formats as with `true` (the default).

Closes dprint#677
@xseman
xseman force-pushed the array-expression-max-width branch from 3f77aea to 69635e1 Compare November 5, 2026 13:50

@dsherret dsherret left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

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.

Option to keep the existing line breaks of arrays Formatting array elements to a maximum width

2 participants