Track sourcemap location on a stack - fixes T7255

This commit is contained in:
Logan Smyth
2016-04-10 21:16:11 -07:00
parent 7d6d4c204b
commit 76bb1dffaa
15 changed files with 118 additions and 77 deletions

View File

@@ -51,9 +51,10 @@ export default class Printer extends Buffer {
if (opts.before) opts.before();
this.map.mark(node);
this._print(node, parent);
let loc = (t.isProgram(node) || t.isFile(node)) ? null : node.loc;
this.withSource("start", loc, () => {
this._print(node, parent);
});
// Check again if any of our children may have left an aux comment on the stack
if (node.loc) this.printAuxAfterComment();
@@ -64,7 +65,6 @@ export default class Printer extends Buffer {
// end
this._printStack.pop();
if (parent) this.map.mark(parent);
if (opts.after) opts.after();
this.format.concise = oldConcise;
@@ -268,46 +268,50 @@ export default class Printer extends Buffer {
this.printedCommentStarts[comment.start] = true;
}
this.catchUp(comment);
// Exclude comments from source mappings since they will only clutter things.
this.withSource(null, null, () => {
this.catchUp(comment);
// whitespace before
this.newline(this.whitespace.getNewlinesBefore(comment));
// whitespace before
this.newline(this.whitespace.getNewlinesBefore(comment));
let column = this.position.column;
let val = this.generateComment(comment);
let column = this.position.column;
let val = this.generateComment(comment);
if (column && !this.isLast(["\n", " ", "[", "{"])) {
this._push(" ");
column++;
}
//
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) {
let offset = comment.loc && comment.loc.start.column;
if (offset) {
let newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
if (column && !this.isLast(["\n", " ", "[", "{"])) {
this._push(" ");
column++;
}
let indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
}
//
if (comment.type === "CommentBlock" && this.format.indent.adjustMultilineComment) {
let offset = comment.loc && comment.loc.start.column;
if (offset) {
let newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
val = val.replace(newlineRegex, "\n");
}
if (column === 0) {
val = this.getIndent() + val;
}
let indent = Math.max(this.indentSize(), column);
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
}
// force a newline for line comments when retainLines is set in case the next printed node
// doesn't catch up
if ((this.format.compact || this.format.concise || this.format.retainLines) && comment.type === "CommentLine") {
val += "\n";
}
if (column === 0) {
val = this.getIndent() + val;
}
//
this._push(val);
// force a newline for line comments when retainLines is set in case the next printed node
// doesn't catch up
if ((this.format.compact || this.format.concise || this.format.retainLines) &&
comment.type === "CommentLine") {
val += "\n";
}
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
//
this._push(val);
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
});
}
printComments(comments?: Array<Object>) {