Skip to content

Commit eeba1db

Browse files
committed
fix(formatter): skip test-call layout when arguments have comments (#27119)
Fixes #24960, closes #26926.
1 parent e9b6ee0 commit eeba1db

10 files changed

Lines changed: 308 additions & 7 deletions

File tree

‎crates/oxc_formatter/DIVERGENCES.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,37 @@ Prettier keeps the Markdown hard break in `/**` only, following jsdoc3.
705705
Tools disagree on `/***`: TypeScript (`isJSDocLikeText`, so editor hovers) treats it as JSDoc, jsdoc3 ignores it.
706706
Formatting it as JSDoc does not break jsdoc3, which ignores it either way.
707707
Type cast comments already follow the rule, in Prettier too.
708+
709+
## test-call-comment-order
710+
711+
- Why: invariant (prettier/prettier#20029, prettier/prettier#20043)
712+
- Pin: `tests/fixtures/js/calls/test-call-comment-order.js`
713+
714+
```js
715+
// input
716+
test("x", () => {
717+
run();
718+
}, // first
719+
// second
720+
60000);
721+
722+
// ours
723+
test(
724+
"x",
725+
() => {
726+
run();
727+
}, // first
728+
// second
729+
60000,
730+
);
731+
732+
// prettier
733+
test("x", () => {
734+
run();
735+
}, // second // first
736+
60000);
737+
```
738+
739+
Prettier's compact test call layout merges and reverses the comments, and is not a fixpoint.
740+
We fall back to the regular argument layout when comments sit around the arguments, following prettier/prettier#20043.
741+
A same-line block comment also falls back, the same output as the other special call layouts (React hook, `require`).

‎crates/oxc_formatter/src/print/call_like_expression/arguments.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for AstNode<'a, ArenaVec<'a, Argument<'
7373
| Argument::TaggedTemplateExpression(_)
7474
)
7575
))
76-
&& is_test_call_expression(call))
76+
&& is_test_call_expression(call, f.comments()))
7777
})
7878
|| is_multiline_template_only_args(self, f.source_text())
7979
|| is_graphql_call_with_single_template_arg(self, call_expression)

‎crates/oxc_formatter/src/print/call_like_expression/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, CallExpression<'a>> {
5555
Expression::StaticMemberExpression(_) | Expression::ComputedMemberExpression(_)
5656
)
5757
&& !is_simple_module_import(self.arguments(), f.comments())
58-
&& !is_test_call_expression(self)
58+
&& !is_test_call_expression(self, f.comments())
5959
{
6060
MemberChain::from_call_expression(self, f).fmt(f);
6161
} else {

‎crates/oxc_formatter/src/print/parameters.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, FormalParameters<'a>> {
6767
// Check if parent is a test call, but exclude angular test wrappers
6868
// like `inject`, `async`, `fakeAsync`, `waitForAsync` because their
6969
// inner function parameters should still be allowed to break.
70-
is_test_call_expression(call) && !is_angular_test_wrapper(call)
70+
is_test_call_expression(call, f.comments()) && !is_angular_test_wrapper(call)
7171
} else {
7272
false
7373
}

‎crates/oxc_formatter/src/print/type_parameters.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatTSTypeParameters<'a, '_> {
147147
write!(
148148
f,
149149
[group(&format_args!("<", format_with(|f| {
150-
if matches!(self.decl.grand_parent(), AstNodes::CallExpression(call) if is_test_call_expression(call))
150+
if matches!(self.decl.grand_parent(), AstNodes::CallExpression(call) if is_test_call_expression(call, f.comments()))
151151
{
152152
f.join_nodes_with_space().entries_with_trailing_separator(params, ",", TrailingSeparator::Omit);
153153
} else {

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
use std::iter;
2+
13
use oxc_ast::ast::*;
4+
use oxc_span::GetSpan;
25

3-
use crate::ast_nodes::{AstNode, AstNodes};
6+
use crate::{
7+
ast_nodes::{AstNode, AstNodes},
8+
formatter::Comments,
9+
};
410

511
/// This is a specialized function that checks if the current [call expression]
612
/// resembles a call expression usually used by a testing frameworks.
@@ -17,13 +23,26 @@ use crate::ast_nodes::{AstNode, AstNodes};
1723
/// 4. The third argument, if present, has to be a number literal
1824
/// 5. The second argument has to be an [arrow function expression] or [function expression]
1925
/// 6. Both function must have zero or one parameters
26+
/// 7. No comments around the [arguments]
2027
///
2128
/// [call expression]: CallExpression
2229
/// [callee]: Expression
2330
/// [arguments]: CallExpression::arguments
2431
/// [arrow function expression]: ArrowFunctionExpression
2532
/// [function expression]: Function
26-
pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>) -> bool {
33+
pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>, comments: &Comments) -> bool {
34+
is_test_call_shape(call, comments) && !has_comment_around_arguments(call, comments)
35+
}
36+
37+
/// The compact test call layout joins arguments with spaces, which breaks comment order.
38+
fn has_comment_around_arguments(call: &CallExpression, comments: &Comments) -> bool {
39+
let first = call.type_arguments.as_ref().map_or(call.callee.span().end, |t| t.span.end);
40+
let starts = iter::once(first).chain(call.arguments.iter().map(|a| a.span().end));
41+
let ends = call.arguments.iter().map(|a| a.span().start).chain(iter::once(call.span.end));
42+
starts.zip(ends).any(|(start, end)| comments.has_any_comment_in_range(start, end))
43+
}
44+
45+
fn is_test_call_shape(call: &AstNode<CallExpression<'_>>, comments: &Comments) -> bool {
2746
if call.optional {
2847
return false;
2948
}
@@ -37,7 +56,7 @@ pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>) -> bool {
3756
(Some(argument), None, None) if arguments.len() == 1 => {
3857
if is_angular_test_wrapper(call) && {
3958
if let AstNodes::CallExpression(call) = call.parent() {
40-
is_test_call_expression(call)
59+
is_test_call_expression(call, comments)
4160
} else {
4261
false
4362
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
test("comments after callback", () => {
2+
run();
3+
}, // first
4+
// second
5+
// third
6+
60000);
7+
8+
it("comments before timeout", function (done) {
9+
done();
10+
},
11+
// first
12+
// second
13+
60000);
14+
15+
test("comments before callback", // first
16+
// second
17+
() => {
18+
run();
19+
});
20+
21+
describe.only(
22+
// description
23+
"suite", () => {
24+
run();
25+
});
26+
27+
test("trailing", () => {
28+
run();
29+
}, 15000 // first
30+
// second
31+
);
32+
33+
test("own-line block", () => {
34+
run();
35+
},
36+
/* c */
37+
15000);
38+
39+
it("params", /* c */ function (aaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccc) {
40+
run();
41+
});
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
test("comments after callback", () => {
6+
run();
7+
}, // first
8+
// second
9+
// third
10+
60000);
11+
12+
it("comments before timeout", function (done) {
13+
done();
14+
},
15+
// first
16+
// second
17+
60000);
18+
19+
test("comments before callback", // first
20+
// second
21+
() => {
22+
run();
23+
});
24+
25+
describe.only(
26+
// description
27+
"suite", () => {
28+
run();
29+
});
30+
31+
test("trailing", () => {
32+
run();
33+
}, 15000 // first
34+
// second
35+
);
36+
37+
test("own-line block", () => {
38+
run();
39+
},
40+
/* c */
41+
15000);
42+
43+
it("params", /* c */ function (aaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccc) {
44+
run();
45+
});
46+
47+
==================== Output ====================
48+
------------------
49+
{ printWidth: 80 }
50+
------------------
51+
test(
52+
"comments after callback",
53+
() => {
54+
run();
55+
}, // first
56+
// second
57+
// third
58+
60000,
59+
);
60+
61+
it(
62+
"comments before timeout",
63+
function (done) {
64+
done();
65+
},
66+
// first
67+
// second
68+
60000,
69+
);
70+
71+
test(
72+
"comments before callback", // first
73+
// second
74+
() => {
75+
run();
76+
},
77+
);
78+
79+
describe.only(
80+
// description
81+
"suite",
82+
() => {
83+
run();
84+
},
85+
);
86+
87+
test(
88+
"trailing",
89+
() => {
90+
run();
91+
},
92+
15000, // first
93+
// second
94+
);
95+
96+
test(
97+
"own-line block",
98+
() => {
99+
run();
100+
},
101+
/* c */
102+
15000,
103+
);
104+
105+
it(
106+
"params",
107+
/* c */ function (
108+
aaaaaaaaaaaaaaaaaaaa,
109+
bbbbbbbbbbbbbbbbbbbbbbbb,
110+
ccccccccccccccc,
111+
) {
112+
run();
113+
},
114+
);
115+
116+
-------------------
117+
{ printWidth: 100 }
118+
-------------------
119+
test(
120+
"comments after callback",
121+
() => {
122+
run();
123+
}, // first
124+
// second
125+
// third
126+
60000,
127+
);
128+
129+
it(
130+
"comments before timeout",
131+
function (done) {
132+
done();
133+
},
134+
// first
135+
// second
136+
60000,
137+
);
138+
139+
test(
140+
"comments before callback", // first
141+
// second
142+
() => {
143+
run();
144+
},
145+
);
146+
147+
describe.only(
148+
// description
149+
"suite",
150+
() => {
151+
run();
152+
},
153+
);
154+
155+
test(
156+
"trailing",
157+
() => {
158+
run();
159+
},
160+
15000, // first
161+
// second
162+
);
163+
164+
test(
165+
"own-line block",
166+
() => {
167+
run();
168+
},
169+
/* c */
170+
15000,
171+
);
172+
173+
it(
174+
"params",
175+
/* c */ function (aaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccc) {
176+
run();
177+
},
178+
);
179+
180+
===================== End =====================
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test<T /* c */>("type arguments", () => {
2+
run();
3+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
test<T /* c */>("type arguments", () => {
6+
run();
7+
});
8+
9+
==================== Output ====================
10+
------------------
11+
{ printWidth: 80 }
12+
------------------
13+
test<T /* c */>("type arguments", () => {
14+
run();
15+
});
16+
17+
-------------------
18+
{ printWidth: 100 }
19+
-------------------
20+
test<T /* c */>("type arguments", () => {
21+
run();
22+
});
23+
24+
===================== End =====================

0 commit comments

Comments
 (0)