fix corner case in comments (#3500)

This commit is contained in:
Alex Lam S.L
2019-10-20 03:21:30 +08:00
committed by GitHub
parent 6b4886c908
commit 543dd7d3d7
2 changed files with 76 additions and 45 deletions

View File

@@ -452,7 +452,7 @@ function OutputStream(options) {
var self = this; var self = this;
var scan = node instanceof AST_Exit && node.value; var scan = node instanceof AST_Exit && node.value;
var comments = dump(node); var comments = dump(node);
if (!comments) return; if (!comments) comments = [];
if (scan) { if (scan) {
var tw = new TreeWalker(function(node) { var tw = new TreeWalker(function(node) {

View File

@@ -48,53 +48,84 @@ describe("comments", function() {
} }
}); });
it("Should handle comment within return correctly", function() { describe("comment within return", function() {
var result = UglifyJS.minify([ it("Should handle leading return", function() {
"function unequal(x, y) {", var result = UglifyJS.minify([
" return (", "function unequal(x, y) {",
" // Either one", " return (",
" x < y", " // Either one",
" ||", " x < y",
" y < x", " ||",
" );", " y < x",
"}", " );",
].join("\n"), { "}",
compress: false, ].join("\n"), {
mangle: false, compress: false,
output: { mangle: false,
beautify: true, output: {
comments: "all", beautify: true,
}, comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function unequal(x, y) {",
" // Either one",
" return x < y || y < x;",
"}",
].join("\n"));
}); });
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function unequal(x, y) {",
" // Either one",
" return x < y || y < x;",
"}",
].join("\n"));
});
it("Should handle comment folded into return correctly", function() { it("Should handle trailing return", function() {
var result = UglifyJS.minify([ var result = UglifyJS.minify([
"function f() {", "function unequal(x) {",
" /* boo */ x();", " var y;",
" return y();", " return (",
"}", " // Either one",
].join("\n"), { " x < y",
mangle: false, " ||",
output: { " y < x",
beautify: true, " );",
comments: "all", "}",
}, ].join("\n"), {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function unequal(x) {",
" var y;",
" // Either one",
" return x < y || y < x;",
"}",
].join("\n"));
});
it("Should handle comment folded into return", function() {
var result = UglifyJS.minify([
"function f() {",
" /* boo */ x();",
" return y();",
"}",
].join("\n"), {
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function f() {",
" /* boo */",
" return x(), y();",
"}",
].join("\n"));
}); });
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function f() {",
" /* boo */",
" return x(), y();",
"}",
].join("\n"));
}); });
it("Should not drop comments after first OutputStream", function() { it("Should not drop comments after first OutputStream", function() {