Stanislav Sysoev 33da726638 Not null check in babel-helper-builder-binary-assignment-operator-visitor (#3647)
Fix: T7537 — https://phabricator.babeljs.io/T7537

When transforming super call in class constructor, part of ast is replaced using method "replaceWithMultiple" here:
https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-es2015-classes/src/vanilla.js#L379

It leads to removing the node (replacing it with null) here:
https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/replacement.js#L51

But parent ExpressionsStatement is still untouched and when it reaches visitor generated in here
https://github.com/babel/babel/blob/master/packages/babel-helper-builder-binary-assignment-operator-visitor/src/index.js#L18

It blows up because expression is null from previous visitors.
2016-08-31 14:41:49 -04:00

54 lines
1.4 KiB
JavaScript

import explode from "babel-helper-explode-assignable-expression";
import * as t from "babel-types";
export default function (opts: {
build: Function;
operator: string;
}): Object {
let visitor = {};
function isAssignment(node) {
return node && node.operator === opts.operator + "=";
}
function buildAssignment(left, right) {
return t.assignmentExpression("=", left, right);
}
visitor.ExpressionStatement = function (path, file) {
// hit the `AssignmentExpression` one below
if (path.isCompletionRecord()) return;
let expr = path.node.expression;
if (!isAssignment(expr)) return;
let nodes = [];
let exploded = explode(expr.left, nodes, file, path.scope, true);
nodes.push(t.expressionStatement(
buildAssignment(exploded.ref, opts.build(exploded.uid, expr.right))
));
path.replaceWithMultiple(nodes);
};
visitor.AssignmentExpression = function (path, file) {
let { node, scope } = path;
if (!isAssignment(node)) return;
let nodes = [];
let exploded = explode(node.left, nodes, file, scope);
nodes.push(buildAssignment(exploded.ref, opts.build(exploded.uid, node.right)));
path.replaceWithMultiple(nodes);
};
visitor.BinaryExpression = function (path) {
let { node } = path;
if (node.operator === opts.operator) {
path.replaceWith(opts.build(node.left, node.right));
}
};
return visitor;
}