From 2aea23c1cbe2b0fe7c8565bfa99a1a91589d2c17 Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 13:37:05 +0800 Subject: [PATCH 1/7] feat: Add option to useExhaustiveDependencies to disable reporting for unused dependencies --- crates/biome_js_analyze/src/lib.rs | 12 ++- .../use_exhaustive_dependencies.rs | 50 ++++++++----- .../reportUnnecessaryDependencies.js | 13 ++++ .../reportUnnecessaryDependencies.js.snap | 21 ++++++ ...reportUnnecessaryDependencies.options.json | 15 ++++ .../@biomejs/backend-jsonrpc/src/workspace.ts | 13 ++-- .../@biomejs/biome/configuration_schema.json | 73 ++++++++++--------- 7 files changed, 136 insertions(+), 61 deletions(-) create mode 100644 crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js create mode 100644 crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap create mode 100644 crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.options.json diff --git a/crates/biome_js_analyze/src/lib.rs b/crates/biome_js_analyze/src/lib.rs index 50b8e00f6176..f0cc4bd192d4 100644 --- a/crates/biome_js_analyze/src/lib.rs +++ b/crates/biome_js_analyze/src/lib.rs @@ -176,7 +176,9 @@ mod tests { use biome_project::{Dependencies, PackageJson}; use std::slice; - use crate::lint::correctness::use_exhaustive_dependencies::{Hook, HooksOptions}; + use crate::lint::correctness::use_exhaustive_dependencies::{ + Hook, UseExhaustiveDependenciesOptions, + }; use crate::{analyze, AnalysisFilter, ControlFlow}; #[ignore] @@ -207,7 +209,13 @@ mod tests { options.configuration.rules.push_rule( RuleKey::new("nursery", "useHookAtTopLevel"), - RuleOptions::new(HooksOptions { hooks: vec![hook] }, None), + RuleOptions::new( + UseExhaustiveDependenciesOptions { + hooks: vec![hook], + report_unnecessary_dependencies: false, + }, + None, + ), ); let mut dependencies = Dependencies::default(); diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index 32e116d7fc37..da80c98ac5b0 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -250,12 +250,12 @@ declare_lint_rule! { } #[derive(Debug, Clone)] -pub struct ReactExtensiveDependenciesOptions { +pub struct HookConfigMaps { pub(crate) hooks_config: FxHashMap, pub(crate) stable_config: FxHashSet, } -impl Default for ReactExtensiveDependenciesOptions { +impl Default for HookConfigMaps { fn default() -> Self { let hooks_config = FxHashMap::from_iter([ ("useEffect".to_string(), (0, 1, true).into()), @@ -289,15 +289,27 @@ impl Default for ReactExtensiveDependenciesOptions { } /// Options for the rule `useExhaustiveDependencies` -#[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Deserialize, Deserializable, Eq, PartialEq, Serialize)] #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] -pub struct HooksOptions { +pub struct UseExhaustiveDependenciesOptions { + // Whether to report errors when a dependency is not used in the closure function + pub report_unnecessary_dependencies: bool, + /// List of hooks of which the dependencies should be validated. #[deserializable(validate = "non_empty")] pub hooks: Vec, } +impl Default for UseExhaustiveDependenciesOptions { + fn default() -> Self { + Self { + report_unnecessary_dependencies: true, + hooks: vec![], + } + } +} + #[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)] #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] @@ -356,15 +368,15 @@ impl DeserializableValidator for Hook { } } -impl ReactExtensiveDependenciesOptions { - pub fn new(hooks: HooksOptions) -> Self { - let mut result = ReactExtensiveDependenciesOptions::default(); - for hook in hooks.hooks { - if let Some(stable_result) = hook.stable_result { - if stable_result != StableHookResult::None { +impl HookConfigMaps { + pub fn new(hooks: &UseExhaustiveDependenciesOptions) -> Self { + let mut result = HookConfigMaps::default(); + for hook in &hooks.hooks { + if let Some(stable_result) = &hook.stable_result { + if *stable_result != StableHookResult::None { result.stable_config.insert(StableReactHookConfiguration { hook_name: hook.name.clone(), - result: stable_result, + result: stable_result.clone(), builtin: false, }); } @@ -373,7 +385,7 @@ impl ReactExtensiveDependenciesOptions { (hook.closure_index, hook.dependencies_index) { result.hooks_config.insert( - hook.name, + hook.name.clone(), ReactHookConfiguration { closure_index, dependencies_index, @@ -448,7 +460,7 @@ fn capture_needs_to_be_in_the_dependency_list( capture: &Capture, component_function_range: &TextRange, model: &SemanticModel, - options: &ReactExtensiveDependenciesOptions, + options: &HookConfigMaps, ) -> bool { // Ignore if referenced in TS typeof if capture @@ -712,18 +724,20 @@ impl Rule for UseExhaustiveDependencies { type Query = Semantic; type State = Fix; type Signals = Vec; - type Options = Box; + type Options = Box; fn run(ctx: &RuleContext) -> Vec { let options = ctx.options(); - let options = ReactExtensiveDependenciesOptions::new(options.as_ref().clone()); + let hook_config_maps = HookConfigMaps::new(options); let mut signals = vec![]; let call = ctx.query(); let model = ctx.model(); - if let Some(result) = react_hook_with_dependency(call, &options.hooks_config, model) { + if let Some(result) = + react_hook_with_dependency(call, &hook_config_maps.hooks_config, model) + { let Some(component_function) = function_of_hook_call(call) else { return vec![]; }; @@ -741,7 +755,7 @@ impl Rule for UseExhaustiveDependencies { capture, &component_function_range, model, - &options, + &hook_config_maps, ) }) .map(|capture| { @@ -862,7 +876,7 @@ impl Rule for UseExhaustiveDependencies { }); } - if !excessive_deps.is_empty() { + if options.report_unnecessary_dependencies && !excessive_deps.is_empty() { signals.push(Fix::RemoveDependency { function_name_range: result.function_name_range, component_function, diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js new file mode 100644 index 000000000000..063eb0224b1f --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js @@ -0,0 +1,13 @@ +import {useEffect} from "react"; + +// should not report errors for the unused `b` when the reportUnecessaryDependencies option is false +function ReportUnecessaryDependencies() { + const [b] = useState("hello") + const [a] = useState("world"); + + useEffect(() => { + console.log(a); + }, [a, b]); + + return a; +} diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap new file mode 100644 index 000000000000..61a9f610dd75 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap @@ -0,0 +1,21 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: reportUnnecessaryDependencies.js +--- +# Input +```jsx +import {useEffect} from "react"; + +// should not report errors for the unused `b` when the reportUnecessaryDependencies option is false +function ReportUnecessaryDependencies() { + const [b] = useState("hello") + const [a] = useState("world"); + + useEffect(() => { + console.log(a); + }, [a, b]); + + return a; +} + +``` diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.options.json b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.options.json new file mode 100644 index 000000000000..5b2ee6764158 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.options.json @@ -0,0 +1,15 @@ +{ + "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", + "linter": { + "rules": { + "correctness": { + "useExhaustiveDependencies": { + "level": "error", + "options": { + "reportUnnecessaryDependencies": false + } + } + } + } + } +} diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index 81fa5a018fc5..d45c3dd5e75c 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -1184,7 +1184,7 @@ export interface Correctness { /** * Enforce all dependencies are correctly specified in a React hook. */ - useExhaustiveDependencies?: RuleConfiguration_for_HooksOptions; + useExhaustiveDependencies?: RuleConfiguration_for_UseExhaustiveDependenciesOptions; /** * Enforce that all React hooks are being called from the Top Level component functions. */ @@ -1981,9 +1981,9 @@ export type RuleFixConfiguration_for_ValidAriaRoleOptions = export type RuleConfiguration_for_ComplexityOptions = | RulePlainConfiguration | RuleWithOptions_for_ComplexityOptions; -export type RuleConfiguration_for_HooksOptions = +export type RuleConfiguration_for_UseExhaustiveDependenciesOptions = | RulePlainConfiguration - | RuleWithOptions_for_HooksOptions; + | RuleWithOptions_for_UseExhaustiveDependenciesOptions; export type RuleConfiguration_for_DeprecatedHooksOptions = | RulePlainConfiguration | RuleWithOptions_for_DeprecatedHooksOptions; @@ -2099,7 +2099,7 @@ export interface RuleWithOptions_for_ComplexityOptions { */ options: ComplexityOptions; } -export interface RuleWithOptions_for_HooksOptions { +export interface RuleWithOptions_for_UseExhaustiveDependenciesOptions { /** * The severity of the emitted diagnostics by the rule */ @@ -2107,7 +2107,7 @@ export interface RuleWithOptions_for_HooksOptions { /** * Rule's options */ - options: HooksOptions; + options: UseExhaustiveDependenciesOptions; } export interface RuleWithOptions_for_DeprecatedHooksOptions { /** @@ -2317,11 +2317,12 @@ export interface ComplexityOptions { /** * Options for the rule `useExhaustiveDependencies` */ -export interface HooksOptions { +export interface UseExhaustiveDependenciesOptions { /** * List of hooks of which the dependencies should be validated. */ hooks: Hook[]; + reportUnnecessaryDependencies: boolean; } /** * Options for the `useHookAtTopLevel` rule have been deprecated, since we now use the React hook naming convention to determine whether a function is a hook. diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index b45098e9db28..d1c515bb18b2 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -1051,7 +1051,7 @@ "useExhaustiveDependencies": { "description": "Enforce all dependencies are correctly specified in a React hook.", "anyOf": [ - { "$ref": "#/definitions/HooksConfiguration" }, + { "$ref": "#/definitions/UseExhaustiveDependenciesConfiguration" }, { "type": "null" } ] }, @@ -1485,25 +1485,6 @@ }, "additionalProperties": false }, - "HooksConfiguration": { - "anyOf": [ - { "$ref": "#/definitions/RulePlainConfiguration" }, - { "$ref": "#/definitions/RuleWithHooksOptions" } - ] - }, - "HooksOptions": { - "description": "Options for the rule `useExhaustiveDependencies`", - "type": "object", - "required": ["hooks"], - "properties": { - "hooks": { - "description": "List of hooks of which the dependencies should be validated.", - "type": "array", - "items": { "$ref": "#/definitions/Hook" } - } - }, - "additionalProperties": false - }, "IndentStyle": { "oneOf": [ { "description": "Tab", "type": "string", "enum": ["tab"] }, @@ -2706,21 +2687,6 @@ }, "additionalProperties": false }, - "RuleWithHooksOptions": { - "type": "object", - "required": ["level"], - "properties": { - "level": { - "description": "The severity of the emitted diagnostics by the rule", - "allOf": [{ "$ref": "#/definitions/RulePlainConfiguration" }] - }, - "options": { - "description": "Rule's options", - "allOf": [{ "$ref": "#/definitions/HooksOptions" }] - } - }, - "additionalProperties": false - }, "RuleWithNamingConventionOptions": { "type": "object", "required": ["level"], @@ -2870,6 +2836,23 @@ }, "additionalProperties": false }, + "RuleWithUseExhaustiveDependenciesOptions": { + "type": "object", + "required": ["level"], + "properties": { + "level": { + "description": "The severity of the emitted diagnostics by the rule", + "allOf": [{ "$ref": "#/definitions/RulePlainConfiguration" }] + }, + "options": { + "description": "Rule's options", + "allOf": [ + { "$ref": "#/definitions/UseExhaustiveDependenciesOptions" } + ] + } + }, + "additionalProperties": false + }, "RuleWithUseImportExtensionsOptions": { "type": "object", "required": ["level"], @@ -4001,6 +3984,26 @@ }, "additionalProperties": false }, + "UseExhaustiveDependenciesConfiguration": { + "anyOf": [ + { "$ref": "#/definitions/RulePlainConfiguration" }, + { "$ref": "#/definitions/RuleWithUseExhaustiveDependenciesOptions" } + ] + }, + "UseExhaustiveDependenciesOptions": { + "description": "Options for the rule `useExhaustiveDependencies`", + "type": "object", + "required": ["hooks", "reportUnnecessaryDependencies"], + "properties": { + "hooks": { + "description": "List of hooks of which the dependencies should be validated.", + "type": "array", + "items": { "$ref": "#/definitions/Hook" } + }, + "reportUnnecessaryDependencies": { "type": "boolean" } + }, + "additionalProperties": false + }, "UseImportExtensionsConfiguration": { "anyOf": [ { "$ref": "#/definitions/RulePlainConfiguration" }, From d768aabf34a3a8b0ed0d258d014e43b46c69f81b Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 14:07:32 +0800 Subject: [PATCH 2/7] Fix comment --- .../src/lint/correctness/use_exhaustive_dependencies.rs | 2 +- packages/@biomejs/backend-jsonrpc/src/workspace.ts | 3 +++ packages/@biomejs/biome/configuration_schema.json | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index da80c98ac5b0..6b57aae61902 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -293,7 +293,7 @@ impl Default for HookConfigMaps { #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UseExhaustiveDependenciesOptions { - // Whether to report errors when a dependency is not used in the closure function + /// Whether to report errors when a dependency is not used in the closure function. pub report_unnecessary_dependencies: bool, /// List of hooks of which the dependencies should be validated. diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index d45c3dd5e75c..fa682ca22b2d 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -2322,6 +2322,9 @@ export interface UseExhaustiveDependenciesOptions { * List of hooks of which the dependencies should be validated. */ hooks: Hook[]; + /** + * Whether to report errors when a dependency is not used in the closure function. + */ reportUnnecessaryDependencies: boolean; } /** diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index d1c515bb18b2..ecd08c988a6b 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -4000,7 +4000,10 @@ "type": "array", "items": { "$ref": "#/definitions/Hook" } }, - "reportUnnecessaryDependencies": { "type": "boolean" } + "reportUnnecessaryDependencies": { + "description": "Whether to report errors when a dependency is not used in the closure function.", + "type": "boolean" + } }, "additionalProperties": false }, From fb192e196767b304790566190cb2752e2dcf9710 Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 14:49:35 +0800 Subject: [PATCH 3/7] Remove unused code, make options optional --- crates/biome_js_analyze/src/lib.rs | 25 ++----------------- .../use_exhaustive_dependencies.rs | 2 ++ .../@biomejs/backend-jsonrpc/src/workspace.ts | 4 +-- .../@biomejs/biome/configuration_schema.json | 3 ++- 4 files changed, 8 insertions(+), 26 deletions(-) diff --git a/crates/biome_js_analyze/src/lib.rs b/crates/biome_js_analyze/src/lib.rs index f0cc4bd192d4..9c0866e4de42 100644 --- a/crates/biome_js_analyze/src/lib.rs +++ b/crates/biome_js_analyze/src/lib.rs @@ -164,8 +164,7 @@ where #[cfg(test)] mod tests { - use biome_analyze::options::RuleOptions; - use biome_analyze::{AnalyzerOptions, Never, RuleCategoriesBuilder, RuleFilter, RuleKey}; + use biome_analyze::{AnalyzerOptions, Never, RuleCategoriesBuilder, RuleFilter}; use biome_console::fmt::{Formatter, Termcolor}; use biome_console::{markup, Markup}; use biome_diagnostics::category; @@ -176,9 +175,6 @@ mod tests { use biome_project::{Dependencies, PackageJson}; use std::slice; - use crate::lint::correctness::use_exhaustive_dependencies::{ - Hook, UseExhaustiveDependenciesOptions, - }; use crate::{analyze, AnalysisFilter, ControlFlow}; #[ignore] @@ -198,26 +194,9 @@ mod tests { let parsed = parse(SOURCE, JsFileSource::tsx(), JsParserOptions::default()); let mut error_ranges: Vec = Vec::new(); - let mut options = AnalyzerOptions::default(); - let hook = Hook { - name: "myEffect".to_string(), - closure_index: Some(0), - dependencies_index: Some(1), - stable_result: None, - }; + let options = AnalyzerOptions::default(); let rule_filter = RuleFilter::Rule("style", "useNodejsImportProtocol"); - options.configuration.rules.push_rule( - RuleKey::new("nursery", "useHookAtTopLevel"), - RuleOptions::new( - UseExhaustiveDependenciesOptions { - hooks: vec![hook], - report_unnecessary_dependencies: false, - }, - None, - ), - ); - let mut dependencies = Dependencies::default(); dependencies.add("buffer", "latest"); analyze( diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index 6b57aae61902..9ae348901bb4 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -294,9 +294,11 @@ impl Default for HookConfigMaps { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UseExhaustiveDependenciesOptions { /// Whether to report errors when a dependency is not used in the closure function. + #[serde(default)] pub report_unnecessary_dependencies: bool, /// List of hooks of which the dependencies should be validated. + #[serde(default)] #[deserializable(validate = "non_empty")] pub hooks: Vec, } diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index fa682ca22b2d..9cf8e37d41a4 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -2321,11 +2321,11 @@ export interface UseExhaustiveDependenciesOptions { /** * List of hooks of which the dependencies should be validated. */ - hooks: Hook[]; + hooks?: Hook[]; /** * Whether to report errors when a dependency is not used in the closure function. */ - reportUnnecessaryDependencies: boolean; + reportUnnecessaryDependencies?: boolean; } /** * Options for the `useHookAtTopLevel` rule have been deprecated, since we now use the React hook naming convention to determine whether a function is a hook. diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index ecd08c988a6b..fb888b4f6340 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -3993,15 +3993,16 @@ "UseExhaustiveDependenciesOptions": { "description": "Options for the rule `useExhaustiveDependencies`", "type": "object", - "required": ["hooks", "reportUnnecessaryDependencies"], "properties": { "hooks": { "description": "List of hooks of which the dependencies should be validated.", + "default": [], "type": "array", "items": { "$ref": "#/definitions/Hook" } }, "reportUnnecessaryDependencies": { "description": "Whether to report errors when a dependency is not used in the closure function.", + "default": false, "type": "boolean" } }, From 1f7c7b65bcc79159de5dfa88cf0ff3c48cb16d12 Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 15:40:13 +0800 Subject: [PATCH 4/7] Change comment --- .../src/lint/correctness/use_exhaustive_dependencies.rs | 2 +- packages/@biomejs/backend-jsonrpc/src/workspace.ts | 2 +- packages/@biomejs/biome/configuration_schema.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index 9ae348901bb4..751895e6365b 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -293,7 +293,7 @@ impl Default for HookConfigMaps { #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UseExhaustiveDependenciesOptions { - /// Whether to report errors when a dependency is not used in the closure function. + /// Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true. #[serde(default)] pub report_unnecessary_dependencies: bool, diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index 9cf8e37d41a4..123dc8265beb 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -2323,7 +2323,7 @@ export interface UseExhaustiveDependenciesOptions { */ hooks?: Hook[]; /** - * Whether to report errors when a dependency is not used in the closure function. + * Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true. */ reportUnnecessaryDependencies?: boolean; } diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index fb888b4f6340..d294a9edad2a 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -4001,7 +4001,7 @@ "items": { "$ref": "#/definitions/Hook" } }, "reportUnnecessaryDependencies": { - "description": "Whether to report errors when a dependency is not used in the closure function.", + "description": "Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true.", "default": false, "type": "boolean" } From 0ef631f4ba3d4c934dfe95c8e57ea6af0f9631cd Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 15:58:30 +0800 Subject: [PATCH 5/7] Update comment --- .../src/lint/correctness/use_exhaustive_dependencies.rs | 2 +- packages/@biomejs/backend-jsonrpc/src/workspace.ts | 2 +- packages/@biomejs/biome/configuration_schema.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index 751895e6365b..ae50451664fa 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -293,7 +293,7 @@ impl Default for HookConfigMaps { #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UseExhaustiveDependenciesOptions { - /// Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true. + /// Whether to report an error when a dependency is listed in the dependencies array but isn't used. Defaults to true. #[serde(default)] pub report_unnecessary_dependencies: bool, diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index 123dc8265beb..ac726e7b519c 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -2323,7 +2323,7 @@ export interface UseExhaustiveDependenciesOptions { */ hooks?: Hook[]; /** - * Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true. + * Whether to report an error when a dependency is listed in the dependencies array but isn't used. Defaults to true. */ reportUnnecessaryDependencies?: boolean; } diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index d294a9edad2a..5205ecaaf381 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -4001,7 +4001,7 @@ "items": { "$ref": "#/definitions/Hook" } }, "reportUnnecessaryDependencies": { - "description": "Whether to report errors when a dependency is listed in the dependencies array but isn't used. Defaults to true.", + "description": "Whether to report an error when a dependency is listed in the dependencies array but isn't used. Defaults to true.", "default": false, "type": "boolean" } From 901af9cdd4766bad8aeeef9cc738ec57d16b678a Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Mon, 30 Sep 2024 17:53:34 +0800 Subject: [PATCH 6/7] Fix test --- .../tests/invalid/hooks_incorrect_options.json.snap | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/biome_service/tests/invalid/hooks_incorrect_options.json.snap b/crates/biome_service/tests/invalid/hooks_incorrect_options.json.snap index 5d69ad53e3dd..197288d87b84 100644 --- a/crates/biome_service/tests/invalid/hooks_incorrect_options.json.snap +++ b/crates/biome_service/tests/invalid/hooks_incorrect_options.json.snap @@ -15,7 +15,5 @@ hooks_incorrect_options.json:9:7 deserialize ━━━━━━━━━━━ i Known keys: + - reportUnnecessaryDependencies - hooks - - - From d02c9efc01d75286c3e478ea03142c244d648ffc Mon Sep 17 00:00:00 2001 From: Simon Paris Date: Tue, 1 Oct 2024 12:22:30 +0800 Subject: [PATCH 7/7] Review feedback --- .../src/lint/correctness/use_exhaustive_dependencies.rs | 8 ++++++-- .../reportUnnecessaryDependencies.js | 4 ++-- .../reportUnnecessaryDependencies.js.snap | 4 ++-- packages/@biomejs/biome/configuration_schema.json | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs index ae50451664fa..38519499846a 100644 --- a/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs +++ b/crates/biome_js_analyze/src/lint/correctness/use_exhaustive_dependencies.rs @@ -294,7 +294,7 @@ impl Default for HookConfigMaps { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct UseExhaustiveDependenciesOptions { /// Whether to report an error when a dependency is listed in the dependencies array but isn't used. Defaults to true. - #[serde(default)] + #[serde(default = "report_unnecessary_dependencies_default")] pub report_unnecessary_dependencies: bool, /// List of hooks of which the dependencies should be validated. @@ -306,12 +306,16 @@ pub struct UseExhaustiveDependenciesOptions { impl Default for UseExhaustiveDependenciesOptions { fn default() -> Self { Self { - report_unnecessary_dependencies: true, + report_unnecessary_dependencies: report_unnecessary_dependencies_default(), hooks: vec![], } } } +fn report_unnecessary_dependencies_default() -> bool { + true +} + #[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)] #[cfg_attr(feature = "schemars", derive(JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js index 063eb0224b1f..b942093ae692 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js @@ -1,7 +1,7 @@ import {useEffect} from "react"; -// should not report errors for the unused `b` when the reportUnecessaryDependencies option is false -function ReportUnecessaryDependencies() { +// should not report errors for the unused `b` when the reportUnnecessaryDependencies option is false +function ReportUnnecessaryDependencies() { const [b] = useState("hello") const [a] = useState("world"); diff --git a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap index 61a9f610dd75..ae5fa7b74aa5 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/useExhaustiveDependencies/reportUnnecessaryDependencies.js.snap @@ -6,8 +6,8 @@ expression: reportUnnecessaryDependencies.js ```jsx import {useEffect} from "react"; -// should not report errors for the unused `b` when the reportUnecessaryDependencies option is false -function ReportUnecessaryDependencies() { +// should not report errors for the unused `b` when the reportUnnecessaryDependencies option is false +function ReportUnnecessaryDependencies() { const [b] = useState("hello") const [a] = useState("world"); diff --git a/packages/@biomejs/biome/configuration_schema.json b/packages/@biomejs/biome/configuration_schema.json index 5205ecaaf381..f3ae50e5c038 100644 --- a/packages/@biomejs/biome/configuration_schema.json +++ b/packages/@biomejs/biome/configuration_schema.json @@ -4002,7 +4002,7 @@ }, "reportUnnecessaryDependencies": { "description": "Whether to report an error when a dependency is listed in the dependencies array but isn't used. Defaults to true.", - "default": false, + "default": true, "type": "boolean" } },