fix output of imported AST (#2771)

This commit is contained in:
Alex Lam S.L
2018-01-12 01:05:49 +08:00
committed by GitHub
parent 1c9e13f47d
commit 6a696d0a7b

View File

@@ -454,78 +454,78 @@ function OutputStream(options) {
function prepend_comments(node) { function prepend_comments(node) {
var self = this; var self = this;
var start = node.start; var start = node.start;
if (!(start.comments_before && start.comments_before._dumped === self)) { if (!start) return;
var comments = start.comments_before; if (start.comments_before && start.comments_before._dumped === self) return;
if (!comments) { var comments = start.comments_before;
comments = start.comments_before = []; if (!comments) {
} comments = start.comments_before = [];
comments._dumped = self; }
comments._dumped = self;
if (node instanceof AST_Exit && node.value) { if (node instanceof AST_Exit && node.value) {
var tw = new TreeWalker(function(node) { var tw = new TreeWalker(function(node) {
var parent = tw.parent(); var parent = tw.parent();
if (parent instanceof AST_Exit if (parent instanceof AST_Exit
|| parent instanceof AST_Binary && parent.left === node || parent instanceof AST_Binary && parent.left === node
|| parent.TYPE == "Call" && parent.expression === node || parent.TYPE == "Call" && parent.expression === node
|| parent instanceof AST_Conditional && parent.condition === node || parent instanceof AST_Conditional && parent.condition === node
|| parent instanceof AST_Dot && parent.expression === node || parent instanceof AST_Dot && parent.expression === node
|| parent instanceof AST_Sequence && parent.expressions[0] === node || parent instanceof AST_Sequence && parent.expressions[0] === node
|| parent instanceof AST_Sub && parent.expression === node || parent instanceof AST_Sub && parent.expression === node
|| parent instanceof AST_UnaryPostfix) { || parent instanceof AST_UnaryPostfix) {
var text = node.start.comments_before; var text = node.start.comments_before;
if (text && text._dumped !== self) { if (text && text._dumped !== self) {
text._dumped = self; text._dumped = self;
comments = comments.concat(text); comments = comments.concat(text);
}
} else {
return true;
} }
}); } else {
tw.push(node); return true;
node.value.walk(tw);
}
if (current_pos == 0) {
if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
print("#!" + comments.shift().value + "\n");
indent();
}
var preamble = options.preamble;
if (preamble) {
print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
}
}
comments = comments.filter(comment_filter, node);
if (comments.length == 0) return;
var last_nlb = /(^|\n) *$/.test(OUTPUT);
comments.forEach(function(c, i) {
if (!last_nlb) {
if (c.nlb) {
print("\n");
indent();
last_nlb = true;
} else if (i > 0) {
space();
}
}
if (/comment[134]/.test(c.type)) {
print("//" + c.value.replace(/[@#]__PURE__/g, ' ') + "\n");
indent();
last_nlb = true;
} else if (c.type == "comment2") {
print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
last_nlb = false;
} }
}); });
tw.push(node);
node.value.walk(tw);
}
if (current_pos == 0) {
if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
print("#!" + comments.shift().value + "\n");
indent();
}
var preamble = options.preamble;
if (preamble) {
print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
}
}
comments = comments.filter(comment_filter, node);
if (comments.length == 0) return;
var last_nlb = /(^|\n) *$/.test(OUTPUT);
comments.forEach(function(c, i) {
if (!last_nlb) { if (!last_nlb) {
if (start.nlb) { if (c.nlb) {
print("\n"); print("\n");
indent(); indent();
} else { last_nlb = true;
} else if (i > 0) {
space(); space();
} }
} }
if (/comment[134]/.test(c.type)) {
print("//" + c.value.replace(/[@#]__PURE__/g, ' ') + "\n");
indent();
last_nlb = true;
} else if (c.type == "comment2") {
print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
last_nlb = false;
}
});
if (!last_nlb) {
if (start.nlb) {
print("\n");
indent();
} else {
space();
}
} }
} }
@@ -534,35 +534,33 @@ function OutputStream(options) {
var token = node.end; var token = node.end;
if (!token) return; if (!token) return;
var comments = token[tail ? "comments_before" : "comments_after"]; var comments = token[tail ? "comments_before" : "comments_after"];
if (comments if (!comments || comments._dumped === self) return;
&& comments._dumped !== self if (!(node instanceof AST_Statement || all(comments, function(c) {
&& (node instanceof AST_Statement || all(comments, function(c) { return !/comment[134]/.test(c.type);
return !/comment[134]/.test(c.type); }))) return;
}))) { comments._dumped = self;
comments._dumped = self; var insert = OUTPUT.length;
var insert = OUTPUT.length; comments.filter(comment_filter, node).forEach(function(c, i) {
comments.filter(comment_filter, node).forEach(function(c, i) { need_space = false;
need_space = false; if (need_newline_indented) {
if (need_newline_indented) { print("\n");
print("\n"); indent();
indent(); need_newline_indented = false;
need_newline_indented = false; } else if (c.nlb && (i > 0 || !/(^|\n) *$/.test(OUTPUT))) {
} else if (c.nlb && (i > 0 || !/(^|\n) *$/.test(OUTPUT))) { print("\n");
print("\n"); indent();
indent(); } else if (i > 0 || !tail) {
} else if (i > 0 || !tail) { space();
space(); }
} if (/comment[134]/.test(c.type)) {
if (/comment[134]/.test(c.type)) { print("//" + c.value.replace(/[@#]__PURE__/g, ' '));
print("//" + c.value.replace(/[@#]__PURE__/g, ' ')); need_newline_indented = true;
need_newline_indented = true; } else if (c.type == "comment2") {
} else if (c.type == "comment2") { print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/"); need_space = true;
need_space = true; }
} });
}); if (OUTPUT.length > insert) newline_insert = insert;
if (OUTPUT.length > insert) newline_insert = insert;
}
} }
var stack = []; var stack = [];