-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform-use-async-function.ts
166 lines (153 loc) · 5.07 KB
/
transform-use-async-function.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* 1. Find all async function declarations
* 2. Find all these function calls
* 3. Add await expression if needed.
*/
import fs from "fs";
6;
import { FileInfo, API, Options } from "jscodeshift";
const debug = require("debug")("transform:use-async-function");
const debug2 = require("debug")("transform:print:use-async-function");
import {
convertAllCallExpressionToAsync,
convertAllMemberExpressionCallToAsync,
findParentObject,
} from "./utils";
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
debug(
`\n**************************************************
*** ${fileInfo.path}
**************************************************\n`
);
let fileChanged = false;
const rootCollection = j(fileInfo.source);
const findVariableAssignments = (name: string) => {
debug("Find variable declaration using this name:", name);
rootCollection.find(j.VariableDeclaration).map((p) => {
// debug("***VariableDeclaration", j(p).toSource());
p.value.declarations.map((d) => {
// debug("declaration", d);
if (d.type === "VariableDeclarator") {
if (d.init?.type === "Identifier" && d.init.name === name) {
// const theVariable = theValue;
if (d.id.type === "Identifier") {
debug("convert to async/await:", d.id.name);
if (
convertAllCallExpressionToAsync(d.id.name, rootCollection, j)
) {
fileChanged = true;
}
}
}
if (
d.init?.type === "ObjectExpression" &&
d.id.type === "Identifier"
) {
const objectName = d.id.name;
d.init.properties.map((dp) => {
// debug("****ObjectExpression", dp);
if (
dp.type === "ObjectProperty" &&
dp.value.type === "Identifier" &&
dp.value.name === name &&
dp.key.type === "Identifier"
) {
debug("convert to async/await:", objectName);
if (
convertAllMemberExpressionCallToAsync(
objectName,
dp.key.name,
rootCollection,
j
)
) {
fileChanged = true;
}
}
});
}
}
});
return null;
});
};
// find all function definitions
rootCollection.find(j.Function).map((p) => {
// debug("Function", p.value);
switch (p.value.type) {
case "FunctionDeclaration":
debug("FunctionDeclaration", p.value.id?.loc?.start);
// check if async function
if (p.value.async && p.value.id?.name) {
debug("async function name:", p.value.id.name);
if (
convertAllCallExpressionToAsync(p.value.id?.name, rootCollection, j)
) {
fileChanged = true;
}
findVariableAssignments(p.value.id?.name);
}
break;
case "ArrowFunctionExpression":
case "FunctionExpression":
debug("Function Expression", p.value.loc?.start);
// check if async function
if (p.value.async) {
switch (p.parentPath.value.type) {
case "VariableDeclarator":
debug("async function name:", p.parentPath.value.id.name);
if (
convertAllCallExpressionToAsync(
p.parentPath.value.id.name,
rootCollection,
j
)
) {
fileChanged = true;
}
findVariableAssignments(p.parentPath.value.id.name);
break;
case "ObjectProperty": {
debug("the property", p.parentPath.value.key.name);
const parentObject = findParentObject(p.parentPath);
// debug("parent object", parentObject);
if (
parentObject?.value.type === "VariableDeclarator" &&
parentObject.value.id.type === "Identifier"
) {
debug("object name:", parentObject.value.id.name);
if (
convertAllMemberExpressionCallToAsync(
parentObject.value.id.name,
p.parentPath.value.key.name,
rootCollection,
j
)
) {
fileChanged = true;
}
}
break;
}
default:
debug("Unhandled parent type:", p.parentPath.value.type);
}
}
break;
// case "ClassMethod":
// debug("ClassMethod", p.value.loc?.start);
// debug("p.value", p.value);
// // check if async function
// if (p.value.async) {
// }
// break;
default:
debug("Unhandled function type:", p.value.type);
}
return null;
});
if (fileChanged) {
debug2("file changed: ", fileInfo.path);
return rootCollection.toSource();
}
};