Merge pull request #3103 from amasad/spacing

Respect spacing in compact mode
This commit is contained in:
Sebastian McKenzie 2015-11-25 14:02:05 +11:00
commit 58f512f7c4
8 changed files with 19 additions and 3 deletions

View File

@ -141,7 +141,9 @@ export function ExpressionStatement(node: Object) {
export function AssignmentPattern(node: Object) {
this.print(node.left, node);
this.push(" = ");
this.space();
this.push("=");
this.space();
this.print(node.right, node);
}
@ -158,7 +160,6 @@ export function AssignmentExpression(node: Object, parent: Object) {
this.print(node.left, node);
let spaces = !this.format.compact || node.operator === "in" || node.operator === "instanceof";
spaces = true; // todo: https://github.com/babel/babel/issues/1835
if (spaces) this.push(" ");
this.push(node.operator);
@ -168,7 +169,11 @@ 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: "--" });
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 });
}
if (spaces) this.push(" ");

View File

@ -146,6 +146,7 @@ export default class Printer extends Buffer {
printBlock(parent) {
let node = parent.body;
if (!t.isEmptyStatement(node)) {
this.space();
}

View File

@ -0,0 +1,2 @@
x = 1;
var { y = 1 } = obj;

View File

@ -0,0 +1 @@
x=1;var {y=1}=obj;

View File

@ -0,0 +1,4 @@
1 * 1;
1 && 1;
1 + +1;
x + ++y;

View File

@ -0,0 +1 @@
1*1;1&&1;1+ +1;x+ ++y;

View File

@ -0,0 +1 @@
while(true) x();

View File

@ -0,0 +1 @@
while(true)x();