-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.es2015.js
140 lines (120 loc) · 3.6 KB
/
main.es2015.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
// @flow
import syntaxAsyncFunctions from 'babel-plugin-syntax-async-functions';
import type {NodePath} from 'babel-traverse';
import traverse from 'babel-traverse';
import nameFunction from 'babel-helper-function-name';
import template from 'babel-template';
const FUNCTION_TYPES = [
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
];
const BUILD_WRAPPER = template(`
(() => {
var REF = FUNCTION;
return function NAME(PARAMS) {
return REF.apply(this, arguments);
};
})
`);
const NAMED_BUILD_WRAPPER = template(`
(() => {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})
`);
export default function asyncToBluebird(pluginArg: any) {
const {types: t} = pluginArg;
function classOrObjectMethod(path: NodePath, state: any, hasAwait: boolean) {
const {node} = path;
const {body} = node;
node.async = false;
node.generator = false;
const container = t.functionExpression(null, [], t.blockStatement(body.body), hasAwait);
container.shadow = true;
const bbImport = state.addImport('bluebird', hasAwait ? 'coroutine' : 'method');
body.body = [
t.returnStatement(
t.callExpression(
t.callExpression(
bbImport,
[container]
),
[]
)
),
];
}
function plainFunction(path: NodePath, state: any, hasAwait: boolean) {
const {node} = path;
const isDeclaration = path.isFunctionDeclaration();
const asyncFnId = node.id;
let wrapper = BUILD_WRAPPER;
if (path.isArrowFunctionExpression()) path.arrowFunctionToShadowed();
else if (!isDeclaration && asyncFnId) wrapper = NAMED_BUILD_WRAPPER;
node.async = false;
node.generator = hasAwait;
node.id = null;
if (isDeclaration) node.type = 'FunctionExpression';
const bbImport = state.addImport('bluebird', hasAwait ? 'coroutine' : 'method');
const built = t.callExpression(bbImport, [node]);
const container = wrapper({
NAME: asyncFnId,
REF: path.scope.generateUidIdentifier('ref'),
FUNCTION: built,
PARAMS: node.params.map(() => path.scope.generateUidIdentifier('x')),
}).expression;
if (isDeclaration) {
const declar = t.variableDeclaration('let', [
t.variableDeclarator(
t.identifier(asyncFnId.name),
t.callExpression(container, [])
),
]);
declar._blockHoist = true;
path.replaceWith(declar);
} else {
const 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);
}
}
}
return {
inherits: syntaxAsyncFunctions,
visitor: {
Function(path: NodePath, state: any) {
const {node, scope} = path;
if (!node.async || node.generator) return;
const hasAwait = traverse.hasType(node.body, scope, 'AwaitExpression', FUNCTION_TYPES);
traverse(node, {
blacklist: FUNCTION_TYPES,
AwaitExpression(path2: NodePath) {
// eslint-disable-next-line no-param-reassign
path2.node.type = 'YieldExpression';
path2.node.argument = t.callExpression(
state.addImport('bluebird', 'resolve'),
[path2.node.argument]
);
},
}, scope);
const isClassOrObjectMethod = path.isClassMethod() || path.isObjectMethod();
(isClassOrObjectMethod ? classOrObjectMethod : plainFunction)(path, state, hasAwait);
},
},
};
}