diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-05 12:16:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-05 12:16:02 +0800 |
commit | eb98a7f2f38f5de16b50560199ee7ec719a1e945 (patch) | |
tree | 6e7f1e8cfbb32a1a3d728a0910a8887ace2459fc /lib/output.js | |
parent | 78d1bb92d4560b73099afddd3bd2a85641bf3002 (diff) | |
download | tracifyjs-eb98a7f2f38f5de16b50560199ee7ec719a1e945.tar.gz tracifyjs-eb98a7f2f38f5de16b50560199ee7ec719a1e945.zip |
fix handling of shebang and preamble (#1545)
fixes #1332
Diffstat (limited to 'lib/output.js')
-rw-r--r-- | lib/output.js | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/lib/output.js b/lib/output.js index 4a0a1e0e..10fed135 100644 --- a/lib/output.js +++ b/lib/output.js @@ -46,17 +46,8 @@ var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/; function is_some_comments(comment) { - var text = comment.value; - var type = comment.type; - if (type == "comment2") { - // multiline comment - return /@preserve|@license|@cc_on/i.test(text); - } - return type == "comment5"; -} - -function is_comment5(comment) { - return comment.type == "comment5"; + // multiline comment + return comment.type == "comment2" && /@preserve|@license|@cc_on/i.test(comment.value); } function OutputStream(options) { @@ -86,7 +77,7 @@ function OutputStream(options) { }, true); // Convert comment option to RegExp if neccessary and set up comments filter - var comment_filter = options.shebang ? is_comment5 : return_false; // Default case, throw all comments away except shebangs + var comment_filter = return_false; // Default case, throw all comments away if (options.comments) { var comments = options.comments; if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) { @@ -98,12 +89,12 @@ function OutputStream(options) { } if (comments instanceof RegExp) { comment_filter = function(comment) { - return comment.type == "comment5" || comments.test(comment.value); + return comment.type != "comment5" && comments.test(comment.value); }; } else if (typeof comments === "function") { comment_filter = function(comment) { - return comment.type == "comment5" || comments(this, comment); + return comment.type != "comment5" && comments(this, comment); }; } else if (comments === "some") { @@ -400,10 +391,6 @@ function OutputStream(options) { return OUTPUT; }; - if (options.preamble) { - print(options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n")); - } - var stack = []; return { get : get, @@ -523,6 +510,17 @@ function OutputStream(options) { })); } + if (comments.length > 0 && output.pos() == 0) { + if (output.option("shebang") && comments[0].type == "comment5") { + output.print("#!" + comments.shift().value + "\n"); + output.indent(); + } + var preamble = output.option("preamble"); + if (preamble) { + output.print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n")); + } + } + comments = comments.filter(output.comment_filter, self); // Keep single line comments after nlb, after nlb @@ -547,10 +545,6 @@ function OutputStream(options) { output.space(); } } - else if (output.pos() === 0 && c.type == "comment5" && output.option("shebang")) { - output.print("#!" + c.value + "\n"); - output.indent(); - } }); } }); |