-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
165 lines (137 loc) 路 3.91 KB
/
index.js
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
/* @noflow */
import type { NodePath } from "babel-traverse";
import nameFunction from "babel-helper-function-name";
import template from "babel-template";
import * as t from "babel-types";
import rewriteForAwait from "./for-await";
let buildWrapper = template(`
(() => {
var REF = FUNCTION;
return function NAME(PARAMS) {
return REF.apply(this, arguments);
};
})
`);
let namedBuildWrapper = template(`
(() => {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})
`);
let awaitVisitor = {
Function(path) {
if (path.isArrowFunctionExpression() && !path.node.async) {
path.arrowFunctionToShadowed();
return;
}
path.skip();
},
AwaitExpression({ node }, { wrapAwait }) {
node.type = "YieldExpression";
if (wrapAwait) {
node.argument = t.callExpression(wrapAwait, [node.argument]);
}
},
ForAwaitStatement(path, { file, wrapAwait }) {
let { node } = path;
let build = rewriteForAwait(path, {
getAsyncIterator: file.addHelper("asyncIterator"),
wrapAwait
});
let { declar, loop } = build;
let block = loop.body;
// ensure that it's a block so we can take all its statements
path.ensureBlock();
// add the value declaration to the new loop body
if (declar) {
block.body.push(declar);
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
t.inherits(loop, node);
t.inherits(loop.body, node.body);
if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
path.remove();
} else {
path.replaceWithMultiple(build.node);
}
}
};
function classOrObjectMethod(path: NodePath, callId: Object) {
let node = path.node;
let body = node.body;
node.async = false;
let container = t.functionExpression(null, [], t.blockStatement(body.body), true);
container.shadow = true;
body.body = [
t.returnStatement(t.callExpression(
t.callExpression(callId, [container]),
[]
))
];
}
function plainFunction(path: NodePath, callId: Object) {
let node = path.node;
let isDeclaration = path.isFunctionDeclaration();
let asyncFnId = node.id;
let wrapper = buildWrapper;
if (path.isArrowFunctionExpression()) {
path.arrowFunctionToShadowed();
} else if (!isDeclaration && asyncFnId) {
wrapper = namedBuildWrapper;
}
node.async = false;
node.generator = true;
node.id = null;
if (isDeclaration) {
node.type = "FunctionExpression";
}
let built = t.callExpression(callId, [node]);
let container = wrapper({
NAME: asyncFnId,
REF: path.scope.generateUidIdentifier("ref"),
FUNCTION: built,
PARAMS: node.params.map(() => path.scope.generateUidIdentifier("x"))
}).expression;
if (isDeclaration) {
let declar = t.variableDeclaration("let", [
t.variableDeclarator(
t.identifier(asyncFnId.name),
t.callExpression(container, [])
)
]);
declar._blockHoist = true;
path.replaceWith(declar);
} else {
let retFunction = container.body.body[1].argument;
if (!asyncFnId) {
nameFunction({
node: retFunction,
parent: path.parent,
scope: path.scope
});
}
if (!retFunction || retFunction.id || node.params.length) {
// we have an inferred function id or params so we need this wrapper
path.replaceWith(t.callExpression(container, []));
} else {
// we can omit this wrapper as the conditions it protects for do not apply
path.replaceWith(built);
}
}
}
export default function (path: NodePath, file: Object, helpers: Object) {
path.get("body").traverse(awaitVisitor, {
file,
wrapAwait: helpers.wrapAwait
});
if (path.isClassMethod() || path.isObjectMethod()) {
classOrObjectMethod(path, helpers.wrapAsync);
} else {
plainFunction(path, helpers.wrapAsync);
}
}