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.
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
import pull from "lodash/array/pull";
|
|
|
|
export default function ({ types: t }) {
|
|
function isProtoKey(node) {
|
|
return t.isLiteral(t.toComputedKey(node, node.key), { value: "__proto__" });
|
|
}
|
|
|
|
function isProtoAssignmentExpression(node) {
|
|
let left = node.left;
|
|
return t.isMemberExpression(left) && t.isLiteral(t.toComputedKey(left, left.property), { value: "__proto__" });
|
|
}
|
|
|
|
function buildDefaultsCallExpression(expr, ref, file) {
|
|
return t.expressionStatement(t.callExpression(file.addHelper("defaults"), [ref, expr.right]));
|
|
}
|
|
|
|
return {
|
|
visitor: {
|
|
AssignmentExpression(path, file) {
|
|
if (!isProtoAssignmentExpression(path.node)) return;
|
|
|
|
let nodes = [];
|
|
let left = path.node.left.object;
|
|
let temp = path.scope.maybeGenerateMemoised(left);
|
|
|
|
if (temp) nodes.push(t.expressionStatement(t.assignmentExpression("=", temp, left)));
|
|
nodes.push(buildDefaultsCallExpression(path.node, temp || left, file));
|
|
if (temp) nodes.push(temp);
|
|
|
|
path.replaceWithMultiple(nodes);
|
|
},
|
|
|
|
ExpressionStatement(path, file) {
|
|
let expr = path.node.expression;
|
|
if (!t.isAssignmentExpression(expr, { operator: "=" })) return;
|
|
|
|
if (isProtoAssignmentExpression(expr)) {
|
|
path.replaceWith(buildDefaultsCallExpression(expr, expr.left.object, file));
|
|
}
|
|
},
|
|
|
|
ObjectExpression(path, file) {
|
|
let proto;
|
|
let { node } = path;
|
|
|
|
for (let prop of (node.properties: Array)) {
|
|
if (isProtoKey(prop)) {
|
|
proto = prop.value;
|
|
pull(node.properties, prop);
|
|
}
|
|
}
|
|
|
|
if (proto) {
|
|
let args = [t.objectExpression([]), proto];
|
|
if (node.properties.length) args.push(node);
|
|
path.replaceWith(t.callExpression(file.addHelper("extends"), args));
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|