Remove whitespace generation (#5833)
* Remove whitespace generation and rely on default printing Changes to printing: * Add newline after last empty SwitchCase * Add newlines around block comments if they are non-flow comments or contain newlines * Fix a few more fixtures
This commit is contained in:
committed by
Brian Ng
parent
bc29145465
commit
b3372a572d
@@ -1,10 +1,7 @@
|
||||
import find from "lodash/find";
|
||||
import findLast from "lodash/findLast";
|
||||
import isInteger from "lodash/isInteger";
|
||||
import repeat from "lodash/repeat";
|
||||
import Buffer from "./buffer";
|
||||
import * as n from "./node";
|
||||
import Whitespace from "./whitespace";
|
||||
import * as t from "babel-types";
|
||||
|
||||
import * as generatorFunctions from "./generators";
|
||||
@@ -32,17 +29,15 @@ export type Format = {
|
||||
};
|
||||
|
||||
export default class Printer {
|
||||
constructor(format, map, tokens) {
|
||||
constructor(format, map) {
|
||||
this.format = format || {};
|
||||
this._buf = new Buffer(map);
|
||||
this._whitespace = tokens.length > 0 ? new Whitespace(tokens) : null;
|
||||
}
|
||||
|
||||
format: Format;
|
||||
inForStatementInitCounter: number = 0;
|
||||
|
||||
_buf: Buffer;
|
||||
_whitespace: Whitespace;
|
||||
_printStack: Array<Node> = [];
|
||||
_indent: number = 0;
|
||||
_insideAux: boolean = false;
|
||||
@@ -498,43 +493,13 @@ export default class Printer {
|
||||
}
|
||||
|
||||
let lines = 0;
|
||||
|
||||
if (node.start != null && !node._ignoreUserWhitespace && this._whitespace) {
|
||||
// user node
|
||||
if (leading) {
|
||||
const comments = node.leadingComments;
|
||||
const comment =
|
||||
comments &&
|
||||
find(
|
||||
comments,
|
||||
comment =>
|
||||
!!comment.loc && this.format.shouldPrintComment(comment.value),
|
||||
);
|
||||
|
||||
lines = this._whitespace.getNewlinesBefore(comment || node);
|
||||
} else {
|
||||
const comments = node.trailingComments;
|
||||
const comment =
|
||||
comments &&
|
||||
findLast(
|
||||
comments,
|
||||
comment =>
|
||||
!!comment.loc && this.format.shouldPrintComment(comment.value),
|
||||
);
|
||||
|
||||
lines = this._whitespace.getNewlinesAfter(comment || node);
|
||||
}
|
||||
} else {
|
||||
// generated node
|
||||
// don't add newlines at the beginning of the file
|
||||
if (this._buf.hasContent()) {
|
||||
if (!leading) lines++; // always include at least a single line after
|
||||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
||||
|
||||
let needs = n.needsWhitespaceAfter;
|
||||
if (leading) needs = n.needsWhitespaceBefore;
|
||||
const needs = leading ? n.needsWhitespaceBefore : n.needsWhitespaceAfter;
|
||||
if (needs(node, parent)) lines++;
|
||||
|
||||
// generated nodes can't add starting file whitespace
|
||||
if (!this._buf.hasContent()) lines = 0;
|
||||
}
|
||||
|
||||
this.newline(lines);
|
||||
@@ -563,23 +528,17 @@ export default class Printer {
|
||||
this._printedCommentStarts[comment.start] = true;
|
||||
}
|
||||
|
||||
// whitespace before
|
||||
this.newline(
|
||||
this._whitespace ? this._whitespace.getNewlinesBefore(comment) : 0,
|
||||
);
|
||||
const isBlockComment = comment.type === "CommentBlock";
|
||||
|
||||
// Always add a newline before a block comment
|
||||
this.newline(this._buf.hasContent() && isBlockComment ? 1 : 0);
|
||||
|
||||
if (!this.endsWith("[") && !this.endsWith("{")) this.space();
|
||||
|
||||
let val =
|
||||
comment.type === "CommentLine"
|
||||
? `//${comment.value}\n`
|
||||
: `/*${comment.value}*/`;
|
||||
let val = !isBlockComment ? `//${comment.value}\n` : `/*${comment.value}*/`;
|
||||
|
||||
//
|
||||
if (
|
||||
comment.type === "CommentBlock" &&
|
||||
this.format.indent.adjustMultilineComment
|
||||
) {
|
||||
if (isBlockComment && this.format.indent.adjustMultilineComment) {
|
||||
const offset = comment.loc && comment.loc.start.column;
|
||||
if (offset) {
|
||||
const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
||||
@@ -600,12 +559,8 @@ export default class Printer {
|
||||
this._append(val);
|
||||
});
|
||||
|
||||
// whitespace after
|
||||
this.newline(
|
||||
(this._whitespace ? this._whitespace.getNewlinesAfter(comment) : 0) +
|
||||
// Subtract one to account for the line force-added above.
|
||||
(comment.type === "CommentLine" ? -1 : 0),
|
||||
);
|
||||
// Always add a newline after a block comment
|
||||
this.newline(isBlockComment ? 1 : 0);
|
||||
}
|
||||
|
||||
_printComments(comments?: Array<Object>) {
|
||||
|
||||
Reference in New Issue
Block a user