Skip to content

Commit 41df59b

Browse files
fix(lint): only highlight function names in useMaxParams rule (#7608)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 309ae41 commit 41df59b

File tree

5 files changed

+100
-87
lines changed

5 files changed

+100
-87
lines changed

.changeset/ten-donkeys-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#7604](https://github.com/biomejs/biome/issues/7604): the `useMaxParams` rule now highlights parameter lists instead of entire function bodies. This provides more precise error highlighting. Previously, the entire function was highlighted; now only the parameter list is highlighted, such as `(a, b, c, d, e, f, g, h)`.

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use biome_js_syntax::{
88
JsConstructorParameters, JsFunctionDeclaration, JsFunctionExpression, JsMethodClassMember,
99
JsMethodObjectMember, JsParameters, TsDeclareFunctionDeclaration, TsTypeAliasDeclaration,
1010
};
11-
use biome_rowan::{AstNode, declare_node_union};
11+
use biome_rowan::{AstNode, TextRange, declare_node_union};
1212
use biome_rule_options::use_max_params::UseMaxParamsOptions;
1313

1414
declare_lint_rule! {
@@ -84,6 +84,44 @@ declare_node_union! {
8484
pub AnyFunctionLike = JsFunctionDeclaration | JsFunctionExpression | JsArrowFunctionExpression | JsMethodClassMember | JsMethodObjectMember | JsConstructorClassMember | TsDeclareFunctionDeclaration | TsTypeAliasDeclaration
8585
}
8686

87+
impl AnyFunctionLike {
88+
pub fn parameter_range(&self) -> Option<TextRange> {
89+
match self {
90+
Self::JsFunctionDeclaration(func) => {
91+
func.parameters().ok().map(|params| params.range())
92+
}
93+
Self::JsFunctionExpression(func) => func.parameters().ok().map(|params| params.range()),
94+
Self::JsArrowFunctionExpression(func) => {
95+
func.parameters().ok().map(|params| params.range())
96+
}
97+
Self::JsMethodClassMember(method) => {
98+
method.parameters().ok().map(|params| params.range())
99+
}
100+
Self::JsMethodObjectMember(method) => {
101+
method.parameters().ok().map(|params| params.range())
102+
}
103+
Self::JsConstructorClassMember(constructor) => {
104+
constructor.parameters().ok().map(|params| params.range())
105+
}
106+
Self::TsDeclareFunctionDeclaration(decl) => {
107+
decl.parameters().ok().map(|params| params.range())
108+
}
109+
Self::TsTypeAliasDeclaration(decl) => {
110+
if let Ok(ty) = decl.ty() {
111+
match ty {
112+
biome_js_syntax::AnyTsType::TsFunctionType(func_type) => {
113+
func_type.parameters().ok().map(|params| params.range())
114+
}
115+
_ => None,
116+
}
117+
} else {
118+
None
119+
}
120+
}
121+
}
122+
}
123+
}
124+
87125
#[derive(Debug, Clone)]
88126
pub struct UseMaxParamsState {
89127
pub parameter_count: usize,
@@ -156,10 +194,13 @@ impl Rule for UseMaxParams {
156194
let node = ctx.query();
157195
let options = ctx.options();
158196

197+
// Use the parameter list range if available, otherwise fall back to the whole node
198+
let range = node.parameter_range().unwrap_or_else(|| node.range());
199+
159200
Some(
160201
RuleDiagnostic::new(
161202
rule_category!(),
162-
node.range(),
203+
range,
163204
markup! {
164205
"Function has "{state.parameter_count}" parameters, but only "{options.max}" are allowed."
165206
},

crates/biome_js_analyze/tests/specs/nursery/useMaxParams/invalid.js.snap

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@ function withThisParam(this, a, b, c, d, e, f, g) {
5050

5151
# Diagnostics
5252
```
53-
invalid.js:1:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
53+
invalid.js:1:23 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5454
5555
! Function has 8 parameters, but only 4 are allowed.
5656
5757
> 1 │ function tooManyParams(a, b, c, d, e, f, g, h) {
58-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59-
> 2return a + b + c + d + e + f + g + h;
60-
> 3}
61-
│ ^
62-
4 │
63-
5 │ function namedFunction(a, b, c, d, e, f, g, h, i) {
58+
^^^^^^^^^^^^^^^^^^^^^^^^
59+
2return a + b + c + d + e + f + g + h;
60+
3}
6461
6562
i Functions with many parameters are hard to read and maintain.
6663
@@ -70,19 +67,16 @@ invalid.js:1:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━
7067
```
7168

7269
```
73-
invalid.js:5:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
70+
invalid.js:5:23 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7471
7572
! Function has 9 parameters, but only 4 are allowed.
7673
7774
3 │ }
7875
4 │
7976
> 5 │ function namedFunction(a, b, c, d, e, f, g, h, i) {
80-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
81-
> 6return a + b + c + d + e + f + g + h + i;
82-
> 7}
83-
│ ^
84-
8 │
85-
9 │ const fn1 = function(a, b, c, d, e, f, g, h) {
77+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
6return a + b + c + d + e + f + g + h + i;
79+
7}
8680
8781
i Functions with many parameters are hard to read and maintain.
8882
@@ -92,19 +86,16 @@ invalid.js:5:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━
9286
```
9387

9488
```
95-
invalid.js:9:13 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
89+
invalid.js:9:21 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
9690
9791
! Function has 8 parameters, but only 4 are allowed.
9892
9993
7 │ }
10094
8 │
10195
> 9 │ const fn1 = function(a, b, c, d, e, f, g, h) {
102-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103-
> 10return a + b + c + d + e + f + g + h;
104-
> 11};
105-
│ ^
106-
12 │
107-
13 │ const fn2 = function namedFnExpression(a, b, c, d, e, f, g, h, i) {
96+
^^^^^^^^^^^^^^^^^^^^^^^^
97+
10return a + b + c + d + e + f + g + h;
98+
11};
10899
109100
i Functions with many parameters are hard to read and maintain.
110101
@@ -114,19 +105,16 @@ invalid.js:9:13 lint/nursery/useMaxParams ━━━━━━━━━━━━
114105
```
115106

116107
```
117-
invalid.js:13:13 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
108+
invalid.js:13:39 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
118109
119110
! Function has 9 parameters, but only 4 are allowed.
120111
121112
11 │ };
122113
12 │
123114
> 13 │ const fn2 = function namedFnExpression(a, b, c, d, e, f, g, h, i) {
124-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125-
> 14return a + b + c + d + e + f + g + h + i;
126-
> 15};
127-
│ ^
128-
16 │
129-
17 │ const arrow1 = (a, b, c, d, e, f, g, h) => {
115+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
14return a + b + c + d + e + f + g + h + i;
117+
15};
130118
131119
i Functions with many parameters are hard to read and maintain.
132120
@@ -143,12 +131,9 @@ invalid.js:17:16 lint/nursery/useMaxParams ━━━━━━━━━━━━
143131
15 │ };
144132
16 │
145133
> 17 │ const arrow1 = (a, b, c, d, e, f, g, h) => {
146-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147-
> 18return a + b + c + d + e + f + g + h;
148-
> 19};
149-
│ ^
150-
20 │
151-
21 │ const arrow2 = (a, b, c, d, e, f, g, h, i) => a + b + c + d + e + f + g + h + i;
134+
^^^^^^^^^^^^^^^^^^^^^^^^
135+
18return a + b + c + d + e + f + g + h;
136+
19};
152137
153138
i Functions with many parameters are hard to read and maintain.
154139
@@ -165,7 +150,7 @@ invalid.js:21:16 lint/nursery/useMaxParams ━━━━━━━━━━━━
165150
19 │ };
166151
20 │
167152
> 21 │ const arrow2 = (a, b, c, d, e, f, g, h, i) => a + b + c + d + e + f + g + h + i;
168-
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
153+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
169154
22 │
170155
23 │ class MyClass {
171156
@@ -177,18 +162,15 @@ invalid.js:21:16 lint/nursery/useMaxParams ━━━━━━━━━━━━
177162
```
178163
179164
```
180-
invalid.js:24:5 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
165+
invalid.js:24:11 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
181166
182167
! Function has 8 parameters, but only 4 are allowed.
183168
184169
23class MyClass {
185170
> 24 │ method(a, b, c, d, e, f, g, h) {
186-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
187-
> 25return a + b + c + d + e + f + g + h;
188-
> 26 │ }
189-
^
190-
27
191-
28constructor(a, b, c, d, e, f, g, h, i) {
171+
^^^^^^^^^^^^^^^^^^^^^^^^
172+
25return a + b + c + d + e + f + g + h;
173+
26 │ }
192174
193175
i Functions with many parameters are hard to read and maintain.
194176
@@ -198,19 +180,16 @@ invalid.js:24:5 lint/nursery/useMaxParams ━━━━━━━━━━━━
198180
```
199181
200182
```
201-
invalid.js:28:5 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
183+
invalid.js:28:16 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
202184
203185
! Function has 9 parameters, but only 4 are allowed.
204186
205187
26 │ }
206188
27
207189
> 28constructor(a, b, c, d, e, f, g, h, i) {
208-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
209-
> 29this.sum = a + b + c + d + e + f + g + h + i;
210-
> 30 │ }
211-
^
212-
31 │ }
213-
32
190+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
191+
29 │ this.sum = a + b + c + d + e + f + g + h + i;
192+
30 │ }
214193
215194
i Functions with many parameters are hard to read and maintain.
216195
@@ -220,18 +199,15 @@ invalid.js:28:5 lint/nursery/useMaxParams ━━━━━━━━━━━━
220199
```
221200
222201
```
223-
invalid.js:34:5 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
202+
invalid.js:34:11 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
224203
225204
! Function has 8 parameters, but only 4 are allowed.
226205
227206
33const obj = {
228207
> 34 │ method(a, b, c, d, e, f, g, h) {
229-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
230-
> 35return a + b + c + d + e + f + g + h;
231-
> 36 │ }
232-
│ ^
233-
37 │ };
234-
38
208+
^^^^^^^^^^^^^^^^^^^^^^^^
209+
35return a + b + c + d + e + f + g + h;
210+
36 │ }
235211
236212
i Functions with many parameters are hard to read and maintain.
237213
@@ -241,18 +217,16 @@ invalid.js:34:5 lint/nursery/useMaxParams ━━━━━━━━━━━━
241217
```
242218
243219
```
244-
invalid.js:39:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
220+
invalid.js:39:23 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
245221
246222
! Function has 8 parameters, but only 4 are allowed.
247223
248224
37 │ };
249225
38
250226
> 39function withThisParam(this, a, b, c, d, e, f, g) {
251-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
252-
> 40return a + b + c + d + e + f + g;
253-
> 41}
254-
│ ^
255-
42 │
227+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
228+
40return a + b + c + d + e + f + g;
229+
41 │ }
256230
257231
i Functions with many parameters are hard to read and maintain.
258232

crates/biome_js_analyze/tests/specs/nursery/useMaxParams/invalid.ts.snap

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ type sum = (a: number, b: number, c: number, d: number, e: number) => number;
2424

2525
# Diagnostics
2626
```
27-
invalid.ts:5:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
27+
invalid.ts:5:23 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2828
2929
! Function has 6 parameters, but only 4 are allowed.
3030
3131
3 │ }
3232
4 │
3333
> 5 │ function withThisParam(this: MyInterface, a: number, b: number, c: number, d: number, e: number): number {
34-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35-
> 6return this.value + a + b + c + d + e;
36-
> 7}
37-
│ ^
38-
8 │
39-
9 │ function tooManyParamsWithThis(this: MyInterface, a: number, b: number, c: number, d: number, e: number, f: number): number {
34+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
6return this.value + a + b + c + d + e;
36+
7}
4037
4138
i Functions with many parameters are hard to read and maintain.
4239
@@ -46,19 +43,16 @@ invalid.ts:5:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━
4643
```
4744

4845
```
49-
invalid.ts:9:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
46+
invalid.ts:9:31 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5047
5148
! Function has 7 parameters, but only 4 are allowed.
5249
5350
7 │ }
5451
8 │
5552
> 9 │ function tooManyParamsWithThis(this: MyInterface, a: number, b: number, c: number, d: number, e: number, f: number): number {
56-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57-
> 10return this.value + a + b + c + d + e + f;
58-
> 11}
59-
│ ^
60-
12 │
61-
13 │ declare function makeDate(m: number, d: number, y: number, h: number, min: number, s: number): Date;
53+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
10return this.value + a + b + c + d + e + f;
55+
11}
6256
6357
i Functions with many parameters are hard to read and maintain.
6458
@@ -68,14 +62,14 @@ invalid.ts:9:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━
6862
```
6963

7064
```
71-
invalid.ts:13:9 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
65+
invalid.ts:13:26 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7266
7367
! Function has 6 parameters, but only 4 are allowed.
7468
7569
11 │ }
7670
12 │
7771
> 13 │ declare function makeDate(m: number, d: number, y: number, h: number, min: number, s: number): Date;
78-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7973
14 │
8074
15 │ type sum = (a: number, b: number, c: number, d: number, e: number) => number;
8175
@@ -87,14 +81,14 @@ invalid.ts:13:9 lint/nursery/useMaxParams ━━━━━━━━━━━━
8781
```
8882

8983
```
90-
invalid.ts:15:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
84+
invalid.ts:15:12 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
9185
9286
! Function has 5 parameters, but only 4 are allowed.
9387
9488
13 │ declare function makeDate(m: number, d: number, y: number, h: number, min: number, s: number): Date;
9589
14 │
9690
> 15 │ type sum = (a: number, b: number, c: number, d: number, e: number) => number;
97-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9892
16 │
9993
10094
i Functions with many parameters are hard to read and maintain.

crates/biome_js_analyze/tests/specs/nursery/useMaxParams/invalidCustomMax.js.snap

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ function twoParams(a, b) {
1212

1313
# Diagnostics
1414
```
15-
invalidCustomMax.js:2:1 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
15+
invalidCustomMax.js:2:19 lint/nursery/useMaxParams ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1616
1717
! Function has 2 parameters, but only 1 are allowed.
1818
1919
1 │ // should generate diagnostics - function exceeds max of 1
2020
> 2 │ function twoParams(a, b) {
21-
^^^^^^^^^^^^^^^^^^^^^^^^^^
22-
> 3return a + b;
23-
> 4}
24-
│ ^
21+
^^^^^^
22+
3return a + b;
23+
4}
2524
2625
i Functions with many parameters are hard to read and maintain.
2726

0 commit comments

Comments
 (0)