Skip to content

Commit

Permalink
fix(lint/useHookAtTopLevel): don't flag jest function calls that look…
Browse files Browse the repository at this point in the history
… like hooks
  • Loading branch information
dyc3 committed Jul 17, 2024
1 parent 76da70e commit 37d1157
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions crates/biome_js_analyze/src/react/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ pub(crate) fn is_react_hook_call(call: &JsCallExpression) -> bool {
return false;
};

// HACK: jest has some functions that start with `use` and are not hooks
if let Some(expr) = call
.callee()
.ok()
.and_then(|callee| callee.as_js_static_member_expression().cloned())
.and_then(|member| member.object().ok())
.and_then(|object| object.as_js_identifier_expression().cloned())
.and_then(|ident| ident.name().ok())
.and_then(|name| name.value_token().ok())
{
if expr.text_trimmed() == "jest" {
return false;
}
}

is_react_hook(name.text_trimmed())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ test('c', () => {

expect(result.current).toBeDefined();
});

describe("foo", () => {
beforeEach(() => jest.useFakeTimers('legacy'));
afterEach(() => jest.useRealTimers());
})
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ test('c', () => {
expect(result.current).toBeDefined();
});

```

describe("foo", () => {
beforeEach(() => jest.useFakeTimers('legacy'));
afterEach(() => jest.useRealTimers());
})

```

0 comments on commit 37d1157

Please sign in to comment.