diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-07-12 16:16:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-12 23:16:12 +0800 |
commit | 92c3fddd7ae57a69de6f4e3448ae4a06b18f2fc6 (patch) | |
tree | 9ce34e74476eb2233aba17e71600c8d12989eec6 | |
parent | 0d350b78bfd7df22a6311efa94761e71950e3494 (diff) | |
download | tracifyjs-92c3fddd7ae57a69de6f4e3448ae4a06b18f2fc6.tar.gz tracifyjs-92c3fddd7ae57a69de6f4e3448ae4a06b18f2fc6.zip |
fix corner case in `unused` & `yields` (#5077)
fixes #5076
-rw-r--r-- | lib/compress.js | 27 | ||||
-rw-r--r-- | test/compress/yields.js | 29 |
2 files changed, 46 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index aa77ced3..f602af9c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7179,16 +7179,15 @@ merge(Compressor.prototype, { } trimmed = prop.value.transform(trimmer); if (!trimmed) { - if (node.rest || prop.key instanceof AST_Node) trimmed = retain_lhs(prop.value); + if (node.rest || retain_key(prop)) trimmed = retain_lhs(prop.value); if (drop_keys && !(key in drop_keys)) { if (mapped) { drop_keys[key] = mapped; 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 }), - }); + prop_map[key] = retain_key(mapped) && make_node(AST_ObjectKeyVal, mapped, { + key: mapped.key, + value: make_node(AST_Number, mapped, { value: 0 }), + }); } } else { drop_keys[key] = true; @@ -7222,7 +7221,7 @@ merge(Compressor.prototype, { prop.value = trimmed; return prop; } - return prop.key instanceof AST_Node ? make_node(AST_ObjectKeyVal, prop, { + return retain_key(prop) ? make_node(AST_ObjectKeyVal, prop, { key: prop.key, value: make_node(AST_Number, prop, { value: 0 }), }) : List.skip; @@ -7259,6 +7258,10 @@ merge(Compressor.prototype, { value: value, }; + function retain_key(prop) { + return prop.key instanceof AST_Node && prop.key.has_side_effects(compressor); + } + function retain_lhs(node) { if (node instanceof AST_Destructured) { if (value === null) { @@ -9604,12 +9607,16 @@ merge(Compressor.prototype, { var key = prop.key; var value = prop.value.transform(tt); if (value) { - side_effects.push(key instanceof AST_Node ? key : make_node_from_constant(key, prop)); + if (side_effects.length) { + if (!(key instanceof AST_Node)) key = make_node_from_constant(key, prop); + side_effects.push(key); + key = make_sequence(node, side_effects); + side_effects = []; + } properties.push(make_node(AST_DestructuredKeyVal, prop, { - key: make_sequence(node, side_effects), + key: key, value: value, })); - side_effects = []; } else if (key instanceof AST_Node) { side_effects.push(key); } diff --git a/test/compress/yields.js b/test/compress/yields.js index 9df533b9..37fd30e3 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -1246,3 +1246,32 @@ issue_5034: { expect_stdout: "PASS" node_version: ">=4" } + +issue_5076: { + options = { + evaluate: true, + hoist_vars: true, + passes: 2, + pure_getters: "strict", + side_effects: true, + toplevel: true, + unused: true, + yields: true, + } + input: { + var a; + console.log("PASS"); + var b = function*({ + p: {}, + }) {}({ + p: { a } = 42, + }); + } + expect: { + var a; + console.log("PASS"); + a = 42["a"]; + } + expect_stdout: "PASS" + node_version: ">=6" +} |