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

View File

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