Skip to content

Commit cc5595e

Browse files
committed
fix(linter/react-hooks): fix diagnostic message for literal in dependency array
1 parent f3b61d8 commit cc5595e

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ fn dependency_array_not_array_literal_diagnostic(hook_name: &str, span: Span) ->
119119
.with_error_code_scope(SCOPE)
120120
}
121121

122+
fn literal_in_dependency_array_diagnostic(span: Span) -> OxcDiagnostic {
123+
OxcDiagnostic::warn("The literal is not a valid dependency because it never changes.")
124+
.with_label(span)
125+
.with_help("Remove the literal from the array.")
126+
.with_error_code_scope(SCOPE)
127+
}
128+
122129
fn duplicate_dependency_diagnostic(span: Span) -> OxcDiagnostic {
123130
OxcDiagnostic::warn("This dependency is specified more than once in the dependency array.")
124131
.with_label(span)
@@ -530,13 +537,15 @@ impl Rule for ExhaustiveDeps {
530537

531538
if let Ok(dep) = analyze_property_chain(elem, ctx) {
532539
dep
540+
} else if elem.is_literal() {
541+
ctx.diagnostic(literal_in_dependency_array_diagnostic(elem.span()));
533542
} else {
534543
ctx.diagnostic(complex_expression_in_dependency_array_diagnostic(
535544
hook_name.as_str(),
536545
elem.span(),
537546
));
538-
None
539547
}
548+
None
540549
}
541550
});
542551

crates/oxc_linter/src/snapshots/react_exhaustive_deps.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,32 +420,32 @@ source: crates/oxc_linter/src/tester.rs
420420
╰────
421421
help: Either include it or remove the dependency array.
422422

423-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
423+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
424424
╭─[exhaustive_deps.tsx:2:32]
425425
1function MyComponent() {
426426
2useEffect(() => {}, ['foo']);
427427
· ─────
428428
3 │ }
429429
╰────
430-
help: Extract the expression to a separate variable so it can be statically checked.
430+
help: Remove the literal from the array.
431431

432-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
432+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
433433
╭─[exhaustive_deps.tsx:4:15]
434434
3console.log(foo, bar, baz);
435435
4 │ }, ['foo', 'bar']);
436436
· ─────
437437
5 │ }
438438
╰────
439-
help: Extract the expression to a separate variable so it can be statically checked.
439+
help: Remove the literal from the array.
440440

441-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
441+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
442442
╭─[exhaustive_deps.tsx:4:22]
443443
3console.log(foo, bar, baz);
444444
4 │ }, ['foo', 'bar']);
445445
· ─────
446446
5 │ }
447447
╰────
448-
help: Extract the expression to a separate variable so it can be statically checked.
448+
help: Remove the literal from the array.
449449

450450
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'bar', 'foo', and 'baz'
451451
╭─[exhaustive_deps.tsx:4:14]
@@ -458,32 +458,32 @@ source: crates/oxc_linter/src/tester.rs
458458
╰────
459459
help: Either include it or remove the dependency array.
460460

461-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
461+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
462462
╭─[exhaustive_deps.tsx:4:15]
463463
3console.log(foo, bar, baz);
464464
4 │ }, [42, false, null]);
465465
· ──
466466
5 │ }
467467
╰────
468-
help: Extract the expression to a separate variable so it can be statically checked.
468+
help: Remove the literal from the array.
469469

470-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
470+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
471471
╭─[exhaustive_deps.tsx:4:19]
472472
3console.log(foo, bar, baz);
473473
4 │ }, [42, false, null]);
474474
· ─────
475475
5 │ }
476476
╰────
477-
help: Extract the expression to a separate variable so it can be statically checked.
477+
help: Remove the literal from the array.
478478

479-
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a complex expression in the dependency array.
479+
eslint-plugin-react-hooks(exhaustive-deps): The literal is not a valid dependency because it never changes.
480480
╭─[exhaustive_deps.tsx:4:26]
481481
3console.log(foo, bar, baz);
482482
4 │ }, [42, false, null]);
483483
· ────
484484
5 │ }
485485
╰────
486-
help: Extract the expression to a separate variable so it can be statically checked.
486+
help: Remove the literal from the array.
487487

488488
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'bar', 'foo', and 'baz'
489489
╭─[exhaustive_deps.tsx:4:14]

0 commit comments

Comments
 (0)