Skip to content

Commit bf23e37

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

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
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)
@@ -531,10 +538,14 @@ impl Rule for ExhaustiveDeps {
531538
if let Ok(dep) = analyze_property_chain(elem, ctx) {
532539
dep
533540
} else {
534-
ctx.diagnostic(complex_expression_in_dependency_array_diagnostic(
535-
hook_name.as_str(),
536-
elem.span(),
537-
));
541+
if elem.is_literal() {
542+
ctx.diagnostic(literal_in_dependency_array_diagnostic(elem.span()));
543+
} else {
544+
ctx.diagnostic(complex_expression_in_dependency_array_diagnostic(
545+
hook_name.as_str(),
546+
elem.span(),
547+
));
548+
}
538549
None
539550
}
540551
}

0 commit comments

Comments
 (0)