[generator] bug fix
- Fix Whitespace with empty token list - Force a newline for line comments in concise mode
This commit is contained in:
parent
39c9066e40
commit
28ca3f7f3a
@ -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("}");
|
||||||
|
|||||||
@ -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";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
print("hello");
|
||||||
|
// comment
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
{ print("hello"); // comment
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"concise": true
|
||||||
|
}
|
||||||
@ -1,9 +1,10 @@
|
|||||||
var generate = require("../lib");
|
var Whitespace = require("../lib/whitespace");
|
||||||
var assert = require("assert");
|
var generate = require("../lib");
|
||||||
var parse = require("babylon").parse;
|
var assert = require("assert");
|
||||||
var chai = require("chai");
|
var parse = require("babylon").parse;
|
||||||
var t = require("babel-types");
|
var chai = require("chai");
|
||||||
var _ = require("lodash");
|
var t = require("babel-types");
|
||||||
|
var _ = require("lodash");
|
||||||
|
|
||||||
suite("generation", function () {
|
suite("generation", function () {
|
||||||
test("completeness", function () {
|
test("completeness", function () {
|
||||||
@ -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) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user