From c01ff7628868a14338aa1193da2ada011c7ad832 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 13 Feb 2020 05:16:10 +0000 Subject: [PATCH] improve code reuse (#3718) --- lib/compress.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8a33b474..666f4c4c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3071,8 +3071,13 @@ merge(Compressor.prototype, { // If the node has been successfully reduced to a constant, // then its value is returned; otherwise the element itself // is returned. + // // They can be distinguished as constant value is never a // descendant of AST_Node. + // + // When `ignore_side_effects` is `true`, inspect the constant value + // produced without worrying about any side effects caused by said + // expression. AST_Node.DEFMETHOD("evaluate", function(compressor, ignore_side_effects) { if (!compressor.option("evaluate")) return this; var cached = []; @@ -4766,21 +4771,17 @@ merge(Compressor.prototype, { return exprs && make_sequence(this, exprs); } if (exp instanceof AST_Function && (!exp.name || !exp.name.definition().references.length)) { - var node = this.clone(); exp.process_expression(false, function(node) { var value = node.value && node.value.drop_side_effect_free(compressor, true); return value ? make_node(AST_SimpleStatement, node, { body: value }) : make_node(AST_EmptyStatement, node); }); - exp.walk(new TreeWalker(function(node) { - if (node instanceof AST_Return && node.value) { - node.value = node.value.drop_side_effect_free(compressor); - return true; - } - if (node instanceof AST_Scope && node !== exp) return true; - })); - return node; + scan_local_returns(exp, function(node) { + if (node.value) node.value = node.value.drop_side_effect_free(compressor); + }); + // always shallow clone to ensure stripping of negated IIFEs + return this.clone(); } return this; } -- 2.34.1