From a924c9c21887bd8036fe09275992a68b5f107eab Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 8 Jan 2015 00:54:01 +1100 Subject: [PATCH] fix up declarations being split up and use a new algorithm to determine whether or not we should align them --- lib/6to5/generation/generators/statements.js | 10 ++++++---- .../transformation/transformers/es6-destructuring.js | 7 ++++++- .../es6-destructuring/multiple/actual.js | 3 ++- .../es6-modules-ignore/exports-variable/expected.js | 3 ++- .../es7-abstract-references/private/expected.js | 9 ++++++--- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index fd2ddba1ec..959e673ad5 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -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 += " "; diff --git a/lib/6to5/transformation/transformers/es6-destructuring.js b/lib/6to5/transformation/transformers/es6-destructuring.js index b5decb349c..675fa483f9 100644 --- a/lib/6to5/transformation/transformers/es6-destructuring.js +++ b/lib/6to5/transformation/transformers/es6-destructuring.js @@ -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)); } diff --git a/test/fixtures/transformation/es6-destructuring/multiple/actual.js b/test/fixtures/transformation/es6-destructuring/multiple/actual.js index 41dc62261f..09249c483c 100644 --- a/test/fixtures/transformation/es6-destructuring/multiple/actual.js +++ b/test/fixtures/transformation/es6-destructuring/multiple/actual.js @@ -1 +1,2 @@ -var { x, y } = coords, foo = "bar"; +var { x, y } = coords, + foo = "bar"; diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js index 854aad79f8..11ae89321c 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js @@ -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; diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index 8fca7f836d..297188a197 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -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 () {};