From bd876f7a4d3eb28e281905ca3e1a181e1e32437b Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 7 Dec 2014 21:59:22 +1100 Subject: [PATCH] fix starting file newlines --- lib/6to5/generation/buffer.js | 4 +++- lib/6to5/generation/generator.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) 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);