diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-07-12 00:43:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-12 07:43:13 +0800 |
commit | 0d350b78bfd7df22a6311efa94761e71950e3494 (patch) | |
tree | a75e9cf13c3cefdd406fd26dc21ac8c2e0bc85f5 | |
parent | 1ad830facb198878d7fe35586bca89c42b1ea263 (diff) | |
download | tracifyjs-0d350b78bfd7df22a6311efa94761e71950e3494.tar.gz tracifyjs-0d350b78bfd7df22a6311efa94761e71950e3494.zip |
fix corner cases in `unused` (#5075)
fixes #5074
-rw-r--r-- | lib/compress.js | 36 | ||||
-rw-r--r-- | test/compress/destructured.js | 134 |
2 files changed, 154 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js index e3816fa0..aa77ced3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7132,13 +7132,12 @@ merge(Compressor.prototype, { prop_keys = []; prop_map = Object.create(null); value.properties.forEach(function(prop, index) { - if (prop instanceof AST_ObjectSetter) return; if (prop instanceof AST_Spread) return prop_map = false; var key = prop.key; if (key instanceof AST_Node) key = key.evaluate(compressor, true); if (key instanceof AST_Node) { prop_map = false; - } else if (prop_map) { + } else if (prop_map && !(prop instanceof AST_ObjectSetter)) { prop_map[key] = prop; } prop_keys[index] = key; @@ -7184,7 +7183,13 @@ merge(Compressor.prototype, { if (drop_keys && !(key in drop_keys)) { if (mapped) { drop_keys[key] = mapped; - if (value === null) prop_map[key] = false; + if (value === null) { + prop_map[key] = mapped.key instanceof AST_Node + && make_node(AST_ObjectKeyVal, mapped, { + key: mapped.key, + value: make_node(AST_Number, mapped, { value: 0 }), + }); + } } else { drop_keys[key] = true; } @@ -7201,25 +7206,26 @@ merge(Compressor.prototype, { }); value = save_value; drop = save_drop; - if (drop_keys && prop_keys) value.properties = value.properties.filter(function(prop, index) { - if (prop instanceof AST_ObjectSetter) return false; - if (prop instanceof AST_Spread) return true; + if (drop_keys && prop_keys) value.properties = List(value.properties, function(prop, index) { + if (prop instanceof AST_Spread) return prop; var key = prop_keys[index]; - if (key instanceof AST_Node) return true; + if (key instanceof AST_Node) return prop; if (key in drop_keys) { var mapped = drop_keys[key]; - if (!mapped) return true; - if (mapped === prop) return prop_map[key]; + if (!mapped) return prop; + if (mapped === prop) return prop_map[key] || List.skip; } else if (node.rest) { - return true; + return prop; } var trimmed = prop.value.drop_side_effect_free(compressor); - if (!trimmed) { - if (!(prop.key instanceof AST_Node)) return false; - trimmed = make_node(AST_Number, prop, { value: 0 }); + if (trimmed) { + prop.value = trimmed; + return prop; } - prop.value = trimmed; - return true; + return prop.key instanceof AST_Node ? make_node(AST_ObjectKeyVal, prop, { + key: prop.key, + value: make_node(AST_Number, prop, { value: 0 }), + }) : List.skip; }); if (value && !node.rest) switch (properties.length) { case 0: diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 45724b1a..54ee1ac8 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -1154,7 +1154,7 @@ drop_hole: { node_version: ">=6" } -keep_key: { +keep_key_1: { options = { evaluate: true, side_effects: true, @@ -1174,6 +1174,39 @@ keep_key: { node_version: ">=6" } +keep_key_2: { + options = { + evaluate: true, + toplevel: true, + unused: true, + } + input: { + var { 42: a } = { [(console.log("PASS"), 42)](){} }; + } + expect: { + var {} = { [(console.log("PASS"), 42)]: 0 }; + } + expect_stdout: "PASS" + node_version: ">=6" +} + +keep_key_2_pure_getters: { + options = { + evaluate: true, + pure_getters: "strict", + toplevel: true, + unused: true, + } + input: { + var { 42: a } = { [(console.log("PASS"), 42)](){} }; + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} + keep_reference: { options = { reduce_vars: true, @@ -2816,3 +2849,102 @@ issue_5071_2: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5074_getter: { + options = { + evaluate: true, + side_effects: true, + unused: true, + } + input: { + ({} = { get [(console.log("PASS"), 42)]() {} }); + } + expect: { + ({} = { [(console.log("PASS"), 42)]: 0 }); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5074_getter_pure_getters: { + options = { + evaluate: true, + pure_getters: "strict", + side_effects: true, + unused: true, + } + input: { + ({} = { get [(console.log("PASS"), 42)]() {} }); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5074_setter: { + options = { + evaluate: true, + side_effects: true, + unused: true, + } + input: { + ({} = { set [(console.log("PASS"), 42)](v) {} }); + } + expect: { + ({} = { [(console.log("PASS"), 42)]: 0 }); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5074_setter_pure_getters: { + options = { + evaluate: true, + pure_getters: "strict", + side_effects: true, + unused: true, + } + input: { + ({} = { set [(console.log("PASS"), 42)](v) {} }); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5074_method: { + options = { + evaluate: true, + side_effects: true, + unused: true, + } + input: { + ({} = { [(console.log("PASS"), 42)]() {} }); + } + expect: { + ({} = { [(console.log("PASS"), 42)]: 0 }); + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_5074_method_pure_getters: { + options = { + evaluate: true, + pure_getters: "strict", + side_effects: true, + unused: true, + } + input: { + ({} = { [(console.log("PASS"), 42)]() {} }); + } + expect: { + console.log("PASS"); + } + expect_stdout: "PASS" + node_version: ">=6" +} |