Skip to content

Commit 05da0aa

Browse files
authored
fix(eslint-plugin): require-named-effect does not allow to use void operator (#135)
fix: require-named-effect does not allow to use void operator
1 parent 2ac8ef9 commit 05da0aa

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

example-app/eslint-breaking-examples/break-require-named-effect.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ import { useEffect } from "react";
77

88
const MyComponent = () => {
99
const doAThing = (t: number) => t;
10+
const doNothing = () => {};
11+
const initializeStuff = () => Promise.resolve();
1012

1113
useEffect(() => {
1214
const abc = 1;
1315
doAThing(abc);
1416
}, []);
1517

18+
// Does not trigger an error:
19+
useEffect(() => doNothing(), []);
20+
useEffect(() => void initializeStuff(), []);
21+
1622
return <></>;
1723
};
1824

packages/eslint-plugin/docs/rules/require-named-effect.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ useEffect(theNameOfAFunction(), []);
3333
useEffect(() => theNameOfAFunction(), []);
3434
```
3535

36+
```jsx
37+
useEffect(() => void theNameOfAFunction(), []);
38+
```
39+
3640
```jsx
3741
useEffect(() => {
3842
theOnlyChildIsAFunctionCall();

packages/eslint-plugin/lib/rules/require-named-effect.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export const requireNamedEffectRule: Rule.RuleModule = {
5252
return true;
5353
}
5454

55+
// It's a unary expression that contains a function call:
56+
// `useEffect(() => void theNameOfAFunction(), []);`
57+
if (body.type === "UnaryExpression") {
58+
if (body.argument.type === "CallExpression") {
59+
return true;
60+
}
61+
}
62+
5563
// There's a function body, but it just calls another function:
5664
// `useEffect(() => {
5765
// theOnlyChildIsAFunctionCall();

0 commit comments

Comments
 (0)