Skip to content

Commit df61426

Browse files
authored
fix(plugins/hooks-extra): misidentification of 'set' function in IIFE inside of hooks as its inside of 'useEffect', 'useLayoutEffect', closes #967 (#968)
1 parent 5c91ff1 commit df61426

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/hooks/use-no-direct-set-state-in-use-effect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
107107
match(getCallKind(node))
108108
.with("setState", () => {
109109
switch (true) {
110-
case pEntry.node === setupFunction
111-
|| pEntry.kind === "immediate": {
110+
case pEntry.node === setupFunction:
111+
case pEntry.kind === "immediate"
112+
&& AST.findParentNode(pEntry.node, AST.isFunction) === setupFunction: {
112113
onViolation(context, node, {
113114
name: context.sourceCode.getText(node.callee),
114115
});

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-effect.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,5 +785,21 @@ ruleTester.run(RULE_NAME, rule, {
785785
// ...use tooltipHeight in the rendering logic below...
786786
}
787787
`,
788+
// https://github.com/Rel1cx/eslint-react/issues/967
789+
/* tsx */ `
790+
import { useEffect, useState, useCallback } from "react";
791+
792+
function useCustomHook() {
793+
const [something, setSomething] = useState('');
794+
795+
const test = useCallback(() => {
796+
setSomething('') // doesn't trigger the rules
797+
798+
;(() => {
799+
setSomething('') // trigger the rules
800+
})()
801+
}, [])
802+
}
803+
`,
788804
],
789805
});

packages/plugins/eslint-plugin-react-x/src/rules/no-children-prop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ react-x/no-children-prop
2020

2121
## What it does
2222

23-
Disallows passing 'children' as a prop.
23+
Disallows passing `children` as a prop.
2424

2525
Most of the time, `children` should be actual `children`, not passed in as a `prop`.
2626

0 commit comments

Comments
 (0)