/
index.ts
55 lines (46 loc) 路 1.4 KB
/
index.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
import { declare } from "@babel/helper-plugin-utils";
import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator";
import { addNamed } from "@babel/helper-module-imports";
import { types as t } from "@babel/core";
export default declare((api, options) => {
api.assertVersion(7);
const { method, module } = options;
const noNewArrows = api.assumption("noNewArrows");
const ignoreFunctionLength = api.assumption("ignoreFunctionLength");
if (method && module) {
return {
name: "transform-async-to-generator",
visitor: {
Function(path, state) {
if (!path.node.async || path.node.generator) return;
let wrapAsync = state.methodWrapper;
if (wrapAsync) {
wrapAsync = t.cloneNode(wrapAsync);
} else {
wrapAsync = state.methodWrapper = addNamed(path, method, module);
}
remapAsyncToGenerator(
path,
{ wrapAsync },
noNewArrows,
ignoreFunctionLength,
);
},
},
};
}
return {
name: "transform-async-to-generator",
visitor: {
Function(path, state) {
if (!path.node.async || path.node.generator) return;
remapAsyncToGenerator(
path,
{ wrapAsync: state.addHelper("asyncToGenerator") },
noNewArrows,
ignoreFunctionLength,
);
},
},
};
});