-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (30 loc) · 974 Bytes
/
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
module.exports = function(babel) {
var t = babel.types;
var defaultOptions = {
helperFunctionName: '$$await'
};
// Checks if an AwaitExpression path has already been
// wrapped by this plugin.
function noWrappingNeeded(path, helperFunctionName) {
var parent = path.parentPath;
var isGeneratedExpression =
!!parent
&& t.isCallExpression(parent)
&& t.isIdentifier(parent.node.callee, { name: helperFunctionName });
return isGeneratedExpression;
}
return {
visitor: {
AwaitExpression: function(path, state) {
var opts = state.opts;
var helperFunctionName = opts.helperFunctionName || defaultOptions.helperFunctionName;
if (noWrappingNeeded(path, helperFunctionName)) return;
var newPath = t.callExpression(
t.identifier(helperFunctionName),
[path.node]
);
path.replaceWith(newPath);
}
}
};
};