Skip to content

Commit cc62d86

Browse files
committed
improve transforms
1 parent b6c1d9c commit cc62d86

6 files changed

+124
-63
lines changed

transform-export-async-function.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
const tsParser = require("jscodeshift/parser/ts");
2525

2626
const debug = require("debug")("transform:export-async-script");
27+
const debug2 = require("debug")("transform:print:export-async-script");
2728

2829
import {
2930
addAwaitKeyword,
@@ -41,6 +42,8 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
4142
**************************************************\n`
4243
);
4344

45+
let fileChanged = false;
46+
4447
const rootCollection = j(fileInfo.source);
4548

4649
const getPathFromSource = (source: string): string => {
@@ -241,11 +244,15 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
241244
});
242245
debug("==>is async function:", isAsyncFunction);
243246
if (isAsyncFunction) {
244-
convertAllCallExpressionToAsync(
245-
spec.local.name,
246-
rootCollection,
247-
j
248-
);
247+
if (
248+
convertAllCallExpressionToAsync(
249+
spec.local.name,
250+
rootCollection,
251+
j
252+
)
253+
) {
254+
fileChanged = true;
255+
}
249256
}
250257
}
251258
break;
@@ -263,11 +270,15 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
263270
});
264271
debug("==>is async function:", isAsyncFunction);
265272
if (isAsyncFunction) {
266-
convertAllCallExpressionToAsync(
267-
spec.local.name,
268-
rootCollection,
269-
j
270-
);
273+
if (
274+
convertAllCallExpressionToAsync(
275+
spec.local.name,
276+
rootCollection,
277+
j
278+
)
279+
) {
280+
fileChanged = true;
281+
}
271282
}
272283
}
273284
break;
@@ -282,5 +293,8 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
282293
return null;
283294
});
284295

285-
return rootCollection.toSource();
296+
if (fileChanged) {
297+
debug2("file changed:", fileInfo.path);
298+
return rootCollection.toSource();
299+
}
286300
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Find all await expression but not inside an async function
3+
* It won't modify your file
4+
*/
5+
6+
import { FileInfo, API, Options } from "jscodeshift";
7+
import { findParentFunction, getFunctionLocation } from "./utils";
8+
9+
const debug = require("debug")("transform:find-await-without-async");
10+
const debug2 = require("debug")("transform:print:find-await-without-async");
11+
12+
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
13+
debug(
14+
`**************************************************
15+
*** ${fileInfo.path}
16+
**************************************************`
17+
);
18+
19+
const rootCollection = j(fileInfo.source);
20+
// debug(rootCollection)
21+
22+
// find all await expression
23+
rootCollection.find(j.AwaitExpression).map((p) => {
24+
debug(j(p).toSource());
25+
// try to get the parent function
26+
const parentFunction = findParentFunction(p);
27+
if (!parentFunction) {
28+
debug2("Found:", fileInfo.path, p.value.loc?.start);
29+
return null;
30+
}
31+
switch (parentFunction.value.type) {
32+
case "ArrowFunctionExpression":
33+
case "FunctionExpression":
34+
case "FunctionDeclaration":
35+
case "ObjectMethod":
36+
case "ClassMethod":
37+
if (!parentFunction.value.async) {
38+
debug(
39+
"parentFunction.value.type",
40+
parentFunction.value.type,
41+
parentFunction.value
42+
);
43+
debug2("Found 2:", fileInfo.path, p.value.loc?.start);
44+
}
45+
break;
46+
default:
47+
debug("Unhandled function type:", parentFunction.value.type);
48+
}
49+
50+
return null;
51+
});
52+
53+
return undefined;
54+
};

transform-find-promise-all-foreach.ts

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,24 @@
33
* It won't modify your file
44
*/
55

6-
import {
7-
FileInfo,
8-
API,
9-
Options,
10-
ASTPath,
11-
Collection,
12-
MemberExpression,
13-
CallExpression,
14-
ExpressionStatement,
15-
FunctionDeclaration,
16-
ArrowFunctionExpression,
17-
FunctionExpression,
18-
Position,
19-
} from "jscodeshift";
20-
21-
const methodsMapping = {
22-
findOne: "findOneAsync",
23-
insert: "insertAsync",
24-
upsert: "upsertAsync",
25-
update: "updateAsync",
26-
remove: "removeAsync",
27-
createIndex: "createIndexAsync",
28-
dropIndex: "dropIndexAsync",
29-
dropCollection: "dropCollectionAsync",
30-
// methods on cursors
31-
count: "countAsync",
32-
fetch: "fetchAsync",
33-
forEach: "forEachAsync",
34-
map: "mapAsync",
35-
};
36-
37-
import {
38-
addAwaitKeyword,
39-
findParentFunction,
40-
getFunctionLocation,
41-
setFunctionNotAsync,
42-
} from "./utils";
6+
import { FileInfo, API, Options } from "jscodeshift";
437

448
const debug = require("debug")("transform:find-promise-all-foreach");
9+
const debug2 = require("debug")("transform:print:find-promise-all-foreach");
4510

4611
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
47-
// debug(
48-
// `**************************************************
49-
// *** ${fileInfo.path}
50-
// **************************************************`
51-
// );
12+
debug(
13+
`**************************************************
14+
*** ${fileInfo.path}
15+
**************************************************`
16+
);
5217

5318
const rootCollection = j(fileInfo.source);
5419
// debug(rootCollection)
5520

5621
// find all Promise.all() call
5722
rootCollection.find(j.CallExpression).map((p) => {
58-
// debug(j(p).toSource(), p.value.callee);
23+
debug(j(p).toSource(), p.value.callee);
5924
if (p.value.callee.type === "MemberExpression") {
6025
const { object, property } = p.value.callee;
6126
if (
@@ -65,21 +30,21 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
6530
property.type === "Identifier" &&
6631
property.name === "all"
6732
) {
68-
// debug("child", p.value.arguments);
33+
debug("child", p.value.arguments);
6934
if (
7035
p.value.arguments[0] &&
7136
p.value.arguments[0].type === "CallExpression"
7237
) {
7338
if (p.value.arguments[0].callee.type === "MemberExpression") {
7439
const { property: cProperty } = p.value.arguments[0].callee;
75-
// debug(cProperty);
40+
debug(cProperty);
7641
if (
7742
cProperty.type === "Identifier" &&
7843
cProperty.name === "forEach"
7944
) {
8045
// found
8146
debug(fileInfo.path);
82-
debug("!!!FOUND", cProperty.loc?.start);
47+
debug2("!!!FOUND", cProperty.loc?.start);
8348
// debug(j(p).toSource());
8449
}
8550
}

transform-fix-async-overly.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ import {
4343
} from "./utils";
4444

4545
const debug = require("debug")("transform:fix-async-overly");
46+
const debug2 = require("debug")("transform:print:fix-async-overly");
4647

4748
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
4849
debug(
4950
`**************************************************
5051
*** ${fileInfo.path}
5152
**************************************************`
5253
);
54+
let fileChanged = false;
55+
5356
const rootCollection = j(fileInfo.source);
5457
// debug(rootCollection)
5558

@@ -81,7 +84,9 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
8184
debug("found await:", foundAwait);
8285
if (!foundAwait) {
8386
// remove async from function expression
84-
setFunctionNotAsync(p);
87+
if (setFunctionNotAsync(p)) {
88+
fileChanged = true;
89+
}
8590
}
8691
};
8792

@@ -110,8 +115,17 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
110115
}
111116
return null;
112117
});
118+
rootCollection.find(j.ClassMethod).map((p) => {
119+
if (p.value.async) {
120+
checkIfAsyncNeeded(p);
121+
}
122+
return null;
123+
});
113124

114125
debug("**************************************************");
115126

116-
return rootCollection.toSource();
127+
if (fileChanged) {
128+
debug2("file changed:", fileInfo.path);
129+
return rootCollection.toSource();
130+
}
117131
};

transform-use-async-function.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
9999
});
100100
};
101101

102-
// find all async function definitions
102+
// find all function definitions
103103
rootCollection.find(j.Function).map((p) => {
104104
// debug("Function", p.value);
105105
switch (p.value.type) {
106106
case "FunctionDeclaration":
107107
debug("FunctionDeclaration", p.value.id?.loc?.start);
108+
// check if async function
108109
if (p.value.async && p.value.id?.name) {
109110
debug("async function name:", p.value.id.name);
110111
if (
@@ -118,6 +119,7 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
118119
case "ArrowFunctionExpression":
119120
case "FunctionExpression":
120121
debug("Function Expression", p.value.loc?.start);
122+
// check if async function
121123
if (p.value.async) {
122124
switch (p.parentPath.value.type) {
123125
case "VariableDeclarator":
@@ -160,6 +162,14 @@ module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
160162
}
161163
}
162164
break;
165+
// case "ClassMethod":
166+
// debug("ClassMethod", p.value.loc?.start);
167+
// debug("p.value", p.value);
168+
// // check if async function
169+
// if (p.value.async) {
170+
171+
// }
172+
// break;
163173
default:
164174
debug("Unhandled function type:", p.value.type);
165175
}

utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const findParentFunction = (p: ASTPath): ASTPath | undefined => {
1919
if (!p.parentPath) {
2020
return undefined;
2121
}
22-
// debug("find parent function of this", p);
22+
debug("find parent function of this", p);
2323

2424
// debug("parent", p.parentPath.value?.loc?.start);
2525
if (
@@ -28,6 +28,7 @@ export const findParentFunction = (p: ASTPath): ASTPath | undefined => {
2828
"FunctionExpression",
2929
"FunctionDeclaration",
3030
"ObjectMethod",
31+
"ClassMethod",
3132
].includes(p.parentPath.value.type)
3233
) {
3334
return p.parentPath;
@@ -62,7 +63,8 @@ export const setFunctionAsync = (p: ASTPath) => {
6263
p.value.type === "ArrowFunctionExpression" ||
6364
p.value.type === "FunctionDeclaration" ||
6465
p.value.type === "FunctionExpression" ||
65-
p.value.type === "ObjectMethod"
66+
p.value.type === "ObjectMethod" ||
67+
p.value.type === "ClassMethod"
6668
) {
6769
debug("set function async", p.value.loc?.start);
6870
if (p.value.async === true) {
@@ -79,7 +81,8 @@ export const setFunctionNotAsync = (p: ASTPath) => {
7981
p.value.type === "ArrowFunctionExpression" ||
8082
p.value.type === "FunctionDeclaration" ||
8183
p.value.type === "FunctionExpression" ||
82-
p.value.type === "ObjectMethod"
84+
p.value.type === "ObjectMethod" ||
85+
p.value.type === "ClassMethod"
8386
) {
8487
debug("set function async", p.value.loc?.start);
8588
if (p.value.async === false) {
@@ -170,6 +173,7 @@ export const getFunctionLocation = (p: ASTPath) => {
170173
case "FunctionDeclaration":
171174
case "FunctionExpression":
172175
case "ObjectMethod":
176+
case "ClassMethod":
173177
if (p.value.loc) {
174178
return {
175179
start: p.value.loc?.start,

0 commit comments

Comments
 (0)