Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-fans-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

When the `domains` field is set in the configuration file, domains is now automatically enabled when Biome detects certain dependencies in `package.json`.
110 changes: 110 additions & 0 deletions crates/biome_cli/tests/cases/rules_via_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,116 @@ function Component2() {
));
}

#[test]
fn enables_rules_via_dependencies_but_disable_domain_from_config() {
let mut console = BufferConsole::default();
let mut fs = TemporaryFs::new("enables_rules_via_dependencies_but_disable_domain_from_config");
fs.create_file(
"package.json",
r#"{
"dependencies": {
"react": "^18.0.0"
}
}
"#,
);

let content = r#"
import { useEffect, useState } from "react";

function Component2() {
const [local, setLocal] = useState(0);
useEffect(() => {
console.log(local);
}, []);
}
"#;
fs.create_file("test.jsx", content);

fs.create_file(
"biome.json",
r#"{
"linter": {
"domains": {
"react": "none"
}
}
}
"#,
);

let result = run_cli_with_dyn_fs(
Box::new(fs.create_os()),
&mut console,
Args::from(["lint", fs.cli_path()].as_slice()),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"enables_rules_via_dependencies_but_disable_domain_from_config",
fs.create_mem(),
console,
result,
));
}

#[test]
fn enables_rules_via_dependencies_when_setting_domains_in_config() {
let mut console = BufferConsole::default();
let mut fs = TemporaryFs::new("enables_rules_via_dependencies_when_setting_domains_in_config");
fs.create_file(
"package.json",
r#"{
"dependencies": {
"react": "^18.0.0"
}
}
"#,
);

let content = r#"
import { useEffect, useState } from "react";

function Component2() {
const [local, setLocal] = useState(0);
useEffect(() => {
console.log(local);
}, []);
}
"#;
fs.create_file("test.jsx", content);

fs.create_file(
"biome.json",
r#"{
"linter": {
"domains": {
"test": "recommended"
}
}
}
"#,
);

let result = run_cli_with_dyn_fs(
Box::new(fs.create_os()),
&mut console,
Args::from(["lint", fs.cli_path()].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"enables_rules_via_dependencies_when_setting_domains_in_config",
fs.create_mem(),
console,
result,
));
}

#[test]
fn enables_next_rules_via_dependencies() {
let mut console = BufferConsole::default();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
---
## `biome.json`

```json
{
"linter": {
"domains": {
"react": "none"
}
}
}
```

## `package.json`

```json
{
"dependencies": {
"react": "^18.0.0"
}
}

```

## `test.jsx`

```jsx

import { useEffect, useState } from "react";

function Component2() {
const [local, setLocal] = useState(0);
useEffect(() => {
console.log(local);
}, []);
}

```

# Emitted Messages

```block
test.jsx:4:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This function Component2 is unused.

2 │ import { useEffect, useState } from "react";
3 │
> 4 │ function Component2() {
│ ^^^^^^^^^^
5 │ const [local, setLocal] = useState(0);
6 │ useEffect(() => {

i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.

i Unsafe fix: If this is intentional, prepend Component2 with an underscore.

2 2 │ import { useEffect, useState } from "react";
3 3 │
4 │ - function·Component2()·{
4 │ + function·_Component2()·{
5 5 │ const [local, setLocal] = useState(0);
6 6 │ useEffect(() => {


```

```block
test.jsx:5:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable setLocal is unused.

4 │ function Component2() {
> 5 │ const [local, setLocal] = useState(0);
│ ^^^^^^^^
6 │ useEffect(() => {
7 │ console.log(local);

i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.

i Unsafe fix: If this is intentional, prepend setLocal with an underscore.

3 3 │
4 4 │ function Component2() {
5 │ - ····const·[local,·setLocal]·=·useState(0);
5 │ + ····const·[local,·_setLocal]·=·useState(0);
6 6 │ useEffect(() => {
7 7 │ console.log(local);


```

```block
Checked 3 files in <TIME>. No fixes applied.
Found 2 warnings.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
---
## `biome.json`

```json
{
"linter": {
"domains": {
"test": "recommended"
}
}
}
```

## `package.json`

```json
{
"dependencies": {
"react": "^18.0.0"
}
}

```

## `test.jsx`

```jsx

import { useEffect, useState } from "react";

function Component2() {
const [local, setLocal] = useState(0);
useEffect(() => {
console.log(local);
}, []);
}

```

# Termination Message

```block
lint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Some errors were emitted while running checks.



```

# Emitted Messages

```block
test.jsx:4:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This function Component2 is unused.

2 │ import { useEffect, useState } from "react";
3 │
> 4 │ function Component2() {
│ ^^^^^^^^^^
5 │ const [local, setLocal] = useState(0);
6 │ useEffect(() => {

i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.

i Unsafe fix: If this is intentional, prepend Component2 with an underscore.

2 2 │ import { useEffect, useState } from "react";
3 3 │
4 │ - function·Component2()·{
4 │ + function·_Component2()·{
5 5 │ const [local, setLocal] = useState(0);
6 6 │ useEffect(() => {


```

```block
test.jsx:5:19 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This variable setLocal is unused.

4 │ function Component2() {
> 5 │ const [local, setLocal] = useState(0);
│ ^^^^^^^^
6 │ useEffect(() => {
7 │ console.log(local);

i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.

i Unsafe fix: If this is intentional, prepend setLocal with an underscore.

3 3 │
4 4 │ function Component2() {
5 │ - ····const·[local,·setLocal]·=·useState(0);
5 │ + ····const·[local,·_setLocal]·=·useState(0);
6 6 │ useEffect(() => {
7 7 │ console.log(local);


```

```block
test.jsx:6:5 lint/correctness/useExhaustiveDependencies FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× This hook does not specify its dependency on local.

4 │ function Component2() {
5 │ const [local, setLocal] = useState(0);
> 6 │ useEffect(() => {
│ ^^^^^^^^^
7 │ console.log(local);
8 │ }, []);

i This dependency is being used here, but is not specified in the hook dependency list.

5 │ const [local, setLocal] = useState(0);
6 │ useEffect(() => {
> 7 │ console.log(local);
│ ^^^^^
8 │ }, []);
9 │ }

i React relies on hook dependencies to determine when to re-compute Effects.
Failing to specify dependencies can result in Effects not updating correctly when state changes.
These "stale closures" are a common source of surprising bugs.

i Either include it or remove the dependency array.

i Unsafe fix: Add the missing dependency to the list.

8 │ ····},·[local]);
│ +++++

```

```block
Checked 3 files in <TIME>. No fixes applied.
Found 1 error.
Found 2 warnings.
```
Loading