From 8d92a7519015404a9aa2a07abbbc8cbab4c09a64 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 21 Feb 2015 03:22:44 +1100 Subject: [PATCH] remove whitespace from the end of the last newline and improve newlines for switches --- lib/babel/generation/buffer.js | 24 +++++++++++++++++++ lib/babel/generation/generators/statements.js | 3 +-- lib/babel/generation/node/whitespace.js | 11 ++++----- .../expected.js | 1 + .../for-break-continue-return/expected.js | 2 ++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/babel/generation/buffer.js b/lib/babel/generation/buffer.js index a061718d16..330a57b250 100644 --- a/lib/babel/generation/buffer.js +++ b/lib/babel/generation/buffer.js @@ -111,9 +111,33 @@ Buffer.prototype._newline = function (removeLast) { if (removeLast && this.isLast("\n")) this.removeLast("\n"); this.removeLast(" "); + this._removeSpacesAfterLastNewline(); this._push("\n"); }; +/** + * If buffer ends with a newline and some spaces after it, trim those spaces. + */ + +Buffer.prototype._removeSpacesAfterLastNewline = function () { + var lastNewlineIndex = this.buf.lastIndexOf("\n"); + if (lastNewlineIndex === -1) + return; + + var index = this.buf.length - 1; + while (index > lastNewlineIndex) { + if (this.buf[index] !== " ") { + break; + } + + index--; + } + + if (index === lastNewlineIndex) { + this.buf = this.buf.substring(0, index + 1); + } +}; + Buffer.prototype.push = function (str, noIndent) { if (!this.format.compact && this._indent && !noIndent && str !== "\n") { // we have an indent level and we aren't pushing a newline diff --git a/lib/babel/generation/generators/statements.js b/lib/babel/generation/generators/statements.js index fe0cf18e71..01dd142849 100644 --- a/lib/babel/generation/generators/statements.js +++ b/lib/babel/generation/generators/statements.js @@ -149,11 +149,10 @@ exports.SwitchStatement = function (node, print) { this.space(); this.push("{"); print.sequence(node.cases, { indent: true }); - this.removeLast("\n"); this.push("}"); }; -exports.SwitchCase = function (node, print) { +exports.SwitchCase = function (node, print, parent) { if (node.test) { this.push("case "); print(node.test); diff --git a/lib/babel/generation/node/whitespace.js b/lib/babel/generation/node/whitespace.js index ae09727259..7b2bc55c0a 100644 --- a/lib/babel/generation/node/whitespace.js +++ b/lib/babel/generation/node/whitespace.js @@ -41,7 +41,8 @@ var isHelper = function (node) { }; var isType = function (node) { - return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || t.isIdentifier(node) || t.isMemberExpression(node); + return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || + t.isIdentifier(node) || t.isMemberExpression(node); }; exports.nodes = { @@ -56,11 +57,9 @@ exports.nodes = { }, SwitchCase: function (node, parent) { - if (parent.cases[0] === node) { - return { - before: true - }; - } + return { + before: node.consequent.length || parent.cases[0] === node + }; }, LogicalExpression: function (node) { diff --git a/test/fixtures/generation/comments/2-space-multi-comment-with-space/expected.js b/test/fixtures/generation/comments/2-space-multi-comment-with-space/expected.js index ae61df36f1..62aee76944 100644 --- a/test/fixtures/generation/comments/2-space-multi-comment-with-space/expected.js +++ b/test/fixtures/generation/comments/2-space-multi-comment-with-space/expected.js @@ -1,4 +1,5 @@ function test() { + /* * this is comment */ diff --git a/test/fixtures/transformation/es6-block-scoping/for-break-continue-return/expected.js b/test/fixtures/transformation/es6-block-scoping/for-break-continue-return/expected.js index 2748ecf355..72401905e7 100644 --- a/test/fixtures/transformation/es6-block-scoping/for-break-continue-return/expected.js +++ b/test/fixtures/transformation/es6-block-scoping/for-break-continue-return/expected.js @@ -20,8 +20,10 @@ switch (_ret) { case "continue": continue; + case "break": break; + default: if (typeof _ret === "object") return _ret.v; }