Skip to content

Commit dab2fdb

Browse files
authored
Add eslint-plugin-react-hooks/exhaustive-deps rule to check stale closure dependencies (#14636)
* Add ESLint rule for useEffect/useCallback/useMemo Hook dependencies * Fix ReactiveDependencies rule * fix lint errors * Support useLayoutEffect * Add some failing tests and comments * Gather dependencies in child scopes too * If we don't find foo.bar.baz in deps, try foo.bar, then foo * foo is enough for both foo.bar and foo.baz * Shorter rule name * Add fixable meta * Remove a bunch of code and start from scratch * [WIP] Only report errors from dependency array This results in nicer editing experience. Also has autofix. * Fix typo * [Temp] Skip all tests * Fix the first test * Revamp the test suite * Fix [foo] to include foo.bar * Don't suggest call expressions * Special case 'current' for refs * Don't complain about known static deps * Support useImperativeHandle * Better punctuation and formatting * More uniform message format * Treat React.useRef/useState/useReducer as static too * Add special message for ref.current * Add a TODO case * Alphabetize the autofix * Only alphabetize if it already was * Don't add static deps by default * Add an undefined variable case * Tweak wording * Rename to exhaustive-deps * Clean up / refactor a little bit
1 parent 1493abd commit dab2fdb

File tree

5 files changed

+2235
-5
lines changed

5 files changed

+2235
-5
lines changed

fixtures/eslint/.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"plugins": ["react-hooks"],
1111
"rules": {
12-
"react-hooks/rules-of-hooks": 2
12+
"react-hooks/rules-of-hooks": 2,
13+
"react-hooks/exhaustive-deps": 2
1314
}
1415
}

fixtures/eslint/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
// 2. "File > Add Folder to Workspace" this specific folder in VSCode with ESLint extension
55
// 3. Changes to the rule source should get picked up without restarting ESLint server
66

7-
function Foo() {
8-
if (condition) {
9-
useEffect(() => {});
10-
}
7+
function Comment({comment, commentSource}) {
8+
const currentUserID = comment.viewer.id;
9+
const environment = RelayEnvironment.forUser(currentUserID);
10+
const commentID = nullthrows(comment.id);
11+
useEffect(
12+
() => {
13+
const subscription = SubscriptionCounter.subscribeOnce(
14+
`StoreSubscription_${commentID}`,
15+
() =>
16+
StoreSubscription.subscribe(
17+
environment,
18+
{
19+
comment_id: commentID,
20+
},
21+
currentUserID,
22+
commentSource
23+
)
24+
);
25+
return () => subscription.dispose();
26+
},
27+
[commentID, commentSource, currentUserID, environment]
28+
);
1129
}

0 commit comments

Comments
 (0)