Skip to content

Commit 120801c

Browse files
committed
fix(formatter): correct printing yield argument
1 parent 4243d95 commit 120801c

File tree

6 files changed

+324
-8
lines changed

6 files changed

+324
-8
lines changed

crates/oxc_formatter/src/write/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ use self::{
8383
object_like::ObjectLike,
8484
object_pattern_like::ObjectPatternLike,
8585
parameters::{ParameterLayout, ParameterList},
86+
return_or_throw_statement::FormatAdjacentArgument,
8687
semicolon::OptionalSemicolon,
8788
type_parameters::{FormatTSTypeParameters, FormatTSTypeParametersOptions},
8889
utils::{
@@ -935,12 +936,9 @@ impl<'a> FormatWrite<'a, FormatJsArrowFunctionExpressionOptions>
935936

936937
impl<'a> FormatWrite<'a> for AstNode<'a, YieldExpression<'a>> {
937938
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
938-
write!(f, "yield")?;
939-
if self.delegate() {
940-
write!(f, "*")?;
941-
}
939+
write!(f, ["yield", self.delegate().then_some("*")])?;
942940
if let Some(argument) = &self.argument() {
943-
write!(f, [space(), argument])?;
941+
write!(f, [space(), FormatAdjacentArgument(argument)])?;
944942
}
945943
Ok(())
946944
}

crates/oxc_formatter/src/write/return_or_throw_statement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> Format<'a> for ReturnAndThrowStatement<'a, '_> {
6060
write!(f, self.keyword())?;
6161

6262
if let Some(argument) = self.argument() {
63-
write!(f, [space(), FormatReturnOrThrowArgument(argument)])?;
63+
write!(f, [space(), FormatAdjacentArgument(argument)])?;
6464
}
6565

6666
let dangling_comments = f.context().comments().comments_before(self.span().end);
@@ -84,9 +84,9 @@ impl<'a> Format<'a> for ReturnAndThrowStatement<'a, '_> {
8484
}
8585
}
8686

87-
pub struct FormatReturnOrThrowArgument<'a, 'b>(&'b AstNode<'a, Expression<'a>>);
87+
pub struct FormatAdjacentArgument<'a, 'b>(pub &'b AstNode<'a, Expression<'a>>);
8888

89-
impl<'a> Format<'a> for FormatReturnOrThrowArgument<'a, '_> {
89+
impl<'a> Format<'a> for FormatAdjacentArgument<'a, '_> {
9090
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
9191
let argument = self.0;
9292

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function *t1() {
2+
yield (
3+
// comment
4+
a as any
5+
);
6+
}
7+
function *t2() {
8+
yield (
9+
// comment
10+
a as any
11+
) + 1;
12+
}
13+
function *t3() {
14+
yield (
15+
// comment
16+
a as any
17+
) ? 0 : 1;
18+
}
19+
function *t4() {
20+
yield (
21+
// comment
22+
a as any
23+
).b;
24+
}
25+
function *t5() {
26+
yield (
27+
// comment
28+
a as any
29+
)[a];
30+
}
31+
function *t6() {
32+
yield (
33+
// comment
34+
a as any
35+
)();
36+
}
37+
function *t7() {
38+
yield (
39+
// comment
40+
a as any
41+
)``;
42+
}
43+
function *t8() {
44+
yield (
45+
// comment
46+
a as any
47+
) as any;
48+
}
49+
function *t9() {
50+
yield (
51+
// comment
52+
a as any
53+
) satisfies any;
54+
}
55+
function *t10() {
56+
yield (
57+
// comment
58+
a as any
59+
)!;
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
function *t1() {
6+
yield (
7+
// comment
8+
a as any
9+
);
10+
}
11+
function *t2() {
12+
yield (
13+
// comment
14+
a as any
15+
) + 1;
16+
}
17+
function *t3() {
18+
yield (
19+
// comment
20+
a as any
21+
) ? 0 : 1;
22+
}
23+
function *t4() {
24+
yield (
25+
// comment
26+
a as any
27+
).b;
28+
}
29+
function *t5() {
30+
yield (
31+
// comment
32+
a as any
33+
)[a];
34+
}
35+
function *t6() {
36+
yield (
37+
// comment
38+
a as any
39+
)();
40+
}
41+
function *t7() {
42+
yield (
43+
// comment
44+
a as any
45+
)``;
46+
}
47+
function *t8() {
48+
yield (
49+
// comment
50+
a as any
51+
) as any;
52+
}
53+
function *t9() {
54+
yield (
55+
// comment
56+
a as any
57+
) satisfies any;
58+
}
59+
function *t10() {
60+
yield (
61+
// comment
62+
a as any
63+
)!;
64+
}
65+
66+
==================== Output ====================
67+
68+
===================== End =====================
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function *t1() {
2+
yield (
3+
// comment
4+
a as any
5+
);
6+
}
7+
function *t2() {
8+
yield (
9+
// comment
10+
a as any
11+
) + 1;
12+
}
13+
function *t3() {
14+
yield (
15+
// comment
16+
a as any
17+
) ? 0 : 1;
18+
}
19+
function *t4() {
20+
yield (
21+
// comment
22+
a as any
23+
).b;
24+
}
25+
function *t5() {
26+
yield (
27+
// comment
28+
a as any
29+
)[a];
30+
}
31+
function *t6() {
32+
yield (
33+
// comment
34+
a as any
35+
)();
36+
}
37+
function *t7() {
38+
yield (
39+
// comment
40+
a as any
41+
)``;
42+
}
43+
function *t8() {
44+
yield (
45+
// comment
46+
a as any
47+
) as any;
48+
}
49+
function *t9() {
50+
yield (
51+
// comment
52+
a as any
53+
) satisfies any;
54+
}
55+
function *t10() {
56+
yield (
57+
// comment
58+
a as any
59+
)!;
60+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
function *t1() {
6+
yield (
7+
// comment
8+
a as any
9+
);
10+
}
11+
function *t2() {
12+
yield (
13+
// comment
14+
a as any
15+
) + 1;
16+
}
17+
function *t3() {
18+
yield (
19+
// comment
20+
a as any
21+
) ? 0 : 1;
22+
}
23+
function *t4() {
24+
yield (
25+
// comment
26+
a as any
27+
).b;
28+
}
29+
function *t5() {
30+
yield (
31+
// comment
32+
a as any
33+
)[a];
34+
}
35+
function *t6() {
36+
yield (
37+
// comment
38+
a as any
39+
)();
40+
}
41+
function *t7() {
42+
yield (
43+
// comment
44+
a as any
45+
)``;
46+
}
47+
function *t8() {
48+
yield (
49+
// comment
50+
a as any
51+
) as any;
52+
}
53+
function *t9() {
54+
yield (
55+
// comment
56+
a as any
57+
) satisfies any;
58+
}
59+
function *t10() {
60+
yield (
61+
// comment
62+
a as any
63+
)!;
64+
}
65+
66+
==================== Output ====================
67+
function* t1() {
68+
yield (
69+
// comment
70+
a as any
71+
);
72+
}
73+
function* t2() {
74+
yield (
75+
// comment
76+
(a as any) + 1
77+
);
78+
}
79+
function* t3() {
80+
yield (
81+
// comment
82+
(a as any)
83+
? 0
84+
: 1
85+
);
86+
}
87+
function* t4() {
88+
yield (
89+
// comment
90+
(a as any).b
91+
);
92+
}
93+
function* t5() {
94+
yield (
95+
// comment
96+
(a as any)[a]
97+
);
98+
}
99+
function* t6() {
100+
yield (
101+
// comment
102+
(a as any)()
103+
);
104+
}
105+
function* t7() {
106+
yield (
107+
// comment
108+
(a as any)``
109+
);
110+
}
111+
function* t8() {
112+
yield (
113+
// comment
114+
a as any as any
115+
);
116+
}
117+
function* t9() {
118+
yield (
119+
// comment
120+
a as any satisfies any
121+
);
122+
}
123+
function* t10() {
124+
yield (
125+
// comment
126+
(a as any)!
127+
);
128+
}
129+
130+
===================== End =====================

0 commit comments

Comments
 (0)