Skip to content

Commit 4d54d9a

Browse files
committed
feature: @putout/plugin-promises: apply-to-level-await: useEffect: exclude
1 parent 942f43e commit 4d54d9a

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
export default () => {
2-
useEffect(async () => {
3-
const t = await fs.promises.readFile('hello');
2+
useEffect(() => {
3+
(async () => {
4+
const t = await fs.promises.readFile('hello');
5+
})();
46
});
57
};
68

79
export const hello = () => {
8-
useEffect(async function() {
9-
const t = await fs.promises.readFile('hello');
10+
useEffect(function() {
11+
(async function() {
12+
const t = await fs.promises.readFile('hello');
13+
})();
1014
});
1115
};

packages/plugin-promises/lib/apply-top-level-await/fixture/iife-async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export default () => {
2-
useEffect(async () => {
2+
useEffect(() => {
33
(async () => {
44
const t = await fs.promises.readFile('hello');
55
})();
66
});
77
};
88

99
export const hello = () => {
10-
useEffect(async function() {
10+
useEffect(function() {
1111
(async function() {
1212
const t = await fs.promises.readFile('hello');
1313
})();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pipe from 'pipe-io';
2+
3+
useEffect(() => {
4+
if (a)
5+
(async () => {
6+
await pipe();
7+
})();
8+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
useEffect(() => {
2+
if (a)
3+
(async () => {
4+
await pipe();
5+
})()
6+
});

packages/plugin-promises/lib/apply-top-level-await/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {operator} from 'putout';
22

3-
const {replaceWithMultiple} = operator;
3+
const {
4+
replaceWithMultiple,
5+
compare,
6+
} = operator;
47

58
const add = ({push, store}) => (path) => {
69
const calleePath = path.get('callee');
@@ -54,11 +57,13 @@ export const traverse = ({push, store}) => {
5457
};
5558
};
5659

60+
const isInsideUseEffect = (a) => compare(a, 'useEffect(__args)');
61+
5762
function isAsyncParent(path) {
5863
const {parentPath} = path.parentPath.parentPath;
5964

60-
if (!parentPath)
61-
return true;
65+
if (path.find(isInsideUseEffect))
66+
return false;
6267

63-
return !parentPath.isFunction() || parentPath.node.async;
68+
return !parentPath;
6469
}

packages/plugin-promises/lib/apply-top-level-await/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {createTest} from '@putout/test';
2+
import * as declare from '@putout/plugin-declare';
23
import * as applyTopLevelAwait from './index.js';
4+
import * as addMissingAsync from '../add-missing-async/index.js';
35

46
const test = createTest(import.meta.url, {
57
plugins: [
@@ -56,3 +58,11 @@ test('plugin-promises: apply-top-level-await: transform: iife-async', (t) => {
5658
t.transform('iife-async');
5759
t.end();
5860
});
61+
62+
test('plugin-add-missing-await: transform: use-effect', (t) => {
63+
t.transform('use-effect', {
64+
addMissingAsync,
65+
declare,
66+
});
67+
t.end();
68+
});

packages/plugin-promises/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"promises"
3232
],
3333
"devDependencies": {
34+
"@putout/plugin-declare": "*",
3435
"@putout/test": "^14.0.0",
3536
"c8": "^10.0.0",
3637
"eslint": "^9.0.0",

0 commit comments

Comments
 (0)