From 1e4de2e6d31a50dafee4a4e4d020f22691d22292 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 27 Jun 2017 10:31:19 +0800 Subject: [PATCH 1/3] parse `@global_defs` as expressions (#2169) - let parser rejects non-conformant input - eliminate need for extraneous parenthesis --- lib/compress.js | 9 +++------ test/compress/global_defs.js | 21 +++++++++++++++++++++ test/mocha/minify.js | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index dc82db02..0f00d091 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -92,12 +92,9 @@ function Compressor(options, false_by_default) { var global_defs = this.options["global_defs"]; if (typeof global_defs == "object") for (var key in global_defs) { if (/^@/.test(key) && HOP(global_defs, key)) { - var ast = parse(global_defs[key]); - if (ast.body.length == 1 && ast.body[0] instanceof AST_SimpleStatement) { - global_defs[key.slice(1)] = ast.body[0].body; - } else throw new Error(string_template("Can't handle expression: {value}", { - value: global_defs[key] - })); + global_defs[key.slice(1)] = parse(global_defs[key], { + expression: true + }); } } var pure_funcs = this.options["pure_funcs"]; diff --git a/test/compress/global_defs.js b/test/compress/global_defs.js index d784d335..74147ded 100644 --- a/test/compress/global_defs.js +++ b/test/compress/global_defs.js @@ -174,3 +174,24 @@ issue_1986: { console.log(42); } } + +issue_2167: { + options = { + conditionals: true, + dead_code: true, + evaluate: true, + global_defs: { + "@isDevMode": "function(){}", + }, + side_effects: true, + } + input: { + if (isDevMode()) { + greetOverlord(); + } + doWork(); + } + expect: { + doWork(); + } +} diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 638e79f7..b4722ce2 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -212,7 +212,7 @@ describe("minify", function() { }); var err = result.error; assert.ok(err instanceof Error); - assert.strictEqual(err.stack.split(/\n/)[0], "Error: Can't handle expression: debugger"); + assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token: keyword (debugger)"); }); it("should skip inherited properties", function() { var foo = Object.create({ skip: this }); From f0a99125eef4a95a3c4bead81b3bf9a99b207019 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 27 Jun 2017 23:53:42 +0800 Subject: [PATCH 2/3] improve `unsafe_Func` (#2171) - minimise disturbance to `compute_char_frequency()` - remove extraneous quotation marks --- lib/compress.js | 22 +++++++++++----------- test/compress/functions.js | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 0f00d091..352be282 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3120,7 +3120,7 @@ merge(Compressor.prototype, { // https://github.com/mishoo/UglifyJS2/issues/203 // if the code argument is a constant, then we can minify it. try { - var code = "NaN(function(" + self.args.slice(0, -1).map(function(arg) { + var code = "n(function(" + self.args.slice(0, -1).map(function(arg) { return arg.value; }).join(",") + "){" + self.args[self.args.length - 1].value + "})"; var ast = parse(code); @@ -3140,18 +3140,18 @@ merge(Compressor.prototype, { return true; } })); - var args = fun.argnames.map(function(arg, i) { - return make_node(AST_String, self.args[i], { - value: arg.print_to_string() - }); - }); var code = OutputStream(); AST_BlockStatement.prototype._codegen.call(fun, fun, code); - code = code.toString().replace(/^\{|\}$/g, ""); - args.push(make_node(AST_String, self.args[self.args.length - 1], { - value: code - })); - self.args = args; + self.args = [ + make_node(AST_String, self, { + value: fun.argnames.map(function(arg) { + return arg.print_to_string(); + }).join(",") + }), + make_node(AST_String, self.args[self.args.length - 1], { + value: code.get().replace(/^\{|\}$/g, "") + }) + ]; return self; } catch (ex) { if (ex instanceof JS_Parse_Error) { diff --git a/test/compress/functions.js b/test/compress/functions.js index b5def8e1..c8efc12c 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -265,7 +265,7 @@ issue_203: { } expect: { var m = {}; - var fn = Function("n", "o", "o.exports=42"); + var fn = Function("n,o", "o.exports=42"); fn(null, m, m.exports); console.log(m.exports); } From 5e6f26445f932a180890be4792dab574e07cbb0f Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 29 Jun 2017 00:49:06 +0800 Subject: [PATCH 3/3] v3.0.21 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 47e71554..ee2deb06 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.0.20", + "version": "3.0.21", "engines": { "node": ">=0.8.0" },