Repository navigation
fix(prettier): lay out arrays like Prettier (#275, #295, #300) - #292
Merged
Merged
Conversation
…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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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]>
This was referenced Sep 24, 2026
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
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.
#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]>
Merged
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.


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, whatevertrailingCommawas 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).['x',+'y',+]["x", "y"][first,+ blank line +...rest][first, ...rest]fill)[[1, 2], [3, 4]][{+a: 1,+}][+{ a: 1 },+][1,+ blank line +2,]withtrailingComma: "none"2,2[first, /** @type {Entry} */ (second)]first, and the cast is droppeduseMemo(() => value, [long deps])value, [Changes
printArrayExpressionportsprintArray. 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 asoftline, so it shows only when the array breaks. Number-only arrays usefill, 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).isInArray,allowInlineObject) and the no-opisInAttributeare gone, along with the helpers that moved element comments (canAttachLeadingCommentToPreviousElement,buildInlineArrayCommentDoc).shouldExpandLastArgrules. 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 fourconditionalGroupreturns also add Prettier'sbreakParentwhen the hugged argument breaks. A break inside aconditionalGroupdoesn'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.let arr =breaks before[only whenlet arr = [doesn't fit. The special layout for arrays with blank lines is removed (objects keep theirs).isNextLineEmptyAfterIndexis split out ofisNextLineEmptyfor Prettier'sisLineAfterElementEmpty, 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 then2on the next line, but that isn't stable: its next pass attaches the comment to1and prints1 /* note */, 2. Prettier already breaks before a leading line comment. This PR does the same for any leading comment thatprintTsrxNodeends with a line break (hasOwnLineLeadingComment), so the comment stays where it was and the output is idempotent.Repository files
Five tracked
.tsrxfiles had short multiline arrays (mostlyexpect(…).toEqual([…])in the Vite plugin runtime tests) and are reformatted.pnpm format:checkpasses.Tests
arrays lay out like Prettiertests: collapsing, blank lines only in broken arrays,fill, matrices, objects in arrays, comments before elements ([Bug]: Formatter moves a comment that leads an array element onto the previous element and drops JSDoc casts #295/[Bug]: Formatter moves a comment before a later array element onto the previous element, dropping JSDoc casts #300), call-argument rules, JSDoc casts in number arrays, and thebreakParentcase. Expected outputs are Prettier'stypescriptoutput, except that JSDoc cast parentheses are kept, as Prettier'sbabelparser keeps them.ReactiveMapmatrix, trailing holes (the multiline case now forces the break with a line comment), the comment test, and [Bug]: Formatter ignores trailingComma "none" in arrays with blank lines between elements #275's test.pnpm vitest run --project prettier-plugin: 790 passed, and the fullpnpm testpasses. Thetsrx-mcp,vite-plugin-{react,preact,solid,vue}andwebsiteprojects also pass, as dopnpm typecheck,pnpm format:checkandpnpm changeset:check..tsrxfiles: every file is formatted, idempotent, and reparses to the same AST.Merge note
#308 merged first and changed
hasOwnLineLeadingCommentto take the comments printed ahead of the node. 8178e02 updates the call inprintArrayElementsConcisely, which now passes a JSDoc cast'saheadcomments fromgetTypeCastParens.Related
Filed while working on this:
[]or{}moves out of the brackets (parser; same gap as [Bug]: Comments in interfaces, enums, and type literals move to the previous member or out of the body #293)trailingComma: "es5"adds commas after call arguments and parameters (shouldPrintCommalevels reversed)newexpression arguments never break{printArrayExpression)=instead of inside the type🤖 Generated with Claude Code
Note
Medium Risk
Large formatter behavior change in
@tsrx/prettier-pluginwill 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 withfill, 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), andtrailingComma: "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, withbreakParentwhen hugged args force a break.Adds a large Prettier-parity test suite and reformats a few tracked
.tsrxexpectations (mostly single-linetoEqual([…])arrays).Reviewed by Cursor Bugbot for commit 423ea14. Bugbot is set up for automated code reviews on this repo. Configure here.