remove whitespace from the end of the last newline and improve newlines for switches

This commit is contained in:
Sebastian McKenzie 2015-02-21 03:22:44 +11:00
parent 60a7e40140
commit 8d92a75190
5 changed files with 33 additions and 8 deletions

View File

@ -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

View File

@ -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);

View File

@ -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
before: node.consequent.length || parent.cases[0] === node
};
}
},
LogicalExpression: function (node) {

View File

@ -1,4 +1,5 @@
function test() {
/*
* this is comment
*/

View File

@ -20,8 +20,10 @@
switch (_ret) {
case "continue":
continue;
case "break":
break;
default:
if (typeof _ret === "object") return _ret.v;
}