Skip to content

Commit 1b94b29

Browse files
committed
Add IIFE todo tests and notes
#16
1 parent 7c09ce9 commit 1b94b29

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/util/react.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ export const getUseStateNode = (context, ref) => {
151151
// When false, it's likely inside a callback, e.g. a listener, or Promise chain that retrieves external data.
152152
// Note we'll still analyze derived setters because isStateSetter considers that.
153153
// Heuristic inspired by https://eslint-react.xyz/docs/rules/hooks-extra-no-direct-set-state-in-use-effect
154+
// Also returns false for IIFEs, which technically could cause a false negative.
155+
// But IIFEs in effects are typically used to call async functions, implying it retrieves external state.
156+
// So, not a big deal.
154157
export const isDirectCall = (ref) => {
155158
let node = ref.identifier;
156159

test/syntax.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ new MyRuleTester().run("/syntax", {
119119
},
120120
{
121121
// https://github.com/NickvanDyke/eslint-plugin-react-you-might-not-need-an-effect/issues/16
122-
name: "Async derived setter",
122+
name: "External IIFE",
123123
code: js`
124124
import { useEffect, useState } from 'react';
125125
@@ -490,5 +490,34 @@ new MyRuleTester().run("/syntax", {
490490
},
491491
],
492492
},
493+
{
494+
// https://github.com/NickvanDyke/eslint-plugin-react-you-might-not-need-an-effect/issues/16
495+
name: "Internal IIFE",
496+
todo: true, // TODO: `isDirectCall` returns false for IIFEs, so we don't follow `iife`. Otherwise it works.
497+
code: js`
498+
import { useEffect, useState } from 'react';
499+
500+
export const App = () => {
501+
const [data, setData] = useState(null);
502+
503+
const iife = () => {
504+
return (async () => {
505+
setData('Meow');
506+
})();
507+
};
508+
509+
useEffect(() => {
510+
(async () => {
511+
await iife();
512+
})();
513+
}, []);
514+
};
515+
`,
516+
errors: [
517+
{
518+
messageId: messageIds.avoidChainingState,
519+
},
520+
],
521+
},
493522
],
494523
});

0 commit comments

Comments
 (0)