Use the leftMost from the right to check if we need spaces in BinaryExpressions

This commit is contained in:
Amjad Masad 2015-12-14 13:36:52 -08:00
parent 7b25b33ac6
commit 007ef5cdbb
3 changed files with 11 additions and 8 deletions

View File

@ -174,13 +174,14 @@ export function AssignmentExpression(node: Object, parent: Object) {
// http://javascript.spec.whatwg.org/#comment-syntax
spaces = node.operator === "<" &&
t.isUnaryExpression(node.right, { prefix: true, operator: "!" }) &&
t.isUnaryExpression(node.right.argument, { prefix: true, operator: "--" }) ||
// Need spaces for operators of the same kind to avoid: `a+++b`
t.isUnaryExpression(node.right, { prefix: true, operator: node.operator }) ||
t.isUpdateExpression(node.right, { prefix: true, operator: node.operator + node.operator }) ||
(t.isBinaryExpression(node.right) &&
t.isUnaryExpression(getLeftMost(node.right), { prefix: true, operator: node.operator }));
t.isUnaryExpression(node.right.argument, { prefix: true, operator: "--" });
// Need spaces for operators of the same kind to avoid: `a+++b`
if (!spaces) {
let right = getLeftMost(node.right);
spaces = t.isUnaryExpression(right, { prefix: true, operator: node.operator }) ||
t.isUpdateExpression(right, { prefix: true, operator: node.operator + node.operator });
}
}
if (spaces) this.push(" ");

View File

@ -7,4 +7,6 @@ a + +b * 2 * 2 * 2;
a - -b;
1 + -b;
1 - --b;
a - -b * 2
a - -b * 2;
1 - (--t) * t;

View File

@ -1 +1 @@
1*1;1&&1;1+ +1;x+ ++y;a+ +b*2;a+ +b*2*2*2;a- -b;1+-b;1- --b;a- -b*2;
1*1;1&&1;1+ +1;x+ ++y;a+ +b*2;a+ +b*2*2*2;a- -b;1+-b;1- --b;a- -b*2;1- --t*t;