From 9f1f21b810ba3b996236cc7ef6757657757a007a Mon Sep 17 00:00:00 2001 From: kzc Date: Mon, 12 Oct 2015 00:20:42 -0400 Subject: [PATCH 1/7] Output `-- >` instead of `-->` in expressions. Escape `` within string literals. --- lib/output.js | 13 +++++++++++-- test/compress/html_comments.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/compress/html_comments.js diff --git a/lib/output.js b/lib/output.js index c15f3b20..8ca0dc06 100644 --- a/lib/output.js +++ b/lib/output.js @@ -127,6 +127,8 @@ function OutputStream(options) { var ret = make_string(str, quote); if (options.inline_script) ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1"); + ret = ret.replace(/\x3c!--/g, "\\x3c!--"); + ret = ret.replace(/--\x3e/g, "--\\x3e"); return ret; }; @@ -1046,7 +1048,15 @@ function OutputStream(options) { }); DEFPRINT(AST_Binary, function(self, output){ self.left.print(output); - output.space(); + if (self.operator == ">" + && self.left instanceof AST_UnaryPostfix + && self.left.operator == "--") { + // space is mandatory to avoid outputting --> + output.print(" "); + } else { + // the space is optional depending on "beautify" + output.space(); + } output.print(self.operator); if (self.operator == "<" && self.right instanceof AST_UnaryPrefix @@ -1054,7 +1064,6 @@ function OutputStream(options) { && self.right.expression instanceof AST_UnaryPrefix && self.right.expression.operator == "--") { // space is mandatory to avoid outputting comment in"; } + } + expect_exact: 'function f(){return"\\x3c!--HTML--\\x3ecomment in\\x3c!--string literal--\\x3e"}'; +} + + From 17eef5a3c26bfa87879e270b1fa4743646bae559 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Mon, 12 Oct 2015 10:21:22 +0300 Subject: [PATCH 2/7] Only encode in strings when inline_script --- lib/output.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/output.js b/lib/output.js index 8ca0dc06..1bde822e 100644 --- a/lib/output.js +++ b/lib/output.js @@ -125,10 +125,11 @@ function OutputStream(options) { function encode_string(str, quote) { var ret = make_string(str, quote); - if (options.inline_script) + if (options.inline_script) { ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1"); - ret = ret.replace(/\x3c!--/g, "\\x3c!--"); - ret = ret.replace(/--\x3e/g, "--\\x3e"); + ret = ret.replace(/\x3c!--/g, "\\x3c!--"); + ret = ret.replace(/--\x3e/g, "--\\x3e"); + } return ret; }; From 1940fb682c7f29d3d82913d0b58d542d034c2556 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Mon, 12 Oct 2015 10:27:00 +0300 Subject: [PATCH 3/7] Fix tests --- test/run-tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run-tests.js b/test/run-tests.js index fc7476f9..818a65e4 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -190,7 +190,7 @@ function parse_test(file) { function make_code(ast, beautify) { if (arguments.length == 1) beautify = true; - var stream = U.OutputStream({ beautify: beautify }); + var stream = U.OutputStream({ beautify: beautify, inline_script: true }); ast.print(stream); return stream.get(); } From dff54a6552c4d6764c85ac3c11c545ad3909f997 Mon Sep 17 00:00:00 2001 From: kzc Date: Tue, 13 Oct 2015 01:17:10 -0400 Subject: [PATCH 4/7] Fix other operator output related to --- lib/output.js | 7 ++-- test/compress/html_comments.js | 60 ++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/lib/output.js b/lib/output.js index 1bde822e..a3b8f1a7 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1048,8 +1048,9 @@ function OutputStream(options) { output.print(self.operator); }); DEFPRINT(AST_Binary, function(self, output){ + var op = self.operator; self.left.print(output); - if (self.operator == ">" + if (op[0] == ">" /* ">>" ">>>" ">" ">=" */ && self.left instanceof AST_UnaryPostfix && self.left.operator == "--") { // space is mandatory to avoid outputting --> @@ -1058,8 +1059,8 @@ function OutputStream(options) { // the space is optional depending on "beautify" output.space(); } - output.print(self.operator); - if (self.operator == "<" + output.print(op); + if ((op == "<" || op == "<<") && self.right instanceof AST_UnaryPrefix && self.right.operator == "!" && self.right.expression instanceof AST_UnaryPrefix diff --git a/test/compress/html_comments.js b/test/compress/html_comments.js index 6141980d..8495b433 100644 --- a/test/compress/html_comments.js +++ b/test/compress/html_comments.js @@ -5,11 +5,67 @@ html_comment_in_expression: { expect_exact: "function f(a,b,x,y){return a< !--b&&x-- >y}"; } +html_comment_in_less_than: { + input: { + function f(a, b) { return a < !--b; } + } + expect_exact: "function f(a,b){return a< !--b}"; +} + +html_comment_in_left_shift: { + input: { + function f(a, b) { return a << !--b; } + } + expect_exact: "function f(a,b){return a<< !--b}"; +} + +html_comment_in_right_shift: { + input: { + function f(a, b) { return a-- >> b; } + } + expect_exact: "function f(a,b){return a-- >>b}"; +} + +html_comment_in_zero_fill_right_shift: { + input: { + function f(a, b) { return a-- >>> b; } + } + expect_exact: "function f(a,b){return a-- >>>b}"; +} + +html_comment_in_greater_than: { + input: { + function f(a, b) { return a-- > b; } + } + expect_exact: "function f(a,b){return a-- >b}"; +} + +html_comment_in_greater_than_or_equal: { + input: { + function f(a, b) { return a-- >= b; } + } + expect_exact: "function f(a,b){return a-- >=b}"; +} + +html_comment_in_right_shift_assign: { + input: { + // Note: illegal javascript + function f(a, b) { return a-- >>= b; } + } + expect_exact: "function f(a,b){return a-- >>=b}"; +} + +html_comment_in_zero_fill_right_shift_assign: { + input: { + // Note: illegal javascript + function f(a, b) { return a-- >>>= b; } + } + expect_exact: "function f(a,b){return a-- >>>=b}"; +} + html_comment_in_string_literal: { input: { function f() { return "comment in"; } } expect_exact: 'function f(){return"\\x3c!--HTML--\\x3ecomment in\\x3c!--string literal--\\x3e"}'; } - - From eac67b281644d4363deebf5dd3d2a1322e3b2319 Mon Sep 17 00:00:00 2001 From: Damian Krzeminski Date: Tue, 13 Oct 2015 21:01:36 -0400 Subject: [PATCH 5/7] upgrade yargs 3.5.4 -> 3.10.0 we need a version with better support for 'array' params see: https://github.com/bcoe/yargs/pull/164 --- npm-shrinkwrap.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 760575c0..3dd77149 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -115,9 +115,9 @@ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz" }, "yargs": { - "version": "3.5.4", - "from": "yargs@>=3.5.4 <3.6.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.5.4.tgz" + "version": "3.10.0", + "from": "yargs@>=3.10.0 <3.11.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.10.0.tgz" }, "zeparser": { "version": "0.0.7", diff --git a/package.json b/package.json index c172be39..953ab1a5 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "async": "~0.2.6", "source-map": "~0.5.1", "uglify-to-browserify": "~1.0.0", - "yargs": "~3.5.4" + "yargs": "~3.10.0" }, "devDependencies": { "acorn": "~0.6.0", From d5138f74677012b301636cad6d218486e53265e2 Mon Sep 17 00:00:00 2001 From: Damian Krzeminski Date: Tue, 13 Oct 2015 21:05:37 -0400 Subject: [PATCH 6/7] add `--pure-funcs` option it has the same effect as specifying `pure_funcs` in `--compressor` option, however it's much easier to use instead of: --compressor 'pure_func=["Math.floor","debug","console.logTime"]' it's now possible: --compressor --pure-funcs Math.floor debug console.logTime fixes #684 --- README.md | 2 ++ bin/uglifyjs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index b6abc379..c2434547 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,8 @@ The available options are: --mangle-props Mangle property names --mangle-regex Only mangle property names matching the regex --name-cache File to hold mangled names mappings + --pure-funcs List of functions that can be safely removed if + their return value is not used [array] ``` Specify `--output` (`-o`) to declare the output file. Otherwise the output diff --git a/bin/uglifyjs b/bin/uglifyjs index f82d43cb..ca75f159 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -72,6 +72,7 @@ You need to pass an argument to this option to specify the name that your module .describe("mangle-props", "Mangle property names") .describe("mangle-regex", "Only mangle property names matching the regex") .describe("name-cache", "File to hold mangled names mappings") + .describe("pure-funcs", "List of functions that can be safely removed if their return value is not used") .alias("p", "prefix") .alias("o", "output") @@ -104,6 +105,7 @@ You need to pass an argument to this option to specify the name that your module .string("prefix") .string("name-cache") .array("reserved-file") + .array("pure-funcs") .boolean("expr") .boolean("source-map-include-sources") @@ -175,6 +177,10 @@ if (ARGS.d) { if (COMPRESS) COMPRESS.global_defs = getOptions("d"); } +if (ARGS.pure_funcs) { + if (COMPRESS) COMPRESS.pure_funcs = ARGS.pure_funcs; +} + if (ARGS.r) { if (MANGLE) MANGLE.except = ARGS.r.replace(/^\s+|\s+$/g).split(/\s*,+\s*/); } From 6b2861e0861968f1f7acae5cf964cc0e6244b0be Mon Sep 17 00:00:00 2001 From: startswithaj Date: Thu, 15 Oct 2015 16:50:53 +1000 Subject: [PATCH 7/7] Make_string was missing \v and wasnt reversing vertical tabs even though read_escaped_char coverts them --- lib/output.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/output.js b/lib/output.js index a3b8f1a7..933a4ce7 100644 --- a/lib/output.js +++ b/lib/output.js @@ -88,13 +88,14 @@ function OutputStream(options) { function make_string(str, quote) { var dq = 0, sq = 0; - str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s){ + str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s){ switch (s) { case "\\": return "\\\\"; case "\b": return "\\b"; case "\f": return "\\f"; case "\n": return "\\n"; case "\r": return "\\r"; + case "\v": return "\\v"; case "\u2028": return "\\u2028"; case "\u2029": return "\\u2029"; case '"': ++dq; return '"';