-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform-find-promise-all-foreach.ts
58 lines (52 loc) · 1.73 KB
/
transform-find-promise-all-foreach.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Find all codes like Promise.all(SOME_VAR.forEach())
* It won't modify your file
*/
import { FileInfo, API, Options } from "jscodeshift";
const debug = require("debug")("transform:find-promise-all-foreach");
const debug2 = require("debug")("transform:print:find-promise-all-foreach");
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
debug(
`**************************************************
*** ${fileInfo.path}
**************************************************`
);
const rootCollection = j(fileInfo.source);
// debug(rootCollection)
// find all Promise.all() call
rootCollection.find(j.CallExpression).map((p) => {
debug(j(p).toSource(), p.value.callee);
if (p.value.callee.type === "MemberExpression") {
const { object, property } = p.value.callee;
if (
p.value.callee.type === "MemberExpression" &&
object.type === "Identifier" &&
object.name === "Promise" &&
property.type === "Identifier" &&
property.name === "all"
) {
debug("child", p.value.arguments);
if (
p.value.arguments[0] &&
p.value.arguments[0].type === "CallExpression"
) {
if (p.value.arguments[0].callee.type === "MemberExpression") {
const { property: cProperty } = p.value.arguments[0].callee;
debug(cProperty);
if (
cProperty.type === "Identifier" &&
cProperty.name === "forEach"
) {
// found
debug(fileInfo.path);
debug2("!!!FOUND", cProperty.loc?.start);
// debug(j(p).toSource());
}
}
}
}
}
return null;
});
return undefined;
};