Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug with useContext/useSyncExternalStore (#609) #621

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ declare_rule! {
/// - `useMemo`
/// - `useImperativeHandle`
/// - `useState`
/// - `useContext`
/// - `useReducer`
/// - `useRef`
/// - `useDebugValue`
/// - `useDeferredValue`
/// - `useTransition`
/// - `useId`
/// - `useSyncExternalStore`
///
/// If you want to add more hooks to the rule, check the [#options](options).
///
Expand Down Expand Up @@ -180,7 +177,6 @@ impl Default for ReactExtensiveDependenciesOptions {
("useMemo".to_string(), (0, 1).into()),
("useImperativeHandle".to_string(), (1, 2).into()),
("useState".to_string(), ReactHookConfiguration::default()),
("useContext".to_string(), ReactHookConfiguration::default()),
("useReducer".to_string(), ReactHookConfiguration::default()),
("useRef".to_string(), ReactHookConfiguration::default()),
(
Expand All @@ -195,21 +191,13 @@ impl Default for ReactExtensiveDependenciesOptions {
"useTransition".to_string(),
ReactHookConfiguration::default(),
),
("useId".to_string(), ReactHookConfiguration::default()),
(
"useSyncExternalStore".to_string(),
ReactHookConfiguration::default(),
),
]);

let stable_config = FxHashSet::from_iter([
StableReactHookConfiguration::new("useState", Some(1)),
StableReactHookConfiguration::new("useReducer", Some(1)),
StableReactHookConfiguration::new("useTransition", Some(1)),
StableReactHookConfiguration::new("useRef", None),
StableReactHookConfiguration::new("useContext", None),
StableReactHookConfiguration::new("useId", None),
StableReactHookConfiguration::new("useSyncExternalStore", None),
]);

Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* should not generate diagnostics */

import React from "react";
import { useEffect } from "react";
import { useEffect, useSyncExternalStore } from "react";
import doSomething from 'a';

// No captures
Expand Down Expand Up @@ -62,7 +62,7 @@ function MyComponent4() {
console.log(id);

console.log(externalStore);
}, [name, state, memoizedCallback, memoizedValue, isPending]);
}, [name, state, memoizedCallback, memoizedValue, isPending, externalStore, id, theme]);
}

// all hooks with dependencies
Expand Down Expand Up @@ -167,3 +167,12 @@ function MyComponent16() {
console.log(a);
}, [a]);
}

// https://github.com/biomejs/biome/issues/609
function MyComponent17() {
const data = useSyncExternalStore(subscribe, getSnapshot);

useEffect(() => {
console.log(data);
}, [data]);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
assertion_line: 83
expression: valid.js
---
# Input
```js
/* should not generate diagnostics */

import React from "react";
import { useEffect } from "react";
import { useEffect, useSyncExternalStore } from "react";
import doSomething from 'a';

// No captures
Expand Down Expand Up @@ -68,7 +69,7 @@ function MyComponent4() {
console.log(id);

console.log(externalStore);
}, [name, state, memoizedCallback, memoizedValue, isPending]);
}, [name, state, memoizedCallback, memoizedValue, isPending, externalStore, id, theme]);
}

// all hooks with dependencies
Expand Down Expand Up @@ -174,6 +175,15 @@ function MyComponent16() {
}, [a]);
}

// https://github.com/biomejs/biome/issues/609
function MyComponent17() {
const data = useSyncExternalStore(subscribe, getSnapshot);

useEffect(() => {
console.log(data);
}, [data]);
}

```


Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ The rule will inspect the following **known** hooks:
- `useMemo`
- `useImperativeHandle`
- `useState`
- `useContext`
- `useReducer`
- `useRef`
- `useDebugValue`
- `useDeferredValue`
- `useTransition`
- `useId`
- `useSyncExternalStore`

If you want to add more hooks to the rule, check the [#options](options).

Expand Down