Skip to content

Commit 9aad365

Browse files
waltuovleaysgur
andauthored
fix(formatter): keep comments deferred before an assignment operator (#26997)
Closes #23665 Since oxfmt 0.69.0 (#26725), this input loses a comment and prints another one twice: ```ts type myType /* Comment */ = // Comment "VALUE" ``` ```ts // oxfmt 0.69.0 and 0.70.0 type myType = // Comment // Comment "VALUE"; ``` The left side of an assignment-like leaves some comments before the operator unprinted, so they lead the right-hand side: an own-line comment, or a block comment that ends its line. When a line comment followed the operator, `operator_line_run` printed it first. `FormatTrailingComments` advances the cursor by count, so it claimed the pending comment instead. That comment was lost and the line comment was printed again as a leading comment. The same happens for `const`, assignments, class and object properties, and for `// oxfmt-ignore` after the operator. Now nothing glues after the operator while comments before it are pending. Everything leads the right-hand side in source order, like the `as`/`satisfies` site already does. The output is back to what 0.68.0 printed, and for type aliases it matches Prettier 3.9.6: ```ts type myType = /* Comment */ // Comment "VALUE"; ``` For JS values Prettier moves the line comment past a short value (`const a = /* c */ 1; // d`). That is covered by `DIVERGENCES.md#eol-comment-after-assign-colon`, so I added a short note there and in AGENTS.md. Tests: new cases in `operator-eol-line-comment.ts` and `assignment-eol-line-comment.js`. They fail on main and pass with this change. The TS cases match Prettier 3.9.6 at print widths 80 and 100. All oxc_formatter tests pass, and Prettier conformance is unchanged (js 768/811, ts 620/660). AI disclosure: this change and its tests were written with Claude Code. --------- Co-authored-by: waltu <[email protected]> Co-authored-by: leaysgur <[email protected]>
1 parent dc352fd commit 9aad365

9 files changed

Lines changed: 215 additions & 8 deletions

File tree

‎crates/oxc_formatter/AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ The `as`/`satisfies` operator gap follows the same policy (`as_or_satisfies_expr
151151
the operator side breaks only for a riding line comment before the operator.
152152
A suppressed union never runs that printer, so the cast site and `AssignmentLike` lay it out like any other type
153153
- an assignment-like's `=` / `:` (`AssignmentLike`): the left side's trailing run stops at the operator (comments past it are hidden while it prints);
154-
a glued run ending in a line comment prints right after the operator (`operator_line_run`), a block-ending one leads the right-hand side
154+
a glued run ending in a line comment prints right after the operator (`operator_line_run`), a block-ending one leads the right-hand side;
155+
with comments the left side deferred still pending, nothing glues (same as the cast site)
155156

156157
Implemented by the `write_*` helpers in `utils/statement_body.rs` and `FormatParenHeadExpression` (`print/mod.rs`);
157158
their rustdocs cover how the head's generic trailing pass is kept from claiming the gap.

‎crates/oxc_formatter/DIVERGENCES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Block comments glued after the operator before it stay there, in order;
212212
Prettier prints the line comment first and the block on the value's line, crossing the block.
213213
A line comment before the operator rides its `line_suffix` past the operator and gets the same break;
214214
Prettier flushes it past the value.
215+
Comments the left side defers (own-line, or a block ending its line) lead the value, the line comment follows them in source order, as Prettier prints type aliases;
216+
for JS values, Prettier flushes the line comment past the value after a block (`const v = /* c */ 1; // d`) and before an own-line one.
215217

216218
```ts
217219
// input

‎crates/oxc_formatter/src/formatter/comments.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ impl<'a> Comments<'a> {
202202
/// This is automatically called by the trivia formatting functions, but must be
203203
/// called manually if comments are formatted through other means.
204204
#[inline]
205-
pub fn increment_printed_count(&mut self) {
205+
pub fn increment_printed_count(&mut self, comment: &Comment) {
206+
debug_assert_eq!(
207+
self.first_unprinted_span(),
208+
Some(comment.span),
209+
"the claimed comment must be the first unprinted one"
210+
);
206211
self.printed_count += 1;
207212
}
208213

‎crates/oxc_formatter/src/formatter/trivia.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//! **Implementation**:
4545
//! 1. Calls `comments_before(node.span.start)` to get unprinted leading comments
4646
//! 2. Formats each comment with spacing based on line breaks in original source
47-
//! 3. Advances cursor by calling `increment_printed_count()` for each comment
47+
//! 3. Advances cursor by calling `increment_printed_count(comment)` for each comment
4848
//! 4. Handles special cases like JSDoc comment "nestling"
4949
//!
5050
//! ### Trailing Comment Formatting ([`FormatTrailingComments`])
@@ -149,7 +149,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatLeadingComments<'a> {
149149
) {
150150
let mut leading_comments_iter = comments.into_iter().peekable();
151151
while let Some(comment) = leading_comments_iter.next() {
152-
f.context_mut().comments_mut().increment_printed_count();
152+
f.context_mut().comments_mut().increment_printed_count(comment);
153153
write!(f, format_comment_text(comment));
154154

155155
let lines_after = f
@@ -265,7 +265,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatTrailingComments<'a> {
265265
let mut previous_comment: Option<&Comment> = None;
266266

267267
for comment in comments {
268-
f.context_mut().comments_mut().increment_printed_count();
268+
f.context_mut().comments_mut().increment_printed_count(comment);
269269

270270
let lines_before = f.lines_before(comment.span);
271271
total_lines_before += lines_before;
@@ -447,7 +447,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatDanglingComments<'a> {
447447
let mut previous_comment: Option<&Comment> = None;
448448

449449
for comment in comments {
450-
f.context_mut().comments_mut().increment_printed_count();
450+
f.context_mut().comments_mut().increment_printed_count(comment);
451451

452452
let should_nestle = previous_comment.is_some_and(|previous_comment| {
453453
should_nestle_adjacent_comments(previous_comment, comment, f.source_text())
@@ -603,7 +603,7 @@ impl<'a> FormatCommentBeforeContent<'a> {
603603

604604
impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentBeforeContent<'_> {
605605
fn fmt(&self, f: &mut JsFormatter<'_, 'a>) {
606-
f.context_mut().comments_mut().increment_printed_count();
606+
f.context_mut().comments_mut().increment_printed_count(self.0);
607607
write!(f, format_comment_text(self.0));
608608
if self.0.is_line() {
609609
write!(f, hard_line_break());

‎crates/oxc_formatter/src/utils/assignment_like.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,11 @@ impl<'a> AssignmentLike<'a, '_> {
542542
/// The comments glued after the operator up to a line comment ending its line (`= /* c */ // d`),
543543
/// printed right after the operator (see `Comments::mark_suppressed_after_operator` for their suppression target).
544544
/// A right-hand side that hugs its leading comments (object, array, template) takes them instead.
545+
/// Comments the left side deferred are still pending: nothing glues, all lead the right-hand side in order.
545546
fn operator_line_run(&self, operator_end: u32, f: &JsFormatter<'_, 'a>) -> &'a [Comment] {
546-
if self.right_hugs_leading_comments() {
547+
if self.right_hugs_leading_comments()
548+
|| f.context().comments().has_comment_before(operator_end)
549+
{
547550
return &[];
548551
}
549552
let run = f.context().comments().end_of_line_comments_after(operator_end);

‎crates/oxc_formatter/tests/fixtures/js/comments/assignment-eol-line-comment.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,32 @@ class C3 {
5656
f // c
5757
= 1;
5858
}
59+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
60+
// (Prettier flushes the line comment past the value: `const b6 = /* c */ 1; // d`)
61+
const b6 /* c */
62+
= // d
63+
1;
64+
b7 /* c */
65+
= // d
66+
1;
67+
const b8
68+
// c
69+
= // d
70+
1;
71+
a.b
72+
// c
73+
= // d
74+
1;
75+
const b9 /* c */
76+
= // oxfmt-ignore
77+
[1, 2];
78+
class C4 {
79+
f /* c */
80+
= // d
81+
1;
82+
}
83+
const o4 = {
84+
p /* c */
85+
: // d
86+
1,
87+
};

‎crates/oxc_formatter/tests/fixtures/js/comments/assignment-eol-line-comment.js.snap‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,35 @@ class C3 {
6060
f // c
6161
= 1;
6262
}
63+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
64+
// (Prettier flushes the line comment past the value: `const b6 = /* c */ 1; // d`)
65+
const b6 /* c */
66+
= // d
67+
1;
68+
b7 /* c */
69+
= // d
70+
1;
71+
const b8
72+
// c
73+
= // d
74+
1;
75+
a.b
76+
// c
77+
= // d
78+
1;
79+
const b9 /* c */
80+
= // oxfmt-ignore
81+
[1, 2];
82+
class C4 {
83+
f /* c */
84+
= // d
85+
1;
86+
}
87+
const o4 = {
88+
p /* c */
89+
: // d
90+
1,
91+
};
6392

6493
==================== Output ====================
6594
------------------
@@ -123,6 +152,36 @@ class C3 {
123152
f = // c
124153
1;
125154
}
155+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
156+
// (Prettier flushes the line comment past the value: `const b6 = /* c */ 1; // d`)
157+
const b6 =
158+
/* c */
159+
// d
160+
1;
161+
b7 =
162+
/* c */
163+
// d
164+
1;
165+
const b8 =
166+
// c
167+
// d
168+
1;
169+
a.b =
170+
// c
171+
// d
172+
1;
173+
const b9 =
174+
/* c */
175+
// oxfmt-ignore
176+
[1, 2];
177+
class C4 {
178+
f /* c */ = // d
179+
1;
180+
}
181+
const o4 = {
182+
p /* c */: // d
183+
1,
184+
};
126185

127186
-------------------
128187
{ printWidth: 100 }
@@ -185,5 +244,35 @@ class C3 {
185244
f = // c
186245
1;
187246
}
247+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
248+
// (Prettier flushes the line comment past the value: `const b6 = /* c */ 1; // d`)
249+
const b6 =
250+
/* c */
251+
// d
252+
1;
253+
b7 =
254+
/* c */
255+
// d
256+
1;
257+
const b8 =
258+
// c
259+
// d
260+
1;
261+
a.b =
262+
// c
263+
// d
264+
1;
265+
const b9 =
266+
/* c */
267+
// oxfmt-ignore
268+
[1, 2];
269+
class C4 {
270+
f /* c */ = // d
271+
1;
272+
}
273+
const o4 = {
274+
p /* c */: // d
275+
1,
276+
};
188277

189278
===================== End =====================

‎crates/oxc_formatter/tests/fixtures/ts/comments/operator-eol-line-comment.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ interface I {
2323
| AmemberLongEnoughToMakeTheUnionTypeBreakIntoMultipleLines
2424
| BmemberLongEnoughToMakeTheUnionTypeBreakIntoMultipleLines;
2525
}
26+
27+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
28+
type Deferred /* c */
29+
= // d
30+
"VALUE";
31+
32+
type DeferredOwnLine
33+
// c
34+
= // d
35+
"VALUE";
36+
37+
type DeferredUnion<T> /* c */
38+
= // d
39+
| "A" | T;
40+
41+
type DeferredIgnore /* c */
42+
= // prettier-ignore
43+
"A" | "B";

‎crates/oxc_formatter/tests/fixtures/ts/comments/operator-eol-line-comment.ts.snap‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ interface I {
2828
| BmemberLongEnoughToMakeTheUnionTypeBreakIntoMultipleLines;
2929
}
3030

31+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
32+
type Deferred /* c */
33+
= // d
34+
"VALUE";
35+
36+
type DeferredOwnLine
37+
// c
38+
= // d
39+
"VALUE";
40+
41+
type DeferredUnion<T> /* c */
42+
= // d
43+
| "A" | T;
44+
45+
type DeferredIgnore /* c */
46+
= // prettier-ignore
47+
"A" | "B";
48+
3149
==================== Output ====================
3250
------------------
3351
{ printWidth: 80 }
@@ -58,6 +76,27 @@ interface I {
5876
| BmemberLongEnoughToMakeTheUnionTypeBreakIntoMultipleLines;
5977
}
6078

79+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
80+
type Deferred =
81+
/* c */
82+
// d
83+
"VALUE";
84+
85+
type DeferredOwnLine =
86+
// c
87+
// d
88+
"VALUE";
89+
90+
type DeferredUnion<T> =
91+
/* c */
92+
// d
93+
"A" | T;
94+
95+
type DeferredIgnore =
96+
/* c */
97+
// prettier-ignore
98+
"A" | "B";
99+
61100
-------------------
62101
{ printWidth: 100 }
63102
-------------------
@@ -87,4 +126,25 @@ interface I {
87126
| BmemberLongEnoughToMakeTheUnionTypeBreakIntoMultipleLines;
88127
}
89128

129+
// Comments the left side defers lead the right-hand side, the line comment follows them in source order
130+
type Deferred =
131+
/* c */
132+
// d
133+
"VALUE";
134+
135+
type DeferredOwnLine =
136+
// c
137+
// d
138+
"VALUE";
139+
140+
type DeferredUnion<T> =
141+
/* c */
142+
// d
143+
"A" | T;
144+
145+
type DeferredIgnore =
146+
/* c */
147+
// prettier-ignore
148+
"A" | "B";
149+
90150
===================== End =====================

0 commit comments

Comments
 (0)