From 6fb7de77870aa8989c8f6bf8b46a2145b07261e4 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 5 Feb 2022 05:19:42 +0000 Subject: [PATCH] fix corner case in `inline` (#5343) fixes #5342 --- lib/compress.js | 16 ++------ test/compress/arrows.js | 75 ++++++++++++++++++++++++++++++++++++++ test/compress/functions.js | 13 +++---- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 70f0fdb8..95dce446 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -9469,16 +9469,12 @@ Compressor.prototype.compress = function(node) { if (self.bfinally) { body.push(make_node(AST_BlockStatement, self.bfinally, self.bfinally).optimize(compressor)); } - return make_node(AST_BlockStatement, self, { - body: body - }).optimize(compressor); + return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); } if (self.bfinally && has_declarations_only(self.bfinally)) { var body = make_node(AST_BlockStatement, self.bfinally, self.bfinally).optimize(compressor); body = self.body.concat(body); - if (!self.bcatch) return make_node(AST_BlockStatement, self, { - body: body - }).optimize(compressor); + if (!self.bcatch) return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor); self.body = body; self.bfinally = null; } @@ -13174,6 +13170,7 @@ Compressor.prototype.compress = function(node) { if (fn.contains_this()) return; if (!scope) scope = find_scope(compressor); var defined = new Dictionary(); + defined.set("NaN", true); while (!(scope instanceof AST_Scope)) { scope.variables.each(function(def) { defined.set(def.name, true); @@ -13263,13 +13260,6 @@ Compressor.prototype.compress = function(node) { if (sym instanceof AST_SymbolCatch) return; body.push(make_node(AST_SimpleStatement, sym, { body: init_ref(compressor, flatten_var(sym)) })); }); - if (fn.variables.has("NaN")) scope.transform(new TreeTransformer(function(node) { - if (node instanceof AST_NaN) return make_node(AST_Binary, node, { - operator: "/", - left: make_node(AST_Number, node, { value: 0 }), - right: make_node(AST_Number, node, { value: 0 }), - }); - })); var defs = Object.create(null), syms = new Dictionary(); if (simple_argnames && all(call.args, function(arg) { return !(arg instanceof AST_Spread); diff --git a/test/compress/arrows.js b/test/compress/arrows.js index cc8b7072..d044d613 100644 --- a/test/compress/arrows.js +++ b/test/compress/arrows.js @@ -927,3 +927,78 @@ issue_5251: { expect_stdout: true node_version: ">=4" } + +issue_5342_1: { + options = { + dead_code: true, + inline: true, + toplevel: true, + unused: true, + } + input: { + for (var a in 0) { + (() => { + while (1); + })(new function(NaN) { + a.p; + }()); + } + console.log(function() { + return b; + try { + b; + } catch (e) { + var b; + } + }()); + } + expect: { + for (var a in 0) { + (function(NaN) { + a.p; + })(); + while (1); + } + console.log(b); + var b; + } + expect_stdout: "undefined" + node_version: ">=4" +} + +issue_5342_2: { + rename = true + options = { + dead_code: true, + inline: true, + toplevel: true, + unused: true, + } + input: { + for (var a in 0) { + (() => { + while (1); + })(new function(NaN) { + a.p; + }()); + } + console.log(function() { + return b; + try { + b; + } catch (e) { + var b; + } + }()); + } + expect: { + for (var a in 0) { + a.p; + while (1); + } + console.log(c); + var c; + } + expect_stdout: "undefined" + node_version: ">=4" +} diff --git a/test/compress/functions.js b/test/compress/functions.js index fa26d408..9a23985b 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1713,16 +1713,14 @@ issue_2620_5: { } expect: { var c = "FAIL"; - (function() { - var a = 0/0; - var NaN = void 0; + !function(a, NaN) { switch (a) { case a: break; case c = "PASS", NaN: break; } - })(); + }(NaN); console.log(c); } expect_stdout: "PASS" @@ -7635,9 +7633,10 @@ issue_5237: { } expect: { function f() { - while (console.log(0/0)); - var NaN = console && console.log(NaN); - return; + while (console.log(NaN)); + (function() { + var NaN = console && console.log(NaN); + })(); } f(); }