[generator] bug fix

- Fix Whitespace with empty token list
- Force a newline for line comments in concise mode
This commit is contained in:
Jason 2016-02-14 13:42:12 +08:00
parent 39c9066e40
commit 28ca3f7f3a
7 changed files with 29 additions and 10 deletions

View File

@ -23,7 +23,7 @@ export function BlockStatement(node: Object) {
if (node.directives && node.directives.length) this.newline(); if (node.directives && node.directives.length) this.newline();
this.printSequence(node.body, node, { indent: true }); this.printSequence(node.body, node, { indent: true });
if (!this.format.retainLines) this.removeLast("\n"); if (!this.format.retainLines && !this.format.concise) this.removeLast("\n");
this.rightBrace(); this.rightBrace();
} else { } else {
this.push("}"); this.push("}");

View File

@ -299,7 +299,7 @@ export default class Printer extends Buffer {
// force a newline for line comments when retainLines is set in case the next printed node // force a newline for line comments when retainLines is set in case the next printed node
// doesn't catch up // doesn't catch up
if ((this.format.compact || this.format.retainLines) && comment.type === "CommentLine") { if ((this.format.compact || this.format.concise || this.format.retainLines) && comment.type === "CommentLine") {
val += "\n"; val += "\n";
} }

View File

@ -83,11 +83,12 @@ export default class Whitespace {
*/ */
_findToken(test: Function, start: number, end: number): number { _findToken(test: Function, start: number, end: number): number {
if (start >= end) return -1;
const middle = (start + end) >>> 1; const middle = (start + end) >>> 1;
const match: number = test(this.tokens[middle]); const match: number = test(this.tokens[middle]);
if (match < 0 && end > middle) { if (match < 0) {
return this._findToken(test, middle + 1, end); return this._findToken(test, middle + 1, end);
} else if (match > 0 && start < middle) { } else if (match > 0) {
return this._findToken(test, start, middle); return this._findToken(test, start, middle);
} else if (match === 0) { } else if (match === 0) {
return middle; return middle;

View File

@ -0,0 +1,4 @@
{
print("hello");
// comment
}

View File

@ -0,0 +1,2 @@
{ print("hello"); // comment
}

View File

@ -0,0 +1,3 @@
{
"concise": true
}

View File

@ -1,3 +1,4 @@
var Whitespace = require("../lib/whitespace");
var generate = require("../lib"); var generate = require("../lib");
var assert = require("assert"); var assert = require("assert");
var parse = require("babylon").parse; var parse = require("babylon").parse;
@ -44,6 +45,14 @@ suite("programmatic generation", function() {
}); });
}); });
suite("whitespace", function () {
test("empty token list", function () {
var w = new Whitespace([]);
assert.equal(w.getNewlinesBefore(t.stringLiteral('1')), 0);
});
});
var suites = require("babel-helper-fixtures").default(__dirname + "/fixtures"); var suites = require("babel-helper-fixtures").default(__dirname + "/fixtures");
suites.forEach(function (testSuite) { suites.forEach(function (testSuite) {