index.js
1.67 KB
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
"use strict";
exports.__esModule = true;
exports.default = function (_ref) {
var t = _ref.types;
function getTempId(scope) {
var id = scope.path.getData("functionBind");
if (id) return id;
id = scope.generateDeclaredUidIdentifier("context");
return scope.path.setData("functionBind", id);
}
function getStaticContext(bind, scope) {
var object = bind.object || bind.callee.object;
return scope.isStatic(object) && object;
}
function inferBindContext(bind, scope) {
var staticContext = getStaticContext(bind, scope);
if (staticContext) return staticContext;
var tempId = getTempId(scope);
if (bind.object) {
bind.callee = t.sequenceExpression([t.assignmentExpression("=", tempId, bind.object), bind.callee]);
} else {
bind.callee.object = t.assignmentExpression("=", tempId, bind.callee.object);
}
return tempId;
}
return {
inherits: require("babel-plugin-syntax-function-bind"),
visitor: {
CallExpression: function CallExpression(_ref2) {
var node = _ref2.node,
scope = _ref2.scope;
var bind = node.callee;
if (!t.isBindExpression(bind)) return;
var context = inferBindContext(bind, scope);
node.callee = t.memberExpression(bind.callee, t.identifier("call"));
node.arguments.unshift(context);
},
BindExpression: function BindExpression(path) {
var node = path.node,
scope = path.scope;
var context = inferBindContext(node, scope);
path.replaceWith(t.callExpression(t.memberExpression(node.callee, t.identifier("bind")), [context]));
}
}
};
};
module.exports = exports["default"];