diff --git a/lib/output.js b/lib/output.js index b0cecf06..98452e47 100644 --- a/lib/output.js +++ b/lib/output.js @@ -201,6 +201,8 @@ function OutputStream(options) { var might_need_semicolon = false; var might_add_newline = 0; var need_newline_indented = false; + var need_space = false; + var newline_insert = -1; var last = ""; var mapping_token, mapping_name, mappings = options.source_map && []; @@ -266,6 +268,13 @@ function OutputStream(options) { indent(); } } + if (need_space && ch) { + need_space = false; + if (!/[\s;})]/.test(ch)) { + space(); + } + } + newline_insert = -1; var prev = last.charAt(last.length - 1); if (might_need_semicolon) { might_need_semicolon = false; @@ -364,7 +373,13 @@ function OutputStream(options) { } : function(col, cont) { return cont() }; var newline = options.beautify ? function() { - print("\n"); + if (newline_insert < 0) return print("\n"); + if (OUTPUT[newline_insert] != "\n") { + OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert); + current_pos++; + current_line++; + } + newline_insert++; } : options.max_line_len ? function() { ensure_line_len(); might_add_newline = OUTPUT.length; @@ -521,11 +536,16 @@ function OutputStream(options) { var comments = token[tail ? "comments_before" : "comments_after"]; if (comments && comments._dumped !== self) { comments._dumped = self; + var insert = OUTPUT.length; comments.filter(comment_filter, node).forEach(function(c, i) { - if (need_newline_indented || c.nlb) { + need_space = false; + if (need_newline_indented) { print("\n"); indent(); need_newline_indented = false; + } else if (c.nlb && (i > 0 || !/(^|\n) *$/.test(OUTPUT))) { + print("\n"); + indent(); } else if (i > 0 || !tail) { space(); } @@ -534,8 +554,10 @@ function OutputStream(options) { need_newline_indented = true; } else if (c.type == "comment2") { print("/*" + c.value + "*/"); + need_space = true; } }); + if (OUTPUT.length > insert) newline_insert = insert; } } diff --git a/lib/parse.js b/lib/parse.js index cdf3fdc1..7a6089e4 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1276,9 +1276,17 @@ function parse($TEXT, options) { case "(": next(); var ex = expression(true); - start.comments_before.orig_len = start.comments_before.length; + var orig_len = start.comments_before.length; + start.comments_before.orig_len = orig_len; [].push.apply(start.comments_before, ex.start.comments_before); ex.start.comments_before = start.comments_before; + if (orig_len == 0 && start.comments_before.length > 0) { + var comment = start.comments_before[0]; + if (!comment.nlb) { + comment.nlb = start.nlb; + start.nlb = false; + } + } start.comments_after = ex.start.comments_after; ex.start = start; expect(")");