Skip to content

Commit

Permalink
fix #180 by adding parens when there's a comment between the return a…
Browse files Browse the repository at this point in the history
…nd the returned value
  • Loading branch information
fabiosantoscode committed Sep 25, 2019
1 parent beca067 commit 165e9f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
17 changes: 12 additions & 5 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ import {
RESERVED_WORDS,
} from "./parse.js";

var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
const EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
const CODE_LINE_BREAK = 10;
const CODE_SPACE = 32;

Expand Down Expand Up @@ -592,7 +592,7 @@ function OutputStream(options) {
var self = this;
var start = node.start;
if (!start) return;
var printed_comments = self.printed_comments();
var printed_comments = self.printed_comments;
if (start.comments_before && printed_comments.has(start.comments_before)) return;
var comments = start.comments_before;
if (!comments) {
Expand Down Expand Up @@ -674,7 +674,7 @@ function OutputStream(options) {
var self = this;
var token = node.end;
if (!token) return;
var printed_comments = self.printed_comments();
var printed_comments = self.printed_comments;
var comments = token[tail ? "comments_before" : "comments_after"];
if (!comments || printed_comments.has(comments)) return;
if (!(node instanceof AST_Statement || comments.every((c) =>
Expand Down Expand Up @@ -750,7 +750,7 @@ function OutputStream(options) {
with_square : with_square,
add_mapping : add_mapping,
option : function(opt) { return options[opt]; },
printed_comments: () => printed_comments,
printed_comments: printed_comments,
prepend_comments: readonly ? noop : prepend_comments,
append_comments : readonly || comment_filter === return_false ? noop : append_comments,
line : function() { return current_line; },
Expand Down Expand Up @@ -1322,7 +1322,14 @@ function OutputStream(options) {
output.print(kind);
if (this.value) {
output.space();
this.value.print(output);
const comments = this.value.start.comments_before;
if (comments && comments.length && !output.printed_comments.has(comments)) {
output.print("(");
this.value.print(output);
output.print(")");
} else {
this.value.print(output);
}
}
output.semicolon();
});
Expand Down
16 changes: 6 additions & 10 deletions test/mocha/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ describe("comments", function() {
&& e.message === "Unexpected token: operator (>)"
&& e.line === 2
&& e.col === 0;
}
};

for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
Terser.parse(tests[i]);
}, fail, tests[i]);
assert.throws(() => Terser.parse(tests[i]), fail, tests[i]);
}
});

Expand All @@ -42,9 +40,7 @@ describe("comments", function() {
}

for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
Terser.parse(tests[i]);
}, fail, tests[i]);
assert.throws(() => Terser.parse(tests[i]), fail, tests[i]);
}
});

Expand Down Expand Up @@ -261,22 +257,22 @@ describe("comments", function() {
});

describe("comment before constant", function() {
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
var js = "function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }";

it("Should test comment before constant is retained and output after mangle.", function() {
var result = Terser.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: true },
});
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
assert.strictEqual(result.code, "function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}");
});

it("Should test code works when comments disabled.", function() {
var result = Terser.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: false },
});
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
assert.strictEqual(result.code, "function f(){var n=!1;return n}");
});
});

Expand Down

0 comments on commit 165e9f3

Please sign in to comment.