diff --git a/.changeset/nice-fans-beg.md b/.changeset/nice-fans-beg.md new file mode 100644 index 000000000000..31e844cf1dff --- /dev/null +++ b/.changeset/nice-fans-beg.md @@ -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`. diff --git a/crates/biome_cli/tests/cases/rules_via_dependencies.rs b/crates/biome_cli/tests/cases/rules_via_dependencies.rs index eb6421894fc4..9463a0289d50 100644 --- a/crates/biome_cli/tests/cases/rules_via_dependencies.rs +++ b/crates/biome_cli/tests/cases/rules_via_dependencies.rs @@ -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(); diff --git a/crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_but_disable_domain_from_config.snap b/crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_but_disable_domain_from_config.snap new file mode 100644 index 000000000000..674db21e9d68 --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_but_disable_domain_from_config.snap @@ -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