Skip to content

Commit

Permalink
[ESLint] Allow partial matches for custom Effect Hooks (#17663)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 19, 2019
1 parent 7259231 commit 8979766
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Expand Up @@ -3154,6 +3154,37 @@ const tests = {
`and use that variable in the cleanup function.`,
],
},
{
code: `
function MyComponent() {
const myRef = useRef();
useLayoutEffect_SAFE_FOR_SSR(() => {
const handleMove = () => {};
myRef.current.addEventListener('mousemove', handleMove);
return () => myRef.current.removeEventListener('mousemove', handleMove);
});
return <div ref={myRef} />;
}
`,
output: `
function MyComponent() {
const myRef = useRef();
useLayoutEffect_SAFE_FOR_SSR(() => {
const handleMove = () => {};
myRef.current.addEventListener('mousemove', handleMove);
return () => myRef.current.removeEventListener('mousemove', handleMove);
});
return <div ref={myRef} />;
}
`,
errors: [
`The ref value 'myRef.current' will likely have changed by the time ` +
`this effect cleanup function runs. If this ref points to a node ` +
`rendered by React, copy 'myRef.current' to a variable inside the effect, ` +
`and use that variable in the cleanup function.`,
],
options: [{additionalHooks: 'useLayoutEffect_SAFE_FOR_SSR'}],
},
{
// Autofix ignores constant primitives (leaving the ones that are there).
code: `
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Expand Up @@ -81,7 +81,7 @@ export default {
// Get the reactive hook node.
const reactiveHook = node.parent.callee;
const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name;
const isEffect = reactiveHookName.endsWith('Effect');
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);

// Get the declared dependencies for this reactive hook. If there is no
// second argument then the reactive callback will re-run on every render.
Expand Down

0 comments on commit 8979766

Please sign in to comment.