fix up declarations being split up and use a new algorithm to determine whether or not we should align them

This commit is contained in:
Sebastian McKenzie
2015-01-08 00:54:01 +11:00
parent a2ed0ea9c5
commit a924c9c218
5 changed files with 22 additions and 10 deletions

View File

@@ -157,16 +157,18 @@ exports.DebuggerStatement = function () {
exports.VariableDeclaration = function (node, print, parent) {
this.push(node.kind + " ");
var hasInits = false;
var inits = 0;
var noInits = 0;
for (var i in node.declarations) {
if (node.declarations[i].init) {
hasInits = true;
break;
inits++;
} else {
noInits++;
}
}
var sep = ",";
if (hasInits) {
if (inits > noInits) { // more inits than noinits
sep += "\n" + this.getIndent(2);
} else {
sep += " ";

View File

@@ -303,7 +303,12 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
if (t.isPattern(pattern) && patternId) {
pushPattern(opts);
t.inherits(nodes[nodes.length - 1], declar);
if (+i !== node.declarations.length - 1) {
// we aren't the last declarator so let's just make the
// last transformed node inherit from us
t.inherits(nodes[nodes.length - 1], declar);
}
} else {
nodes.push(t.inherits(buildVariableAssign(opts, declar.id, declar.init), declar));
}

View File

@@ -1 +1,2 @@
var { x, y } = coords, foo = "bar";
var { x, y } = coords,
foo = "bar";

View File

@@ -1,7 +1,8 @@
"use strict";
var foo = 1;
var foo = 1, bar = 2;
var foo = 1,
bar = 2;
var foo2 = function () {};
var foo3 = undefined;
var foo4 = 2;

View File

@@ -1,9 +1,11 @@
"use strict";
var A = new WeakMap();
var B = new WeakMap(), C = new WeakMap();
var B = new WeakMap(),
C = new WeakMap();
var D = (function () {
var F = new WeakMap(), G = new WeakMap();
var F = new WeakMap(),
G = new WeakMap();
var E = new WeakMap();
var D = function D() {};
@@ -11,7 +13,8 @@ var D = (function () {
})();
var H = (function () {
var J = new WeakMap(), K = new WeakMap();
var J = new WeakMap(),
K = new WeakMap();
var I = new WeakMap();
var _class = function () {};