aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-05 05:18:01 +0000
committerGitHub <noreply@github.com>2021-03-05 13:18:01 +0800
commitdd30ed6a9bf108f05b103eef81d84f91ae4c6322 (patch)
tree206a02f962fcbbec0a3747789e6a17b56b31e815
parentcb50a2d192ef8be1aac7577d6ec9c06f27b71232 (diff)
downloadtracifyjs-dd30ed6a9bf108f05b103eef81d84f91ae4c6322.tar.gz
tracifyjs-dd30ed6a9bf108f05b103eef81d84f91ae4c6322.zip
enhance `collapse_vars` (#4735)
-rw-r--r--lib/compress.js5
-rw-r--r--test/compress/classes.js3
-rw-r--r--test/compress/collapse_vars.js69
-rw-r--r--test/compress/evaluate.js4
-rw-r--r--test/compress/functions.js20
-rw-r--r--test/compress/sequences.js11
6 files changed, 87 insertions, 25 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 6a59a9d5..c9ba218d 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2068,8 +2068,9 @@ merge(Compressor.prototype, {
if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent);
if (node instanceof AST_PropAccess) {
var exp = node.expression;
- return side_effects || !value_def && exp.may_throw_on_access(compressor)
- || exp instanceof AST_SymbolRef && is_arguments(exp.definition());
+ return side_effects
+ || exp instanceof AST_SymbolRef && is_arguments(exp.definition())
+ || !value_def && (in_try || !lhs_local) && exp.may_throw_on_access(compressor);
}
if (node instanceof AST_Spread) return true;
if (node instanceof AST_SymbolRef) {
diff --git a/test/compress/classes.js b/test/compress/classes.js
index 82c6f30e..93819bfa 100644
--- a/test/compress/classes.js
+++ b/test/compress/classes.js
@@ -620,11 +620,10 @@ collapse_rhs: {
expect: {
"use strict";
var a = "FAIL";
- a = "PASS";
class A {
p = "PASS";
}
- console.log(a);
+ console.log(a = "PASS");
}
expect_stdout: "PASS"
node_version: ">=12"
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 6e82e8b0..dcc87397 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -958,8 +958,7 @@ collapse_vars_misc: {
}
expect: {
function f0(o, a, h) {
- var b = 3 - a;
- return o.run(b)[7] = h;
+ return o.run(3 - a)[7] = h;
}
function f1(x) { return 5 - x }
function f2(x) { return foo() / (5 - x) }
@@ -2276,8 +2275,8 @@ var_defs: {
}
expect: {
var f1 = function(x, y) {
- var r = x + y, z = r * r - r, b = 7;
- console.log(z + b);
+ var r = x + y;
+ console.log(r * r - r + 7);
};
f1("1", 0);
}
@@ -2907,8 +2906,7 @@ issue_2187_1: {
var a = 1;
!function(foo) {
foo();
- var a = 2;
- console.log(a);
+ console.log(2);
}(function() {
console.log(a);
});
@@ -6961,8 +6959,7 @@ sequence_in_iife_2: {
}
expect: {
var a = "foo", b = 42;
- b = a;
- console.log(a, b);
+ console.log(a, b = a);
}
expect_stdout: "foo foo"
}
@@ -8804,3 +8801,59 @@ issue_4732_2: {
}
expect_stdout: "PASS"
}
+
+dot_in_try: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var o, a = 6, b = 7, c;
+ try {
+ c = a * b;
+ o.p(c);
+ } catch (e) {
+ console.log(c);
+ }
+ }
+ expect: {
+ var o, a = 6, b = 7, c;
+ try {
+ c = a * b;
+ o.p(c);
+ } catch (e) {
+ console.log(c);
+ }
+ }
+ expect_stdout: "42"
+}
+
+dot_non_local: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var o, a = 6, b = 7, c;
+ function f() {
+ c = a * b;
+ o.p(c);
+ }
+ try {
+ f();
+ } catch (e) {
+ console.log(c);
+ }
+ }
+ expect: {
+ var o, a = 6, b = 7, c;
+ function f() {
+ c = a * b;
+ o.p(c);
+ }
+ try {
+ f();
+ } catch (e) {
+ console.log(c);
+ }
+ }
+ expect_stdout: "42"
+}
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 597c7c87..2c978fff 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -2726,8 +2726,8 @@ issue_3944: {
}
expect: {
void function f() {
- while (a = 0 == (a = void 0), console.log(a), void 0);
- var a;
+ while (b = void 0, b = console.log(0 == (b && b.p)), void 0);
+ var b;
f;
}();
}
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 98dfc4d8..30779c15 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -2492,14 +2492,14 @@ issue_3297_3: {
input: {
function function1(session) {
var public = {
- processBulk: processBulk
+ processBulk: processBulk,
};
return public;
function processBulk(bulk) {
var subparam1 = session();
function processOne(param1) {
var param2 = {
- subparam1: subparam1
+ subparam1: subparam1,
};
doProcessOne({
param1: param1,
@@ -2525,18 +2525,18 @@ issue_3297_3: {
return {
processBulk: function n(o) {
var r, t, u = c();
- o && 0 < o.length && (r = {
- param1: o.shift(),
- param2: {
- subparam1: u
- }
- },
+ o && 0 < o.length && (r = o.shift(),
t = function() {
n(o);
},
- console.log(JSON.stringify(r)),
+ console.log(JSON.stringify({
+ param1: r,
+ param2: {
+ subparam1: u,
+ },
+ })),
t());
- }
+ },
};
}
function1(function() {
diff --git a/test/compress/sequences.js b/test/compress/sequences.js
index a9dbc707..79b308b5 100644
--- a/test/compress/sequences.js
+++ b/test/compress/sequences.js
@@ -663,12 +663,21 @@ side_effects_cascade_1: {
if (a < 0) a = 0;
b.a = a;
}
+ var m = {}, n = {};
+ f(13, m);
+ f("foo", n);
+ console.log(m.a, n.a);
}
expect: {
function f(a, b) {
- (a -= 42) < 0 && (a = 0), b.a = a;
+ b.a = a = (a -= 42) < 0 ? 0 : a;
}
+ var m = {}, n = {};
+ f(13, m),
+ f("foo", n),
+ console.log(m.a, n.a);
}
+ expect_stdout: "0 NaN"
}
side_effects_cascade_2: {