-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvisitor.js
61 lines (53 loc) · 1.33 KB
/
visitor.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
var recast = require("recast");
var types = recast.types;
var b = types.builders;
exports.visitFunction = function(path) {
// Calling this.traverse(path) first makes for a post-order traversal.
this.traverse(path);
var node = path.value;
if (node.async) {
node.async = false;
} else {
return;
}
awaitVisitor.visit(path.get("body"));
var resultExpr = b.callExpression(
b.memberExpression(
this.getRuntime(),
b.identifier("async"),
false
),
[b.callExpression(
b.functionExpression(
null, // Anonymous.
[], // No parameters.
node.body, // Body without await.
true, // Generator.
node.expression
),
[] // Immediately invoked.
)]
);
if (node.expression) {
node.body = resultExpr;
} else {
node.body = b.blockStatement([
b.returnStatement(resultExpr)
]);
}
};
exports.getRuntime = function() {
return b.callExpression(
b.identifier("require"),
[b.literal("es7-async-await/runtime")]
);
};
var awaitVisitor = types.PathVisitor.fromMethodsObject({
visitFunction: function(path) {
return false; // Don't descend into nested function scopes.
},
visitAwaitExpression: function(path) {
// Convert await expressions to yield expressions.
return b.yieldExpression(path.value.argument, false);
}
});