aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-11-17 08:23:50 +0000
committerGitHub <noreply@github.com>2020-11-17 16:23:50 +0800
commit8a83c8dd46dc8e446cd7231116a06c8b217291b7 (patch)
treeb1f081551a1f9f488ac07d963ddbf41463dd50cd
parent2a612fd472b4d26fee4342fb57fe9ec54ab124cb (diff)
downloadtracifyjs-8a83c8dd46dc8e446cd7231116a06c8b217291b7.tar.gz
tracifyjs-8a83c8dd46dc8e446cd7231116a06c8b217291b7.zip
fix corner cases in `collapse_vars` & `dead_code` (#4285)
fixes #4284
-rw-r--r--lib/compress.js10
-rw-r--r--test/compress/destructured.js62
2 files changed, 68 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b8725b32..e4426fa3 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -845,7 +845,7 @@ merge(Compressor.prototype, {
if (init instanceof AST_Definitions) {
init.definitions[0].name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) node.definition().fixed = false;
- });
+ }, true);
} else if (init instanceof AST_SymbolRef) {
init.definition().fixed = false;
}
@@ -1188,11 +1188,12 @@ merge(Compressor.prototype, {
AST_Node.DEFMETHOD("match_symbol", function(predicate) {
return predicate(this);
});
- AST_Destructured.DEFMETHOD("match_symbol", function(predicate) {
+ AST_Destructured.DEFMETHOD("match_symbol", function(predicate, allow_computed_keys) {
var found = false;
var tw = new TreeWalker(function(node) {
if (found) return true;
if (node instanceof AST_DestructuredKeyVal) {
+ if (!allow_computed_keys && node.key instanceof AST_Node) return found = true;
node.value.walk(tw);
return true;
}
@@ -7045,7 +7046,7 @@ merge(Compressor.prototype, {
name: node,
value: make_value(compressor, node)
}));
- });
+ }, true);
});
return dropped;
};
@@ -7107,7 +7108,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolDeclaration) {
return !node.fixed_value() || may_overlap(compressor, node.definition());
}
- });
+ }, true);
}) ? to_var(self) : self;
}
@@ -9061,6 +9062,7 @@ merge(Compressor.prototype, {
def.fixed = false;
return strip_assignment();
} else if (parent instanceof AST_VarDef) {
+ if (!(parent.name instanceof AST_SymbolDeclaration)) continue;
if (parent.name.definition() !== def) continue;
if (in_try(level, parent)) break;
def.fixed = false;
diff --git a/test/compress/destructured.js b/test/compress/destructured.js
index 65429b92..1555e3e3 100644
--- a/test/compress/destructured.js
+++ b/test/compress/destructured.js
@@ -1341,3 +1341,65 @@ issue_4282: {
expect_stdout: true
node_version: ">=6"
}
+
+issue_4284_1: {
+ options = {
+ dead_code: true,
+ }
+ input: {
+ var a, {
+ 0: b,
+ } = a = "foo";
+ console.log(a, b);
+ }
+ expect: {
+ var a, {
+ 0: b,
+ } = a = "foo";
+ console.log(a, b);
+ }
+ expect_stdout: "foo f"
+ node_version: ">=6"
+}
+
+issue_4284_2: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var a, {
+ [console.log(a)]: b,
+ } = (a = "PASS", 0);
+ var c = a;
+ }
+ expect: {
+ var a, {
+ [console.log(a)]: b,
+ } = (a = "PASS", 0);
+ var c = a;
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}
+
+issue_4284_3: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var a, b;
+ ({
+ [console.log(a)]: b,
+ } = (a = "PASS", 0));
+ var c = a;
+ }
+ expect: {
+ var a, b;
+ ({
+ [console.log(a)]: b,
+ } = (a = "PASS", 0));
+ var c = a;
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}