diff --git a/lib/parse.js b/lib/parse.js index bfed3e97..ee904962 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -934,10 +934,15 @@ function parse($TEXT, options) { function expect(punc) { return expect_token("punc", punc); }; + function has_newline_before(token) { + return token.nlb || !all(token.comments_before, function(comment) { + return !comment.nlb; + }); + } + function can_insert_semicolon() { - return !options.strict && ( - S.token.nlb || is("eof") || is("punc", "}") - ); + return !options.strict + && (is("eof") || is("punc", "}") || has_newline_before(S.token)); }; function is_in_generator() { @@ -985,10 +990,10 @@ function parse($TEXT, options) { if (S.in_directives) { var token = peek(); if (S.token.raw.indexOf("\\") == -1 - && (token.nlb - || is_token(token, "eof") - || is_token(token, "punc", ";") - || is_token(token, "punc", "}"))) { + && (is_token(token, "punc", ";") + || is_token(token, "punc", "}") + || has_newline_before(token) + || is_token(token, "eof"))) { S.input.add_directive(S.token.value); } else { S.in_directives = false; @@ -1111,7 +1116,7 @@ function parse($TEXT, options) { case "throw": next(); - if (S.token.nlb) + if (has_newline_before(S.token)) croak("Illegal newline after 'throw'"); var value = expression(true); semicolon(); @@ -1287,7 +1292,7 @@ function parse($TEXT, options) { }; var arrow_function = function(start, argnames, is_async) { - if (S.token.nlb) { + if (has_newline_before(S.token)) { croak("Unexpected newline before arrow (=>)"); } @@ -2738,7 +2743,7 @@ function parse($TEXT, options) { return ex; } var val = expr_atom(allow_calls, allow_arrows); - while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) { + while (is("operator") && UNARY_POSTFIX(S.token.value) && !has_newline_before(S.token)) { if (val instanceof AST_Arrow) unexpected(); val = make_unary(AST_UnaryPostfix, S.token, val); val.start = start; diff --git a/package.json b/package.json index 71e0b421..ebf73b68 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.3.1", + "version": "3.3.2", "engines": { "node": ">=0.8.0" }, diff --git a/test/compress/issue-2652.js b/test/compress/issue-2652.js new file mode 100644 index 00000000..d3159742 --- /dev/null +++ b/test/compress/issue-2652.js @@ -0,0 +1,33 @@ +insert_semicolon: { + beautify = { + beautify: true, + comments: "all", + } + input: { + var a + /* foo */ var b + } + expect_exact: [ + "var a", + "/* foo */;", + "", + "var b;", + ] +} + +unary_postfix: { + beautify = { + beautify: true, + comments: "all", + } + input: { + a + /* foo */++b + } + expect_exact: [ + "a", + "/* foo */;", + "", + "++b;", + ] +}