Skip to content

Commit 9327c49

Browse files
committed
Update for hooks function support
1 parent ad3785e commit 9327c49

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ const tests = {
338338
}
339339
`,
340340
`
341-
// Valid because render-hooks acts as a component boundary
341+
// Valid because Hooks component acts as a component boundary
342342
function App(props) {
343343
return props.isOpen
344344
? <Hooks>
@@ -347,6 +347,14 @@ const tests = {
347347
: null;
348348
}
349349
`,
350+
`
351+
// Valid because hooks function acts as a component boundary
352+
function App(props) {
353+
return props.isOpen
354+
? hooks(() => <Modal close={useCallback(() => props.setIsOpen(false), [props.setIsOpen])} />)
355+
: null;
356+
}
357+
`,
350358
],
351359
invalid: [
352360
{
@@ -899,7 +907,7 @@ const tests = {
899907
},
900908
{
901909
code: `
902-
// Invalid because rule of hooks still need to be adhered to within render-hooks
910+
// Invalid because rule of hooks still need to be adhered to within Hooks component
903911
function App(props) {
904912
return props.isOpen
905913
? <Hooks>
@@ -910,8 +918,21 @@ const tests = {
910918
: null;
911919
}
912920
`,
913-
errors: [conditionalError('useCallback')]
914-
}
921+
errors: [conditionalError('useCallback')],
922+
},
923+
{
924+
code: `
925+
// Invalid because rule of hooks still need to be adhered to within hooks function
926+
function App(props) {
927+
return props.isOpen
928+
? hooks(() => {
929+
return <Modal close={props.setIsOpen ? useCallback(() => props.setIsOpen(false), [props.setIsOpen]) : undefined} />;
930+
})
931+
: null;
932+
}
933+
`,
934+
errors: [conditionalError('useCallback')],
935+
},
915936
],
916937
};
917938

packages/eslint-plugin-react-hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@lewisl9029/eslint-plugin-react-hooks",
33
"description": "ESLint rules for React Hooks",
4-
"version": "4.2.0",
4+
"version": "4.2.1",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/facebook/react.git",

packages/eslint-plugin-react-hooks/src/RulesOfHooks.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,22 @@ function isInsideComponentOrHook(node) {
105105
}
106106

107107
function isDirectlyInsideRenderHooks(node) {
108-
return node.parent &&
109-
node.parent.type === 'JSXExpressionContainer' &&
110-
node.parent.parent &&
111-
node.parent.parent.type === 'JSXElement' &&
108+
if (!node.parent) {
109+
return false;
110+
}
111+
112+
const isDirectlyInsideHooksComponent =
113+
node.parent.type === 'JSXExpressionContainer' &&
114+
node.parent.parent &&
115+
node.parent.parent.type === 'JSXElement' &&
112116
node.parent.parent.openingElement.name.name === 'Hooks';
117+
const isDirectlyInsideHooksFunction =
118+
node.parent.type === 'CallExpression' &&
119+
node.parent.callee &&
120+
node.parent.callee.type === 'Identifier' &&
121+
node.parent.callee.name === 'hooks';
122+
123+
return isDirectlyInsideHooksComponent || isDirectlyInsideHooksFunction;
113124
}
114125

115126
export default {
@@ -360,7 +371,9 @@ export default {
360371
const isDirectlyInsideComponentOrHook = codePathFunctionName
361372
? isComponentName(codePathFunctionName) ||
362373
isHook(codePathFunctionName)
363-
: isForwardRefCallback(codePathNode) || isMemoCallback(codePathNode) || isDirectlyInsideRenderHooks(codePathNode);
374+
: isForwardRefCallback(codePathNode) ||
375+
isMemoCallback(codePathNode) ||
376+
isDirectlyInsideRenderHooks(codePathNode);
364377

365378
// Compute the earliest finalizer level using information from the
366379
// cache. We expect all reachable final segments to have a cache entry

0 commit comments

Comments
 (0)