Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdcc934

Browse files
authored
fix(lint): enable rules via dependencies when setting domains in config (#8658)
1 parent 6742246 commit bdcc934

5 files changed

Lines changed: 368 additions & 7 deletions

File tree

.changeset/nice-fans-beg.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+
When the `domains` field is set in the configuration file, domains is now automatically enabled when Biome detects certain dependencies in `package.json`.

crates/biome_cli/tests/cases/rules_via_dependencies.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,116 @@ function Component2() {
216216
));
217217
}
218218

219+
#[test]
220+
fn enables_rules_via_dependencies_but_disable_domain_from_config() {
221+
let mut console = BufferConsole::default();
222+
let mut fs = TemporaryFs::new("enables_rules_via_dependencies_but_disable_domain_from_config");
223+
fs.create_file(
224+
"package.json",
225+
r#"{
226+
"dependencies": {
227+
"react": "^18.0.0"
228+
}
229+
}
230+
"#,
231+
);
232+
233+
let content = r#"
234+
import { useEffect, useState } from "react";
235+
236+
function Component2() {
237+
const [local, setLocal] = useState(0);
238+
useEffect(() => {
239+
console.log(local);
240+
}, []);
241+
}
242+
"#;
243+
fs.create_file("test.jsx", content);
244+
245+
fs.create_file(
246+
"biome.json",
247+
r#"{
248+
"linter": {
249+
"domains": {
250+
"react": "none"
251+
}
252+
}
253+
}
254+
"#,
255+
);
256+
257+
let result = run_cli_with_dyn_fs(
258+
Box::new(fs.create_os()),
259+
&mut console,
260+
Args::from(["lint", fs.cli_path()].as_slice()),
261+
);
262+
263+
assert!(result.is_ok(), "run_cli returned {result:?}");
264+
265+
assert_cli_snapshot(SnapshotPayload::new(
266+
module_path!(),
267+
"enables_rules_via_dependencies_but_disable_domain_from_config",
268+
fs.create_mem(),
269+
console,
270+
result,
271+
));
272+
}
273+
274+
#[test]
275+
fn enables_rules_via_dependencies_when_setting_domains_in_config() {
276+
let mut console = BufferConsole::default();
277+
let mut fs = TemporaryFs::new("enables_rules_via_dependencies_when_setting_domains_in_config");
278+
fs.create_file(
279+
"package.json",
280+
r#"{
281+
"dependencies": {
282+
"react": "^18.0.0"
283+
}
284+
}
285+
"#,
286+
);
287+
288+
let content = r#"
289+
import { useEffect, useState } from "react";
290+
291+
function Component2() {
292+
const [local, setLocal] = useState(0);
293+
useEffect(() => {
294+
console.log(local);
295+
}, []);
296+
}
297+
"#;
298+
fs.create_file("test.jsx", content);
299+
300+
fs.create_file(
301+
"biome.json",
302+
r#"{
303+
"linter": {
304+
"domains": {
305+
"test": "recommended"
306+
}
307+
}
308+
}
309+
"#,
310+
);
311+
312+
let result = run_cli_with_dyn_fs(
313+
Box::new(fs.create_os()),
314+
&mut console,
315+
Args::from(["lint", fs.cli_path()].as_slice()),
316+
);
317+
318+
assert!(result.is_err(), "run_cli returned {result:?}");
319+
320+
assert_cli_snapshot(SnapshotPayload::new(
321+
module_path!(),
322+
"enables_rules_via_dependencies_when_setting_domains_in_config",
323+
fs.create_mem(),
324+
console,
325+
result,
326+
));
327+
}
328+
219329
#[test]
220330
fn enables_next_rules_via_dependencies() {
221331
let mut console = BufferConsole::default();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
source: crates/biome_cli/tests/snap_test.rs
3+
expression: redactor(content)
4+
---
5+
## `biome.json`
6+
7+
```json
8+
{
9+
"linter": {
10+
"domains": {
11+
"react": "none"
12+
}
13+
}
14+
}
15+
```
16+
17+
## `package.json`
18+
19+
```json
20+
{
21+
"dependencies": {
22+
"react": "^18.0.0"
23+
}
24+
}
25+
26+
```
27+
28+
## `test.jsx`
29+
30+
```jsx
31+
32+
import { useEffect, useState } from "react";
33+
34+
function Component2() {
35+
const [local, setLocal] = useState(0);
36+
useEffect(() => {
37+
console.log(local);
38+
}, []);
39+
}
40+
41+
```
42+
43+
# Emitted Messages
44+
45+
```block
46+
test.jsx:4:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
47+
48+
! This function Component2 is unused.
49+
50+
2 │ import { useEffect, useState } from "react";
51+
3 │
52+
> 4 │ function Component2() {
53+
^^^^^^^^^^
54+
5const [local, setLocal] = useState(0);
55+
6useEffect(() => {
56+
57+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
58+
59+
i Unsafe fix: If this is intentional, prepend Component2 with an underscore.
60+
61+
2 2import { useEffect, useState } from "react";
62+
3 3
63+
4- function·Component2()·{
64+
4+ function·_Component2()·{
65+
5 5const [local, setLocal] = useState(0);
66+
6 6useEffect(() => {
67+
68+
69+
```
70+
71+
```block
72+
test.jsx:5:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
73+
74+
! This variable setLocal is unused.
75+
76+
4function Component2() {
77+
> 5const [local, setLocal] = useState(0);
78+
^^^^^^^^
79+
6useEffect(() => {
80+
7console.log(local);
81+
82+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
83+
84+
i Unsafe fix: If this is intentional, prepend setLocal with an underscore.
85+
86+
3 3
87+
4 4function Component2() {
88+
5- ····const·[local,·setLocal]·=·useState(0);
89+
5+ ····const·[local,·_setLocal]·=·useState(0);
90+
6 6useEffect(() => {
91+
7 7console.log(local);
92+
93+
94+
```
95+
96+
```block
97+
Checked 3 files in <TIME>. No fixes applied.
98+
Found 2 warnings.
99+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
source: crates/biome_cli/tests/snap_test.rs
3+
expression: redactor(content)
4+
---
5+
## `biome.json`
6+
7+
```json
8+
{
9+
"linter": {
10+
"domains": {
11+
"test": "recommended"
12+
}
13+
}
14+
}
15+
```
16+
17+
## `package.json`
18+
19+
```json
20+
{
21+
"dependencies": {
22+
"react": "^18.0.0"
23+
}
24+
}
25+
26+
```
27+
28+
## `test.jsx`
29+
30+
```jsx
31+
32+
import { useEffect, useState } from "react";
33+
34+
function Component2() {
35+
const [local, setLocal] = useState(0);
36+
useEffect(() => {
37+
console.log(local);
38+
}, []);
39+
}
40+
41+
```
42+
43+
# Termination Message
44+
45+
```block
46+
lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
47+
48+
× Some errors were emitted while running checks.
49+
50+
51+
52+
```
53+
54+
# Emitted Messages
55+
56+
```block
57+
test.jsx:4:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
58+
59+
! This function Component2 is unused.
60+
61+
2 │ import { useEffect, useState } from "react";
62+
3 │
63+
> 4 │ function Component2() {
64+
^^^^^^^^^^
65+
5const [local, setLocal] = useState(0);
66+
6useEffect(() => {
67+
68+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
69+
70+
i Unsafe fix: If this is intentional, prepend Component2 with an underscore.
71+
72+
2 2import { useEffect, useState } from "react";
73+
3 3
74+
4- function·Component2()·{
75+
4+ function·_Component2()·{
76+
5 5const [local, setLocal] = useState(0);
77+
6 6useEffect(() => {
78+
79+
80+
```
81+
82+
```block
83+
test.jsx:5:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
84+
85+
! This variable setLocal is unused.
86+
87+
4function Component2() {
88+
> 5const [local, setLocal] = useState(0);
89+
^^^^^^^^
90+
6useEffect(() => {
91+
7console.log(local);
92+
93+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
94+
95+
i Unsafe fix: If this is intentional, prepend setLocal with an underscore.
96+
97+
3 3
98+
4 4function Component2() {
99+
5- ····const·[local,·setLocal]·=·useState(0);
100+
5+ ····const·[local,·_setLocal]·=·useState(0);
101+
6 6useEffect(() => {
102+
7 7console.log(local);
103+
104+
105+
```
106+
107+
```block
108+
test.jsx:6:5 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
109+
110+
× This hook does not specify its dependency on local.
111+
112+
4function Component2() {
113+
5const [local, setLocal] = useState(0);
114+
> 6useEffect(() => {
115+
^^^^^^^^^
116+
7console.log(local);
117+
8 │ }, []);
118+
119+
i This dependency is being used here, but is not specified in the hook dependency list.
120+
121+
5const [local, setLocal] = useState(0);
122+
6useEffect(() => {
123+
> 7console.log(local);
124+
^^^^^
125+
8 │ }, []);
126+
9 │ }
127+
128+
i React relies on hook dependencies to determine when to re-compute Effects.
129+
Failing to specify dependencies can result in Effects not updating correctly when state changes.
130+
These "stale closures" are a common source of surprising bugs.
131+
132+
i Either include it or remove the dependency array.
133+
134+
i Unsafe fix: Add the missing dependency to the list.
135+
136+
8 │ ····},·[local]);
137+
+++++
138+
139+
```
140+
141+
```block
142+
Checked 3 files in <TIME>. No fixes applied.
143+
Found 1 error.
144+
Found 2 warnings.
145+
```

0 commit comments

Comments
 (0)