Skip to content

Commit 8897b85

Browse files
committed
fix(formatter): correct checking of the short argument for CallArguments
1 parent 6aeed26 commit 8897b85

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

crates/oxc_formatter/src/utils/member_chain/simple_argument.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl<'a, 'b> SimpleArgument<'a, 'b> {
5151
self.is_simple_impl(0)
5252
}
5353

54+
pub fn is_simple_with_depth(&self, depth: u8) -> bool {
55+
self.is_simple_impl(depth)
56+
}
57+
5458
fn is_simple_impl(&self, depth: u8) -> bool {
5559
if depth >= 2 {
5660
return false;

crates/oxc_formatter/src/write/call_arguments.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,20 +449,20 @@ fn is_simple_ts_type(ty: &TSType<'_>) -> bool {
449449
fn is_relatively_short_argument(argument: &Expression<'_>) -> bool {
450450
match argument {
451451
Expression::BinaryExpression(binary) => {
452-
SimpleArgument::from(&binary.left).is_simple()
453-
&& SimpleArgument::from(&binary.right).is_simple()
452+
SimpleArgument::from(&binary.left).is_simple_with_depth(1)
453+
&& SimpleArgument::from(&binary.right).is_simple_with_depth(1)
454454
}
455455
Expression::LogicalExpression(logical) => {
456-
SimpleArgument::from(&logical.left).is_simple()
457-
&& SimpleArgument::from(&logical.right).is_simple()
456+
SimpleArgument::from(&logical.left).is_simple_with_depth(1)
457+
&& SimpleArgument::from(&logical.right).is_simple_with_depth(1)
458458
}
459459
Expression::TSAsExpression(expr) => {
460460
is_simple_ts_type(&expr.type_annotation)
461-
&& SimpleArgument::from(&expr.expression).is_simple()
461+
&& SimpleArgument::from(&expr.expression).is_simple_with_depth(1)
462462
}
463463
Expression::TSSatisfiesExpression(expr) => {
464464
is_simple_ts_type(&expr.type_annotation)
465-
&& SimpleArgument::from(&expr.expression).is_simple()
465+
&& SimpleArgument::from(&expr.expression).is_simple_with_depth(1)
466466
}
467467
Expression::RegExpLiteral(_) => true,
468468
Expression::CallExpression(call) => match call.arguments.len() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
setTimeout(
2+
() => {
3+
//
4+
},
5+
timeout * Math.pow(1)
6+
);
7+
8+
setTimeout(
9+
() => {
10+
//
11+
},
12+
true || isFinite(Infinity)
13+
);
14+
15+
setTimeout(
16+
() => {
17+
//
18+
},
19+
call(f) as any
20+
);
21+
22+
setTimeout(
23+
() => {
24+
//
25+
},
26+
call(f) satisfies any
27+
);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
setTimeout(
6+
() => {
7+
//
8+
},
9+
timeout * Math.pow(1)
10+
);
11+
12+
setTimeout(
13+
() => {
14+
//
15+
},
16+
true || isFinite(Infinity)
17+
);
18+
19+
setTimeout(
20+
() => {
21+
//
22+
},
23+
call(f) as any
24+
);
25+
26+
setTimeout(
27+
() => {
28+
//
29+
},
30+
call(f) satisfies any
31+
);
32+
33+
==================== Output ====================
34+
setTimeout(
35+
() => {
36+
//
37+
},
38+
timeout * Math.pow(1),
39+
);
40+
41+
setTimeout(
42+
() => {
43+
//
44+
},
45+
true || isFinite(Infinity),
46+
);
47+
48+
setTimeout(
49+
() => {
50+
//
51+
},
52+
call(f) as any,
53+
);
54+
55+
setTimeout(
56+
() => {
57+
//
58+
},
59+
call(f) satisfies any,
60+
);
61+
62+
===================== End =====================

0 commit comments

Comments
 (0)