Skip to content

Commit 9965676

Browse files
authored
fix(linter/exhaustive-deps): ignore empty strings passed to additionalHooks option (#14464)
### Issue Passing an empty string to **additionalHooks** in the **react-hooks/exhaustive-deps** rule: `"react-hooks/exhaustive-deps": ["error", { "additionalHooks": "" }],` causes the linter to consider all functions to be additionalHooks. ### Behaviour in ESLint Passing an empty string doesn't do anything. Passing `"()"` causes the same problematic behaviour: `"react-hooks/exhaustive-deps": ["warn", { additionalHooks: "()" }],` The same is true for oxlint. ### Suggested fix Check for empty string. Since ESLint is able to take an empty string without breaking - so should oxlint. Ideally - handle the "()" case too, or leave it as is to match original behaviour. ### How to test Add: `"react-hooks/exhaustive-deps": ["error", { "additionalHooks": "" }],` to your .oxlintrc.json file Lint the codebase. Example: ``` function MyComponent(props) { const foo = bar.add(); } ``` ``` ⚠ eslint-plugin-react-hooks(exhaustive-deps): React hook add requires an effect callback. ╭─[exhaustive_deps.tsx:2:23] 1 │ function MyComponent(props) { 2 │ const foo = bar.add(); · ───────── 3 │ } ╰──── help: Did you forget to pass a callback to the hook? -------- rule config -------- [ { "additionalHooks": "" } ] ```
1 parent a8d542b commit 9965676

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

crates/oxc_linter/src/rules/react/exhaustive_deps.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ impl Rule for ExhaustiveDeps {
277277
.map(|config_json| ExhaustiveDepsConfig {
278278
additional_hooks: config_json
279279
.additional_hooks
280+
.filter(|pattern| !pattern.is_empty())
280281
.and_then(|pattern| Regex::new(&pattern).ok()),
281282
})
282283
.unwrap_or_default();
@@ -4128,6 +4129,14 @@ fn test() {
41284129
Some(serde_json::json!([{ "additionalHooks": "useSpecialEffect" }])),
41294130
)];
41304131

4132+
let pass_additional_hooks_empty_string = vec![(
4133+
"function MyComponent(props) {
4134+
const foo = bar.add();
4135+
}
4136+
",
4137+
Some(serde_json::json!([{ "additionalHooks": "" }])),
4138+
)];
4139+
41314140
let fail_additional_hooks = vec![(
41324141
"function MyComponent() {
41334142
const [state, setState] = React.useState<number>(0);
@@ -4269,7 +4278,11 @@ fn test() {
42694278
Tester::new(
42704279
ExhaustiveDeps::NAME,
42714280
ExhaustiveDeps::PLUGIN,
4272-
pass.iter().map(|&code| (code, None)).chain(pass_additional_hooks).collect::<Vec<_>>(),
4281+
pass.iter()
4282+
.map(|&code| (code, None))
4283+
.chain(pass_additional_hooks)
4284+
.chain(pass_additional_hooks_empty_string)
4285+
.collect::<Vec<_>>(),
42734286
fail.iter().map(|&code| (code, None)).chain(fail_additional_hooks).collect::<Vec<_>>(),
42744287
)
42754288
.expect_fix(fix)

0 commit comments

Comments
 (0)