From 33472c6feeb2c608dc4c9f096846d876bef7afa6 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Thu, 9 Feb 2017 11:59:00 +0800 Subject: [PATCH] evaluate AST_SymbolRef as parameter fix invalid boolean conversion now exposed in `make_node_from_constant()` --- lib/compress.js | 17 ++++++++------ test/compress/evaluate.js | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 4e45df92..560e9c34 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -190,7 +190,7 @@ merge(Compressor.prototype, { case "string": return make_node(AST_String, orig, { value: val - }).optimize(compressor); + }); case "number": if (isNaN(val)) { return make_node(AST_NaN, orig); @@ -203,17 +203,17 @@ merge(Compressor.prototype, { }); } - return make_node(AST_Number, orig, { value: val }).optimize(compressor); + return make_node(AST_Number, orig, { value: val }); case "boolean": - return make_node(val ? AST_True : AST_False, orig).optimize(compressor); + return make_node(val ? AST_True : AST_False, orig).transform(compressor); case "undefined": - return make_node(AST_Undefined, orig).optimize(compressor); + return make_node(AST_Undefined, orig).transform(compressor); default: if (val === null) { - return make_node(AST_Null, orig, { value: null }).optimize(compressor); + return make_node(AST_Null, orig, { value: null }); } if (val instanceof RegExp) { - return make_node(AST_RegExp, orig, { value: val }).optimize(compressor); + return make_node(AST_RegExp, orig, { value: val }); } throw new Error(string_template("Can't handle constant of type: {type}", { type: typeof val @@ -2113,6 +2113,9 @@ merge(Compressor.prototype, { }); OPT(AST_Call, function(self, compressor){ + self.args = self.args.map(function(arg) { + return arg.evaluate(compressor)[0]; + }); if (compressor.option("unsafe")) { var exp = self.expression; if (exp instanceof AST_SymbolRef && exp.undeclared()) { @@ -2292,7 +2295,7 @@ merge(Compressor.prototype, { } } } - return self.evaluate(compressor)[0]; + return self; }); OPT(AST_New, function(self, compressor){ diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index 0ff157dc..f88bc538 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -598,3 +598,50 @@ unsafe_prototype_function: { var h = "" + ({toString: 0}); } } + +call_args: { + options = { + evaluate: true, + } + input: { + const a = 1; + console.log(a); + +function(a) { + return a; + }(a); + } + expect: { + const a = 1; + console.log(1); + +function(a) { + return a; + }(1); + } +} + +in_boolean_context: { + options = { + booleans: true, + evaluate: true, + } + input: { + !42; + !"foo"; + ![1, 2]; + !/foo/; + !b(42); + !b("foo"); + !b([1, 2]); + !b(/foo/); + } + expect: { + !1; + !1; + !1; + !1; + !b(42); + !b("foo"); + !b([1, 2]); + !b(/foo/); + } +}