From b4cf3e06ea077c24938908123d632cc53019ef17 Mon Sep 17 00:00:00 2001 From: rksvc Date: Sat, 3 Jan 2026 11:00:57 +0800 Subject: [PATCH 1/4] fix(lint): enable rules via dependencies when setting domains in config --- .changeset/nice-fans-beg.md | 5 + .../tests/cases/rules_via_dependencies.rs | 55 +++++++ ...encies_when_setting_domains_in_config.snap | 145 ++++++++++++++++++ crates/biome_service/src/file_handlers/mod.rs | 17 +- 4 files changed, 215 insertions(+), 7 deletions(-) create mode 100644 .changeset/nice-fans-beg.md create mode 100644 crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_when_setting_domains_in_config.snap diff --git a/.changeset/nice-fans-beg.md b/.changeset/nice-fans-beg.md new file mode 100644 index 000000000000..f78f93bbe555 --- /dev/null +++ b/.changeset/nice-fans-beg.md @@ -0,0 +1,5 @@ +--- +"@biomejs/biome": patch +--- + +Enabled rules via dependencies in `package.json` file when setting `domains` in configuration file. diff --git a/crates/biome_cli/tests/cases/rules_via_dependencies.rs b/crates/biome_cli/tests/cases/rules_via_dependencies.rs index eb6421894fc4..8075d4628d4c 100644 --- a/crates/biome_cli/tests/cases/rules_via_dependencies.rs +++ b/crates/biome_cli/tests/cases/rules_via_dependencies.rs @@ -150,6 +150,61 @@ function Component2() { )); } +#[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_when_setting_domains_in_config.snap b/crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_when_setting_domains_in_config.snap new file mode 100644 index 000000000000..09ef09531dd9 --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_cases_rules_via_dependencies/enables_rules_via_dependencies_when_setting_domains_in_config.snap @@ -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