fix(linter/react): fix false positives for async components in rules_of_hooks (#3307)

related to #3257
This commit is contained in:
rzvxa 2024-05-16 16:38:16 +00:00
parent 0864cd0115
commit 712ee0dde8

View file

@ -149,18 +149,6 @@ impl Rule for RulesOfHooks {
id.name.as_str(),
));
}
// Hooks can't be called from async function.
AstKind::Function(Function { id: Some(id), r#async: true, .. }) => {
return ctx.diagnostic(diagnostics::async_component(id.span, id.name.as_str()));
}
// Hooks can't be called from async arrow function.
AstKind::ArrowFunctionExpression(ArrowFunctionExpression {
span,
r#async: true,
..
}) => {
return ctx.diagnostic(diagnostics::async_component(*span, "Anonymous"));
}
// Hooks are allowed inside of unnamed functions used as arguments. As long as they are
// not used as a callback inside of components or hooks.
AstKind::Function(Function { id: None, .. }) | AstKind::ArrowFunctionExpression(_)
@ -173,7 +161,11 @@ impl Rule for RulesOfHooks {
return;
}
AstKind::Function(Function { span, id: None, .. })
| AstKind::ArrowFunctionExpression(ArrowFunctionExpression { span, .. }) => {
| AstKind::ArrowFunctionExpression(ArrowFunctionExpression {
span,
r#async: false,
..
}) => {
let ident = get_declaration_identifier(nodes, parent_func.id());
// Hooks cannot be called inside of export default functions or used in a function
@ -206,6 +198,18 @@ impl Rule for RulesOfHooks {
));
}
}
// Hooks can't be called from async function.
AstKind::Function(Function { id: Some(id), r#async: true, .. }) => {
return ctx.diagnostic(diagnostics::async_component(id.span, id.name.as_str()));
}
// Hooks can't be called from async arrow function.
AstKind::ArrowFunctionExpression(ArrowFunctionExpression {
span,
r#async: true,
..
}) => {
return ctx.diagnostic(diagnostics::async_component(*span, "Anonymous"));
}
_ => {}
}
@ -923,6 +927,13 @@ fn test() {
};
};
",
"
test.beforeEach(async () => {
timer = Sinon.useFakeTimers({
toFake: ['setInterval'],
});
});
",
];
let fail = vec![