From 31e7d25cad45bc4eda220cc0de594bf084bb28d8 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 7 Mar 2021 04:44:34 +0000 Subject: fix issues uncovered by lgtm (#4749) --- lib/compress.js | 51 +++++++++++++++++++----------------------------- test/compress/exports.js | 18 ++++++++++++++++- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 0069570c..349dad90 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -277,7 +277,7 @@ merge(Compressor.prototype, { export_symbol(stat.name); } } else if (stat instanceof AST_ExportReferences) { - body.splice(i, 1); + body.splice(i--, 1); [].push.apply(props, stat.properties); } } @@ -773,10 +773,10 @@ merge(Compressor.prototype, { // (function(a,b) {...})(c,d) ---> (function() {var a=c,b=d; ...})() // So existing transformation rules can work on them. var safe = !fn.uses_arguments || tw.has_directive("use strict"); - fn.argnames.forEach(function(arg, i) { + fn.argnames.forEach(function(argname, i) { var value = iife.args[i]; - scan_declaration(tw, compressor, arg, function() { - var j = fn.argnames.indexOf(arg); + scan_declaration(tw, compressor, argname, function() { + var j = fn.argnames.indexOf(argname); var arg = j < 0 ? value : iife.args[j]; if (arg instanceof AST_Sequence && arg.expressions.length < 2) arg = arg.expressions[0]; return arg || make_node(AST_Undefined, iife); @@ -1889,24 +1889,21 @@ merge(Compressor.prototype, { var def = lhs.definition(); var referenced = def.references.length - def.replaced; if (candidate instanceof AST_Assign) referenced--; - if (replaced && referenced == replaced) { - abort = false; - } else { + if (!replaced || referenced > replaced) { candidates.push(hit_stack); force_single = true; continue; } - if (replaced) { - hit_index = 0; - hit = funarg; - for (var i = stat_index; !abort && i < statements.length; i++) { - if (!statements[i].transform(multi_replacer)) statements.splice(i--, 1); - } - if (candidate instanceof AST_VarDef) { - replaced = !compressor.exposed(def) && def.references.length == def.replaced; - } - value_def.single_use = false; + abort = false; + hit_index = 0; + hit = funarg; + for (var i = stat_index; !abort && i < statements.length; i++) { + if (!statements[i].transform(multi_replacer)) statements.splice(i--, 1); + } + if (candidate instanceof AST_VarDef) { + replaced = !compressor.exposed(def) && def.references.length == def.replaced; } + value_def.single_use = false; } if (replaced && !remove_candidate(candidate)) statements.splice(stat_index, 1); } @@ -6927,20 +6924,14 @@ merge(Compressor.prototype, { value = value.drop_side_effect_free(compressor); node.value = value ? make_sequence(node.value, [ value, - make_node(AST_Number, node.value, { - value: 0 - }) + make_node(AST_Number, node.value, { value: 0 }), ]) : null; - } else if (ev && !(ev instanceof AST_Node)) { + } else if (!(ev instanceof AST_Node)) { value = value.drop_side_effect_free(compressor); node.value = value ? make_sequence(node.value, [ value, - make_node(AST_Number, node.value, { - value: 1 - }) - ]) : make_node(AST_Number, node.value, { - value: 1 - }); + make_node(AST_Number, node.value, { value: 1 }), + ]) : make_node(AST_Number, node.value, { value: 1 }); } } }); @@ -8397,7 +8388,7 @@ merge(Compressor.prototype, { continue; } } - } else if (argname && drop_fargs(argname, args[i])) { + } else if (drop_fargs(argname, args[i])) { var node = args[i].drop_side_effect_free(compressor); fn.argnames.splice(i, 1); args.splice(i, 1); @@ -11469,9 +11460,7 @@ merge(Compressor.prototype, { value.key = "" + value.key.value; } } - return changed ? make_node(AST_Object, self, { - properties: values - }) : self; + return make_node(AST_Object, self, { properties: values }); function flush() { keys.each(function(props) { diff --git a/test/compress/exports.js b/test/compress/exports.js index a7239148..3aaae4ae 100644 --- a/test/compress/exports.js +++ b/test/compress/exports.js @@ -203,7 +203,23 @@ mangle_rename: { } } -hoist_exports: { +hoist_exports_1: { + options = { + hoist_exports: true, + } + input: { + export { a }; + export var b; + export function f() {} + } + expect: { + var b; + function f() {} + export { a, b, f }; + } +} + +hoist_exports_2: { options = { evaluate: true, hoist_exports: true, -- cgit v1.2.3