Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions crates/oxc_linter/src/rules/react/exhaustive_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,15 +1408,32 @@ impl<'a> Visit<'a> for ExhaustiveDepsVisitor<'a, '_> {
if let Some(decl) = get_declaration_of_variable(ident, self.semantic) {
let is_set_state_call = match decl.kind() {
AstKind::VariableDeclarator(var_decl) => {
if let Some(Expression::CallExpression(call_expr)) = &var_decl.init {
if let Some(name) = func_call_without_react_namespace(call_expr) {
name == "useState" || name == "useReducer"
} else {
false
}
} else {
false
let Some(Expression::CallExpression(call_expr)) = &var_decl.init else {
return;
};

let Some(name) = func_call_without_react_namespace(call_expr) else {
return;
};

if name != "useState" && name != "useReducer" {
return;
}

let BindingPatternKind::ArrayPattern(array_pat) = &var_decl.id.kind else {
return;
};

let Some(Some(second_arg)) = array_pat.elements.get(1) else {
return;
};

let BindingPatternKind::BindingIdentifier(binding_ident) = &second_arg.kind
else {
return;
};

binding_ident.name == ident.name
}
_ => false,
};
Expand Down Expand Up @@ -2554,6 +2571,7 @@ fn test() {
r"function MyComponent(props) { useEffect(() => { console.log(props.foo!.bar) }, [props.foo!.bar]) }",
r"function MyComponent(props) { useEffect(() => { console.log((props.foo).bar) }, [props.foo!.bar]) }",
r"function MyComponent(props) { const external = {}; const y = useMemo(() => { const z = foo<typeof external>(); return z; }, []) }",
r#"function Test() { const [state, setState] = useState(); useEffect(() => { console.log("state", state); }); }"#,
];

let fail = vec![
Expand Down
Loading