Use the presence of _whitespace to toggle its use.

This commit is contained in:
Logan Smyth 2016-07-07 19:59:28 -07:00
parent 6a74731c6b
commit e056c0b9d6
3 changed files with 10 additions and 20 deletions

View File

@ -13,21 +13,18 @@ class Generator extends Printer {
constructor(ast, opts, code) {
opts = opts || {};
let comments = ast.comments || [];
let tokens = ast.tokens || [];
const tokens = ast.tokens || [];
let format = Generator.normalizeOptions(code, opts, tokens);
let map = opts.sourceMaps ? new SourceMap(opts, code) : null;
super(format, map);
this.comments = comments;
this.tokens = tokens;
this.opts = opts;
this.ast = ast;
this._inForStatementInitCounter = 0;
this.whitespace = new Whitespace(tokens);
this._whitespace = tokens.length > 0 ? new Whitespace(tokens) : null;
}
format: {
@ -49,9 +46,7 @@ class Generator extends Printer {
auxiliaryCommentBefore: string;
auxiliaryCommentAfter: string;
whitespace: Whitespace;
comments: Array<Object>;
tokens: Array<Object>;
_whitespace: Whitespace;
opts: Object;
ast: Object;

View File

@ -443,12 +443,12 @@ export default class Printer {
let lines = 0;
if (node.start != null && !node._ignoreUserWhitespace && this.tokens.length) {
if (node.start != null && !node._ignoreUserWhitespace && this._whitespace) {
// user node
if (leading) {
lines = this.whitespace.getNewlinesBefore(node);
lines = this._whitespace.getNewlinesBefore(node);
} else {
lines = this.whitespace.getNewlinesAfter(node);
lines = this._whitespace.getNewlinesAfter(node);
}
} else {
// generated node
@ -499,7 +499,7 @@ export default class Printer {
// Exclude comments from source mappings since they will only clutter things.
this.withSource("start", comment.loc, () => {
// whitespace before
this.newline(this.whitespace.getNewlinesBefore(comment));
this.newline(this._whitespace ? this._whitespace.getNewlinesBefore(comment) : 0);
if (!this.endsWith("[") && !this.endsWith("{")) this.space();
@ -528,7 +528,8 @@ export default class Printer {
this.token(val);
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
this.newline((this._whitespace ? this._whitespace.getNewlinesAfter(comment) : 0) ||
(comment.type === "CommentLine" ? 1 : 0));
});
}

View File

@ -47,13 +47,7 @@ export default class Whitespace {
if (endToken && endToken.type.label === "eof") {
return 1;
} else {
let lines = this._getNewlinesBetween(startToken, endToken);
if (node.type === "CommentLine" && !lines) {
// line comment
return 1;
} else {
return lines;
}
return this._getNewlinesBetween(startToken, endToken);
}
}