Skip to content

fix(prettier): lay out arrays like Prettier (#275, #295, #300) - #292

Merged
leonidaz merged 7 commits into
mainfrom
fix/prettier-array-trailing-comma-275
Sep 24, 2026
Merged

leonidaz merged 7 commits into
mainfrom
fix/prettier-array-trailing-comma-275

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #275
Closes #295
Closes #300

Summary

The array printer now works like Prettier's printArray. Before, it kept any array written across lines on several lines, kept blank lines between elements even when the array fit, forced one-property objects in arrays onto one line, and printed number arrays one element per line. It also printed a comma after the last element of an array with blank lines, whatever trailingComma was set to (#275). And it moved a comment before an element to after the previous comma, so the next pass attached it to the previous element and dropped JSDoc casts (#295, #300).

Input Before Now (matches Prettier)
['x', + 'y', + ] kept on 4 lines ["x", "y"]
[first, + blank line + ...rest] kept multiline, blank line included [first, ...rest]
27 numbers in one array one number per line several numbers per line (fill)
[[1, 2], [3, 4]] one line one row per line
[{ + a: 1, + }] [ + { a: 1 }, + ] object stays expanded
[1, + blank line + 2,] with trailingComma: "none" 2, 2
[first, /** @type {Entry} */ (second)] comment moves before the line break, then onto first, and the cast is dropped unchanged
useMemo(() => value, [long deps]) deps array expanded after value, [ all arguments broken out

Changes

  • printArrayExpression ports printArray. The group breaks only when the array doesn't fit, or when every element is an object (or every element an array) with more than one entry. A blank line after an element becomes a softline, so it shows only when the array breaks. Number-only arrays use fill, and a blank line there is always kept. A trailing hole still always prints its comma ([Bug]: Formatter removes trailing array holes and changes iterator behavior #269).
  • Objects in arrays print like any other object. The object printer's array-only modes (isInArray, allowInlineObject) and the no-op isInAttribute are gone, along with the helpers that moved element comments (canAttachLeadingCommentToPreviousElement, buildInlineArrayCommentDoc).
  • Call arguments follow two more of Prettier's shouldExpandLastArg rules. An array after a lone arrow function (useMemo(() => value, [deps])) and a number-only array after other arguments are no longer expanded in place. The four conditionalGroup returns also add Prettier's breakParent when the hugged argument breaks. A break inside a conditionalGroup doesn't reach the groups around it, so without this, [on("click", () => { … })] stayed on the [ line. The old array printer hid this by forcing breaks from the source layout.
  • Declarations use the fluid layout for array initializers, like Prettier: let arr = breaks before [ only when let arr = [ doesn't fit. The special layout for arrays with blank lines is removed (objects keep theirs).
  • isNextLineEmptyAfterIndex is split out of isNextLineEmpty for Prettier's isLineAfterElementEmpty, which first finds the element's comma, past parentheses and comments.

One difference from Prettier

In a number-only array, a block comment on its own line before an element keeps its line break. Prettier prints 1, /* note */ and then 2 on the next line, but that isn't stable: its next pass attaches the comment to 1 and prints 1 /* note */, 2. Prettier already breaks before a leading line comment. This PR does the same for any leading comment that printTsrxNode ends with a line break (hasOwnLineLeadingComment), so the comment stays where it was and the output is idempotent.

Repository files

Five tracked .tsrx files had short multiline arrays (mostly expect(…).toEqual([…]) in the Vite plugin runtime tests) and are reformatted. pnpm format:check passes.

Tests

Merge note

#308 merged first and changed hasOwnLineLeadingComment to take the comments printed ahead of the node. 8178e02 updates the call in printArrayElementsConcisely, which now passes a JSDoc cast's ahead comments from getTypeCastParens.

Related

Filed while working on this:

🤖 Generated with Claude Code


Note

Medium Risk
Large formatter behavior change in @tsrx/prettier-plugin will reshuffle array/object/call layout across the repo on format, though scope is printing only and coverage is extensive.

Overview
Replaces the custom array printer with Prettier-aligned printArrayExpression, so arrays collapse when they fit, break for homogeneous multi-entry object/array matrices, pack numeric literals with fill, and only preserve blank lines between elements when the array actually breaks. Element-leading comments stay attached to the right element (fixing lost JSDoc casts), and trailingComma: "none" applies to multiline arrays except trailing holes.

Simplifies related printing paths: drops array/attribute-specific object inlining (isInArray, allowInlineObject, isInAttribute) and the old comment-relocation helpers; uses fluid = [ breaking for array declarations; and tightens call arguments so dependency arrays after a lone arrow callback and concise numeric arrays break out with siblings, with breakParent when hugged args force a break.

Adds a large Prettier-parity test suite and reformats a few tracked .tsrx expectations (mostly single-line toEqual([…]) arrays).

Reviewed by Cursor Bugbot for commit 423ea14. Bugbot is set up for automated code reviews on this repo. Configure here.

…275)

In the blank-line ArrayExpression layout, the last element got its own
comma whenever trailing commas were off, and the separate trailing comma
was appended when they were on, so a comma printed either way. The last
element now never carries a comma, and the shared trailingCommaDoc adds
it (and keeps a trailing hole's comma) like every other layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 24, 2026 8:31pm UTC
tsrx-website-mcp Ready Ready Preview Sep 24, 2026 8:31pm UTC

Request Review

leonidaz and others added 3 commits September 24, 2026 17:01
The array printer kept any array written across lines on several lines,
kept blank lines between elements even when the array fit, forced
one-property objects inline, and printed numbers one per line. It is
now a port of Prettier's printArray: an array breaks only when it
doesn't fit or is a matrix of objects or arrays with several entries,
a blank line survives only in a broken array, and number-only arrays
pack with fill. Objects in arrays print like any other object.

Supporting changes from Prettier:
- call arguments don't expand a deps array after a lone arrow or a
  number-only array after other arguments, and hugged arguments that
  break add a breakParent (breaks don't propagate out of conditionalGroup)
- array declarator inits use the fluid layout

A block comment on its own line before an element in a number array
keeps its line break, which Prettier drops and then reparses as a
trailing comment on the previous element.

Also reformats the tracked .tsrx files that had short multiline arrays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The array printer rewrite dropped the code that moved an element's
leading comments after the previous comma, so the comment now stays
with its element and JSDoc casts survive a second pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Number arrays crash while formatting
    • printArrayElementsConcisely now passes the next element's leading comments into hasOwnLineLeadingComment, so number-only arrays format instead of calling .some on the options object.

Create PR

Or push these changes by commenting:

@cursor push 59bad17af7
Preview (59bad17af7)
diff --git a/packages/prettier-plugin/src/index.js b/packages/prettier-plugin/src/index.js
--- a/packages/prettier-plugin/src/index.js
+++ b/packages/prettier-plugin/src/index.js
@@ -6457,13 +6457,14 @@
 		parts.push([print(elementPath), isLast ? trailingComma : ',']);
 
 		if (!isLast) {
+			const nextElement = elements[index + 1];
 			// Prettier breaks before a leading line comment. A block comment on its
 			// own line breaks too: after `1, /* note */` it would reparse as a
 			// trailing comment of `1`.
 			parts.push(
 				isLineAfterElementEmpty(elements[index], options)
 					? [hardline, hardline]
-					: hasOwnLineLeadingComment(elements[index + 1], options)
+					: hasOwnLineLeadingComment(nextElement, nextElement.leadingComments ?? [], options)
 						? hardline
 						: line,
 			);

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 5a7cc4b. Configure here.

Comment thread packages/prettier-plugin/src/index.js Resolved
leonidaz and others added 2 commits September 24, 2026 17:24
#308 changed hasOwnLineLeadingComment to take the comments printed ahead
of the node, so the number-array printer passed the options object as
the comments and every number array with two or more elements threw.
The separator before an element is now pushed while visiting that
element, whose path gives getTypeCastParens the comments that print
ahead of a JSDoc cast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 73a5cd8 into main Sep 24, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-array-trailing-comma-275 branch September 24, 2026 21:36
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant