Print inter-node newlines before other node items.

This commit is contained in:
Logan Smyth
2016-07-16 14:31:46 -07:00
parent 6bf52b74c0
commit 38b91235cc
2 changed files with 18 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
/* eslint max-len: 0 */
import find from "lodash/find";
import findLast from "lodash/findLast";
import repeat from "lodash/repeat";
import Buffer from "./buffer";
import * as n from "./node";
@@ -282,6 +284,8 @@ export default class Printer {
print(node, parent, opts = {}) {
if (!node) return;
this._printNewline(true, node, parent, opts);
let oldConcise = this.format.concise;
if (node._compact) {
this.format.concise = true;
@@ -304,8 +308,6 @@ export default class Printer {
this._printLeadingComments(node, parent);
this._printNewline(true, node, parent, opts);
let loc = (t.isProgram(node) || t.isFile(node)) ? null : node.loc;
this.withSource("start", loc, () => {
this[node.type](node, parent);
@@ -462,9 +464,17 @@ export default class Printer {
if (node.start != null && !node._ignoreUserWhitespace && this._whitespace) {
// user node
if (leading) {
lines = this._whitespace.getNewlinesBefore(node);
const comments = node.leadingComments;
const comment = comments && find(comments, (comment) =>
!!comment.loc && this.format.shouldPrintComment(comment.value));
lines = this._whitespace.getNewlinesBefore(comment || node);
} else {
lines = this._whitespace.getNewlinesAfter(node);
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

View File

@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.test = undefined;
/*after*/
/*before*/require("foo"); /*after*/
/*after*/ /*before*/require("foo"); /*after*/
/*before*/require("foo-bar"); /*after*/
@@ -13,11 +13,9 @@ exports.test = undefined;
var /*before*/_foo = require("foo2") /*after*/;
/*before*/
var _foo2 = babelHelpers.interopRequireDefault(_foo);
/*before*/var _foo2 = babelHelpers.interopRequireDefault(_foo);
/*after*/
var /*before*/_foo3 = require("foo3") /*after*/;
/*after*/var /*before*/_foo3 = require("foo3") /*after*/;
/*before*/var /*after*/foo2 = babelHelpers.interopRequireWildcard(_foo3);