Skip to content

Commit b064786

Browse files
authored
fix(lint): correct multiline autofix in useConsistentArrowReturn (#8183)
1 parent 204844f commit b064786

File tree

5 files changed

+399
-1
lines changed

5 files changed

+399
-1
lines changed

.changeset/wild-ghosts-design.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#8179](https://github.com/biomejs/biome/issues/8179): The [`useConsistentArrowReturn`](https://biomejs.dev/linter/rules/use-consistent-arrow-return/) rule now correctly handles multiline expressions in its autofix when the `style` option is set to `"always"`.
6+
7+
Previously, the autofix would incorrectly place a newline after the `return` keyword, causing unexpected behavior.
8+
9+
```diff
10+
const foo = (l) =>
11+
l
12+
.split('\n')
13+
14+
- // Incorrectly fixed to:
15+
- const foo = (l) => {
16+
- return
17+
- l.split('\n');
18+
- }
19+
20+
+ // Now correctly produces:
21+
+ const foo = (l) => {
22+
+ return l.split('\n');
23+
+ }
24+
```

crates/biome_js_analyze/src/lint/nursery/use_consistent_arrow_return.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Rule for UseConsistentArrowReturn {
184184
biome_js_syntax::TriviaPieceKind::Whitespace,
185185
" ",
186186
)]))
187-
.with_argument(expr_to_return)
187+
.with_argument(expr_to_return.trim_leading_trivia()?)
188188
.with_semicolon_token(make::token(T![;]))
189189
.build();
190190

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const chainedOnNewLine = (l) =>
2+
l
3+
.split('\n')
4+
5+
const multipleChainedMethods = (arr) =>
6+
arr
7+
.filter(x => x > 0)
8+
.map(x => x * 2)
9+
10+
const objectWithMultipleProperties = () =>
11+
({
12+
prop1: 'value1',
13+
prop2: 'value2'
14+
})
15+
16+
const arrayWithMultipleElements = () =>
17+
[
18+
1,
19+
2,
20+
3
21+
]
22+
23+
const nested = () =>
24+
someFunction(
25+
arg1,
26+
arg2
27+
)
28+
29+
const withInlineComment = (l) =>
30+
l
31+
/* keep this comment */
32+
.split('\n')
33+
34+
const withLineComment = (arr) =>
35+
arr
36+
// this comment is important
37+
.filter(x => x > 0)
38+
39+
const withCommentInObject = () =>
40+
({
41+
// important note
42+
prop1: 'value1',
43+
prop2: 'value2'
44+
})
45+
46+
const withCommentInArray = () =>
47+
[
48+
// first element
49+
1,
50+
2,
51+
3
52+
]
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: multiline.invalid.js
4+
---
5+
# Input
6+
```js
7+
const chainedOnNewLine = (l) =>
8+
l
9+
.split('\n')
10+
11+
const multipleChainedMethods = (arr) =>
12+
arr
13+
.filter(x => x > 0)
14+
.map(x => x * 2)
15+
16+
const objectWithMultipleProperties = () =>
17+
({
18+
prop1: 'value1',
19+
prop2: 'value2'
20+
})
21+
22+
const arrayWithMultipleElements = () =>
23+
[
24+
1,
25+
2,
26+
3
27+
]
28+
29+
const nested = () =>
30+
someFunction(
31+
arg1,
32+
arg2
33+
)
34+
35+
const withInlineComment = (l) =>
36+
l
37+
/* keep this comment */
38+
.split('\n')
39+
40+
const withLineComment = (arr) =>
41+
arr
42+
// this comment is important
43+
.filter(x => x > 0)
44+
45+
const withCommentInObject = () =>
46+
({
47+
// important note
48+
prop1: 'value1',
49+
prop2: 'value2'
50+
})
51+
52+
const withCommentInArray = () =>
53+
[
54+
// first element
55+
1,
56+
2,
57+
3
58+
]
59+
60+
```
61+
62+
# Diagnostics
63+
```
64+
multiline.invalid.js:1:26 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
65+
66+
! This arrow function body should be a block statement.
67+
68+
> 1 │ const chainedOnNewLine = (l) =>
69+
│ ^^^^^^
70+
> 2 │ l
71+
> 3 │ .split('\n')
72+
│ ^^^^^^^^^^^^
73+
4 │
74+
5 │ const multipleChainedMethods = (arr) =>
75+
76+
i Safe fix: Add braces to the arrow function body.
77+
78+
1 1 │ const chainedOnNewLine = (l) =>
79+
2 │ - ··l
80+
3 │ - ····.split('\n')
81+
2 │ + ··{
82+
3+return·l
83+
4+ ····.split('\n');
84+
5+ }
85+
4 6 │
86+
5 7 │ const multipleChainedMethods = (arr) =>
87+
88+
89+
```
90+
91+
```
92+
multiline.invalid.js:5:32 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
93+
94+
! This arrow function body should be a block statement.
95+
96+
3 │ .split('\n')
97+
4 │
98+
> 5 │ const multipleChainedMethods = (arr) =>
99+
│ ^^^^^^^^
100+
> 6 │ arr
101+
> 7 │ .filter(x => x > 0)
102+
> 8 │ .map(x => x * 2)
103+
│ ^^^^^^^^^^^^^^^^
104+
9 │
105+
10 │ const objectWithMultipleProperties = () =>
106+
107+
i Safe fix: Add braces to the arrow function body.
108+
109+
4 4 │
110+
5 5 │ const multipleChainedMethods = (arr) =>
111+
6 │ - ··arr
112+
6 │ + ··{
113+
7+return·arr
114+
7 8 │ .filter(x => x > 0)
115+
8- ····.map(x·=>·x·*·2)
116+
9+ ····.map(x·=>·x·*·2);
117+
10+ }
118+
9 11 │
119+
10 12 │ const objectWithMultipleProperties = () =>
120+
121+
122+
```
123+
124+
```
125+
multiline.invalid.js:7:13 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
126+
127+
! This arrow function body should be a block statement.
128+
129+
5 │ const multipleChainedMethods = (arr) =>
130+
6 │ arr
131+
> 7 │ .filter(x => x > 0)
132+
│ ^^^^^^^^^^
133+
8 │ .map(x => x * 2)
134+
9 │
135+
136+
i Safe fix: Add braces to the arrow function body.
137+
138+
5 5 │ const multipleChainedMethods = (arr) =>
139+
6 6 │ arr
140+
7 │ - ····.filter(x·=>·x·>·0)
141+
7 │ + ····.filter(x·=>·{
142+
8+return·x·>·0;
143+
9+ })
144+
8 10 │ .map(x => x * 2)
145+
9 11 │
146+
147+
148+
```
149+
150+
```
151+
multiline.invalid.js:8:10 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
152+
153+
! This arrow function body should be a block statement.
154+
155+
6 │ arr
156+
7 │ .filter(x => x > 0)
157+
> 8 │ .map(x => x * 2)
158+
│ ^^^^^^^^^^
159+
9 │
160+
10 │ const objectWithMultipleProperties = () =>
161+
162+
i Safe fix: Add braces to the arrow function body.
163+
164+
6 6 │ arr
165+
7 7 │ .filter(x => x > 0)
166+
8 │ - ····.map(x·=>·x·*·2)
167+
8 │ + ····.map(x·=>·{
168+
9+return·x·*·2;
169+
10+ })
170+
9 11 │
171+
10 12 │ const objectWithMultipleProperties = () =>
172+
173+
174+
```
175+
176+
```
177+
multiline.invalid.js:10:38 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
178+
179+
! This arrow function body should be a block statement.
180+
181+
8 │ .map(x => x * 2)
182+
9 │
183+
> 10 │ const objectWithMultipleProperties = () =>
184+
│ ^^^^^
185+
> 11 │ ({
186+
> 12prop1: 'value1',
187+
> 13prop2: 'value2'
188+
> 14})
189+
│ ^^
190+
15 │
191+
16 │ const arrayWithMultipleElements = () =>
192+
193+
i Safe fix: Add braces to the arrow function body.
194+
195+
9 9 │
196+
10 10 │ const objectWithMultipleProperties = () =>
197+
11 │ - ··({
198+
11+ ··{
199+
12 │ + → return·{
200+
12 13 │ prop1: 'value1',
201+
13 14 │ prop2: 'value2'
202+
14- ··})
203+
15+ ··};
204+
16 │ + }
205+
15 17 │
206+
16 18 │ const arrayWithMultipleElements = () =>
207+
208+
209+
```
210+
211+
```
212+
multiline.invalid.js:16:35 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
213+
214+
! This arrow function body should be a block statement.
215+
216+
14 │ })
217+
15 │
218+
> 16 │ const arrayWithMultipleElements = () =>
219+
│ ^^^^^
220+
> 17 │ [
221+
> 18 │ 1,
222+
> 19 │ 2,
223+
> 20 │ 3
224+
> 21 │ ]
225+
│ ^
226+
22 │
227+
23 │ const nested = () =>
228+
229+
i Safe fix: Add braces to the arrow function body.
230+
231+
15 15 │
232+
16 16 │ const arrayWithMultipleElements = () =>
233+
17 │ - ··[
234+
17 │ + ··{
235+
18+return·[
236+
18 191,
237+
19 202,
238+
20 213
239+
21- ··]
240+
22+ ··];
241+
23+ }
242+
22 24 │
243+
23 25 │ const nested = () =>
244+
245+
246+
```
247+
248+
```
249+
multiline.invalid.js:23:16 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
250+
251+
! This arrow function body should be a block statement.
252+
253+
21 │ ]
254+
22 │
255+
> 23 │ const nested = () =>
256+
│ ^^^^^
257+
> 24 │ someFunction(
258+
> 25 │ arg1,
259+
> 26 │ arg2
260+
> 27 │ )
261+
│ ^
262+
28 │
263+
29 │ const withInlineComment = (l) =>
264+
265+
i Safe fix: Add braces to the arrow function body.
266+
267+
22 22 │
268+
23 23 │ const nested = () =>
269+
24 │ - ··someFunction(
270+
24 │ + ··{
271+
25+return·someFunction(
272+
25 26arg1,
273+
26 27arg2
274+
27- ··)
275+
28+ ··);
276+
29+ }
277+
28 30 │
278+
29 31 │ const withInlineComment = (l) =>
279+
280+
281+
```
282+
283+
```
284+
multiline.invalid.js:37:13 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━
285+
286+
! This arrow function body should be a block statement.
287+
288+
35 │ arr
289+
36 │ // this comment is important
290+
> 37 │ .filter(x => x > 0)
291+
│ ^^^^^^^^^^
292+
38 │
293+
39 │ const withCommentInObject = () =>
294+
295+
i Safe fix: Add braces to the arrow function body.
296+
297+
35 35 │ arr
298+
36 36 │ // this comment is important
299+
37 │ - ····.filter(x·=>·x·>·0)
300+
37 │ + ····.filter(x·=>·{
301+
38+return·x·>·0;
302+
39+ })
303+
38 40 │
304+
39 41 │ const withCommentInObject = () =>
305+
306+
307+
```

0 commit comments

Comments
 (0)