diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index ae6e174400..9bfa757816 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -66,7 +66,6 @@ Buffer.prototype.removeLast = function (cha) { }; Buffer.prototype.newline = function (i, removeLast) { - if (!this.buf) return; if (this.format.compact) return; if (_.isBoolean(i)) { @@ -88,7 +87,10 @@ Buffer.prototype.newline = function (i, removeLast) { if (removeLast && this.isLast("\n")) this.removeLast("\n"); this.removeLast(" "); + + // remove whitespace if last character was a newline this.buf = this.buf.replace(/\n +$/, "\n"); + this._push("\n"); }; diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 842e397899..63919c94d3 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -138,6 +138,9 @@ CodeGenerator.prototype.print = function (node, parent, opts) { var needs = n.needsWhitespaceAfter; if (leading) needs = n.needsWhitespaceBefore; lines += needs(node, parent); + + // generated nodes can't add starting file whitespace + if (!self.buffer.get()) lines = 0; } self.newline(lines);