-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
45 lines (39 loc) · 1.01 KB
/
main.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
import {addNamed} from '@babel/helper-module-imports';
export default function promiseToBluebird({types: t}) {
return {
visitor: {
ReferencedIdentifier(path) {
const {node, parent, scope} = path;
if (node.name !== 'Promise') return;
if (t.isMemberExpression(parent)) return;
if (scope.getBindingIdentifier('Promise')) return;
path.replaceWith(addNamed(path, 'default', 'bluebird', {nameHint: 'Promise'}));
},
MemberExpression(path) {
const {node} = path;
const obj = node.object;
if (obj.name !== 'Promise') return;
if (!path.isReferenced()) return;
if (path.scope.getBindingIdentifier('Promise')) return;
if (node.computed) {
path.replaceWith(
t.memberExpression(
addNamed(path, 'default', 'bluebird', {nameHint: 'Promise'}),
node.property,
true
)
);
} else {
path.replaceWith(
addNamed(
path,
node.property.name,
'bluebird',
{nameHint: 'Promise'}
)
);
}
},
},
};
}