Drop some space-insertion logic.

This commit is contained in:
Logan Smyth 2016-04-30 14:39:25 -07:00
parent 39896b4d91
commit 1514d0a1ad
2 changed files with 14 additions and 39 deletions

View File

@ -151,6 +151,16 @@ export default class Buffer {
*/
token(str: string) {
// space is mandatory to avoid outputting <!--
// http://javascript.spec.whatwg.org/#comment-syntax
if ((str === "--" && this.last === "!") ||
// Need spaces for operators of the same kind to avoid: `a+++b`
(str[0] === "+" && this.last === "+") ||
(str[0] === "-" && this.last === "-")) {
this.push(" ");
}
this.push(str);
}

View File

@ -10,23 +10,13 @@ const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/;
export function UnaryExpression(node: Object) {
let needsSpace = /[a-z]$/.test(node.operator);
let arg = node.argument;
if (t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) {
needsSpace = true;
}
if (t.isUnaryExpression(arg) && arg.operator === "!") {
needsSpace = false;
}
if (node.operator === "void" || node.operator === "delete" || node.operator === "typeof") {
this.word(node.operator);
this.space();
} else {
this.token(node.operator);
}
if (needsSpace) this.push(" ");
this.print(node.argument, node);
}
@ -176,31 +166,13 @@ export function AssignmentExpression(node: Object, parent: Object) {
this.print(node.left, node);
let spaces = !this.format.compact || node.operator === "in" || node.operator === "instanceof";
if (spaces) this.push(" ");
this.space();
if (node.operator === "in" || node.operator === "instanceof") {
this.word(node.operator);
} else {
this.token(node.operator);
}
if (!spaces) {
// space is mandatory to avoid outputting <!--
// 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`
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(" ");
this.space();
this.print(node.right, node);
@ -258,10 +230,3 @@ export function MetaProperty(node: Object) {
this.token(".");
this.print(node.property, node);
}
function getLeftMost(binaryExpr) {
if (!t.isBinaryExpression(binaryExpr)) {
return binaryExpr;
}
return getLeftMost(binaryExpr.left);
}