I'm extremely stupid and didn't commit as I go. To anyone reading this I'm extremely sorry. A lot of these changes are very broad and I plan on releasing Babel 6.0.0 today live on stage at Ember Camp London so I'm afraid I couldn't wait. If you're ever in London I'll buy you a beer (or assorted beverage!) to make up for it, also I'll kiss your feet and give you a back massage, maybe.
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
export default function ({ types: t }) {
|
|
function getTempId(scope) {
|
|
let id = scope.path.getData("functionBind");
|
|
if (id) return id;
|
|
|
|
id = scope.generateDeclaredUidIdentifier("context");
|
|
return scope.path.setData("functionBind", id);
|
|
}
|
|
|
|
function getStaticContext(bind, scope) {
|
|
let object = bind.object || bind.callee.object;
|
|
return scope.isStatic(object) && object;
|
|
}
|
|
|
|
function inferBindContext(bind, scope) {
|
|
let staticContext = getStaticContext(bind, scope);
|
|
if (staticContext) return staticContext;
|
|
|
|
let 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({ node, scope }) {
|
|
let bind = node.callee;
|
|
if (!t.isBindExpression(bind)) return;
|
|
|
|
let context = inferBindContext(bind, scope);
|
|
node.callee = t.memberExpression(bind.callee, t.identifier("call"));
|
|
node.arguments.unshift(context);
|
|
},
|
|
|
|
BindExpression(path) {
|
|
let { node, scope } = path;
|
|
let context = inferBindContext(node, scope);
|
|
path.replaceWith(t.callExpression(t.memberExpression(node.callee, t.identifier("bind")), [context]));
|
|
}
|
|
}
|
|
};
|
|
}
|