Skip to content

Commit

Permalink
Merge pull request #3121 from amasad/fix-T6736
Browse files Browse the repository at this point in the history
Fix spacing in binary expression when right is a binary expression and has a unary on the left
  • Loading branch information
amasad committed Dec 1, 2015
2 parents 2f5b953 + 2efb677 commit 27252b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/babel-generator/src/generators/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ export function AssignmentExpression(node: Object, parent: Object) {
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.isUpdateExpression(node.right, { prefix: true, operator: node.operator + node.operator }) ||
(t.isBinaryExpression(node.right) &&
t.isUnaryExpression(getLeftMost(node.right), { prefix: true, operator: node.operator }));

}

Expand Down Expand Up @@ -230,3 +232,10 @@ export function MetaProperty(node: Object) {
this.push(".");
this.print(node.property, node);
}

function getLeftMost(binaryExpr) {
if (!t.isBinaryExpression(binaryExpr)) {
return binaryExpr;
}
return getLeftMost(binaryExpr.left);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
1 && 1;
1 + +1;
x + ++y;
(a+(+b)*2);
a + +b * 2 * 2 * 2;
a - -b;
1 + -b;
1 - --b;
a - -b * 2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1*1;1&&1;1+ +1;x+ ++y;
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;

0 comments on commit 27252b2

Please sign in to comment.