optimise generator for compact mode

This commit is contained in:
Sebastian McKenzie 2015-06-24 23:15:22 +01:00
parent aa25903c05
commit 32a4d7172b
9 changed files with 61 additions and 27 deletions

View File

@ -3,6 +3,7 @@ import trimRight from "trim-right";
import isBoolean from "lodash/lang/isBoolean";
import includes from "lodash/collection/includes";
import isNumber from "lodash/lang/isNumber";
import * as t from "../types";
export default class Buffer {
constructor(position, format) {
@ -54,9 +55,10 @@ export default class Buffer {
this.space();
}
space() {
if (this.format.compact) return;
if (this.buf && !this.isLast(" ") && !this.isLast("\n")) {
space(force?) {
if (!force && this.format.compact) return;
if (force || this.buf && !this.isLast(" ") && !this.isLast("\n")) {
this.push(" ");
}
}
@ -155,8 +157,12 @@ export default class Buffer {
this.buf += str;
}
endsWith(str) {
return this.buf.slice(-str.length) === str;
endsWith(str, buf = this.buf) {
if (str.length === 1) {
return buf[buf.length - 1] === str;
} else {
return buf.slice(-str.length) === str;
}
}
isLast(cha) {

View File

@ -2,19 +2,19 @@ import isNumber from "lodash/lang/isNumber";
import * as t from "../../types";
export function UnaryExpression(node, print) {
var hasSpace = /[a-z]$/.test(node.operator);
var needsSpace = /[a-z]$/.test(node.operator);
var arg = node.argument;
if (t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) {
hasSpace = true;
needsSpace = true;
}
if (t.isUnaryExpression(arg) && arg.operator === "!") {
hasSpace = false;
needsSpace = false;
}
this.push(node.operator);
if (hasSpace) this.push(" ");
if (needsSpace) this.push(" ");
print.plain(node.argument);
}
@ -83,19 +83,16 @@ export function CallExpression(node, print) {
this.push("(");
var separator = ",";
var isPrettyCall = node._prettyCall && !this.format.retainLines;
var isPrettyCall = node._prettyCall && !this.format.retainLines && !this.format.compact;
var separator;
if (isPrettyCall) {
separator += "\n";
separator = ",\n";
this.newline();
this.indent();
} else {
separator += " ";
}
print.list(node.arguments, { separator: separator });
print.list(node.arguments, { separator });
if (isPrettyCall) {
this.newline();
@ -141,9 +138,22 @@ export function AssignmentPattern(node, print) {
export function AssignmentExpression(node, print) {
// todo: add cases where the spaces can be dropped when in compact mode
print.plain(node.left);
this.push(" ");
var spaces = node.operator === "in" || node.operator === "instanceof";
this.space(spaces);
this.push(node.operator);
this.push(" ");
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: "--" })
}
this.space(spaces);
print.plain(node.right);
}

View File

@ -42,7 +42,7 @@ export function _method(node, print) {
}
this._params(value, print);
this.push(" ");
this.space();
print.plain(value.body);
}

View File

@ -190,16 +190,29 @@ export function VariableDeclaration(node, print, parent) {
}
}
var sep = ",";
//
// use a pretty separator when we aren't in compact mode, have initializers and don't have retainLines on
// this will format declarations like:
//
// var foo = "bar", bar = "foo";
//
// into
//
// var foo = "bar",
// bar = "foo";
//
var sep;
if (!this.format.compact && !this.format.concise && hasInits && !this.format.retainLines) {
sep += `\n${repeating(" ", node.kind.length + 1)}`;
} else {
sep += " ";
sep = `,\n${repeating(" ", node.kind.length + 1)}`;
}
//
print.list(node.declarations, { separator: sep });
if (t.isFor(parent)) {
// don't give semicolons to these nodes since they'll be inserted in the parent generator
if (parent.left === node || parent.init === node) return;
}

View File

@ -80,7 +80,7 @@ export function ArrayExpression(node, print) {
// both (all) of the holes.
this.push(",");
} else {
if (i > 0) this.push(" ");
if (i > 0) this.space();
print.plain(elem);
if (i < len - 1) this.push(",");
}

View File

@ -128,8 +128,7 @@ class CodeGenerator {
// catch up to this nodes newline if we're behind
if (node.loc && this.format.retainLines && this.buffer.buf) {
var needsParens = false;
if (!leftParenPrinted && parent &&
this.position.line < node.loc.start.line && t.isTerminatorless(parent)) {
if (!leftParenPrinted && parent && this.position.line < node.loc.start.line && t.isTerminatorless(parent)) {
needsParens = true;
this._push("(");
}

View File

@ -18,7 +18,11 @@ export default class NodePrinter {
}
list(items, opts = {}) {
if (opts.separator == null) opts.separator = ", ";
if (opts.separator == null) {
opts.separator = ",";
if (!this.generator.format.compact) opts.separator += " ";
}
return this.join(items, opts);
}

View File

@ -230,6 +230,7 @@ export function unshiftContext(context) {
*/
export function setup(parentPath, container, listKey, key) {
this.inList = !!listKey;
this.listKey = listKey;
this.parentKey = listKey || key;
this.container = container;

View File

@ -21,6 +21,7 @@ export default class NodePath {
this.context = null;
this.container = null;
this.listKey = null;
this.inList = false;
this.parentKey = null;
this.key = null;
this.node = null;