Skip to content

Commit 0d240e4

Browse files
committed
fix(linter): false positive in react/exhaustive-deps with default formal parameter (#11395)
fixes #11396
1 parent 50a98cf commit 0d240e4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use oxc_ast::{
77
AstKind, AstType,
88
ast::{
99
Argument, ArrayExpressionElement, ArrowFunctionExpression, BindingPatternKind,
10-
CallExpression, ChainElement, Expression, Function, FunctionBody, IdentifierReference,
11-
MemberExpression, StaticMemberExpression, TSTypeAnnotation, TSTypeParameter,
12-
TSTypeReference, VariableDeclarationKind,
10+
CallExpression, ChainElement, Expression, FormalParameters, Function, FunctionBody,
11+
IdentifierReference, MemberExpression, StaticMemberExpression, TSTypeAnnotation,
12+
TSTypeParameter, TSTypeReference, VariableDeclarationKind,
1313
},
1414
match_expression,
1515
};
@@ -373,6 +373,8 @@ impl Rule for ExhaustiveDeps {
373373
let (found_dependencies, refs_inside_cleanups) = {
374374
let mut found_dependencies = ExhaustiveDepsVisitor::new(ctx.semantic());
375375

376+
found_dependencies.visit_formal_parameters(callback_node.parameters());
377+
376378
if let Some(function_body) = callback_node.body() {
377379
found_dependencies.visit_function_body(function_body);
378380
}
@@ -631,6 +633,13 @@ impl<'a> CallbackNode<'a> {
631633
}
632634
}
633635

636+
fn parameters(&self) -> &FormalParameters<'a> {
637+
match self {
638+
CallbackNode::Function(func) => &func.params,
639+
CallbackNode::ArrowFunction(func) => &func.params,
640+
}
641+
}
642+
634643
fn body(&self) -> Option<&FunctionBody<'a>> {
635644
match self {
636645
CallbackNode::Function(func) => func.body.as_deref(),
@@ -2139,6 +2148,17 @@ fn test() {
21392148
};
21402149
}, []);
21412150
}",
2151+
r#"function X() {
2152+
const defaultParam1 = "";
2153+
const myFunction = useCallback(
2154+
(param1 = defaultParam1, param2) => {
2155+
},
2156+
[defaultParam1]
2157+
);
2158+
2159+
return null;
2160+
}
2161+
"#,
21422162
];
21432163

21442164
let fail = vec![

0 commit comments

Comments
 (0)