aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-07-18 12:38:09 +0100
committerGitHub <noreply@github.com>2021-07-18 19:38:09 +0800
commita7e7865e6bb446d7bcb26fa96d57041c65001d01 (patch)
treea6e636d327267ce845fe63b39cf7aa015cf3e006
parentef5f7fc25e93833396540cf7479685b241bd1b8f (diff)
downloadtracifyjs-a7e7865e6bb446d7bcb26fa96d57041c65001d01.tar.gz
tracifyjs-a7e7865e6bb446d7bcb26fa96d57041c65001d01.zip
fix corner case in `unused` (#5086)
fixes #5085
-rw-r--r--lib/compress.js31
-rw-r--r--test/compress/destructured.js50
2 files changed, 66 insertions, 15 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 27fcacb0..0de072ad 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -6513,8 +6513,10 @@ merge(Compressor.prototype, {
return null;
}, true);
if (trimmed.name) {
- def.name = trimmed.name;
- def.value = value = trimmed.value;
+ def = make_node(AST_VarDef, def, {
+ name: trimmed.name,
+ value: value = trimmed.value,
+ });
flush();
} else if (trimmed.value) {
side_effects.push(trimmed.value);
@@ -6722,19 +6724,18 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Assign) {
descend(node, tt);
- if (node.left instanceof AST_Destructured) {
- var trimmed = trim_destructured(node.left, node.right, function(node) {
- return node;
- }, node.write_only === true);
- if (!trimmed.name) {
- if (trimmed.value) return trimmed.value;
- if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
- return make_node(AST_Number, node, { value: 0 });
- }
- node.left = trimmed.name;
- node.right = trimmed.value;
- }
- return node;
+ if (!(node.left instanceof AST_Destructured)) return node;
+ var trimmed = trim_destructured(node.left, node.right, function(node) {
+ return node;
+ }, node.write_only === true);
+ if (trimmed.name) return make_node(AST_Assign, node, {
+ operator: node.operator,
+ left: trimmed.name,
+ right: trimmed.value,
+ });
+ if (trimmed.value) return trimmed.value;
+ if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
+ return make_node(AST_Number, node, { value: 0 });
}
if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
// Certain combination of unused name + side effect leads to invalid AST:
diff --git a/test/compress/destructured.js b/test/compress/destructured.js
index 06fb57cd..4ba32f3e 100644
--- a/test/compress/destructured.js
+++ b/test/compress/destructured.js
@@ -2948,3 +2948,53 @@ issue_5074_method_pure_getters: {
expect_stdout: "PASS"
node_version: ">=6"
}
+
+issue_5085_1: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ var a = "PASS";
+ var [ b ] = [ 42, a ], c = b ? 0 : a = "FAIL";
+ console.log(a);
+ }
+ expect: {
+ var a = "PASS";
+ var b = [ 42 ][0];
+ b;
+ console.log(a);
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}
+
+issue_5085_2: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ side_effects: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ var a = "PASS";
+ (function(b) {
+ [ b ] = [ 42, a ];
+ var c = b ? 0 : a = "FAIL";
+ })();
+ console.log(a);
+ }
+ expect: {
+ var a = "PASS";
+ (function(b) {
+ b = [ 42 ][0];
+ })();
+ console.log(a);
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}