Skip to content

Commit

Permalink
improve handling of non-trivial assignment values (#5227)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Dec 22, 2021
1 parent 343bf6d commit 7b2eb4b
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 217 deletions.
335 changes: 190 additions & 145 deletions lib/compress.js

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion test/compress/assignments.js
Expand Up @@ -489,7 +489,7 @@ logical_assignments: {
node_version: ">=15"
}

logical_collapse_vars: {
logical_collapse_vars_1: {
options = {
collapse_vars: true,
}
Expand All @@ -509,6 +509,27 @@ logical_collapse_vars: {
node_version: ">=15"
}

logical_collapse_vars_2: {
options = {
collapse_vars: true,
}
input: {
var a = "PASS";
(function(b) {
b ||= (a = "FAIL", {});
return b;
})(console).log(a);
}
expect: {
var a = "PASS";
(function(b) {
return b ||= (a = "FAIL", {});
})(console).log(a);
}
expect_stdout: "PASS"
node_version: ">=15"
}

logical_reduce_vars: {
options = {
evaluate: true,
Expand Down
146 changes: 117 additions & 29 deletions test/compress/collapse_vars.js
Expand Up @@ -346,9 +346,7 @@ collapse_vars_if: {
return "x" != "Bar" + x / 4 ? g9 : g5;
}
function f3(x) {
if (x)
return 1;
return 2;
return x ? 1 : 2;
}
}
}
Expand Down Expand Up @@ -1912,6 +1910,42 @@ collapse_vars_regexp: {
]
}

collapse_arg_sequence: {
options = {
collapse_vars: true,
unused: true,
}
input: {
(function(a) {
a("foo");
})((console.log("bar"), console.log));
}
expect: {
(function(a) {
(0, console.log)("foo");
})(console.log("bar"));
}
expect_stdout: [
"bar",
"foo",
]
}

collapse_for_init: {
options = {
collapse_vars: true,
toplevel: true,
}
input: {
for (var a = (Math, console), b = a.log("PASS"); b;);
}
expect: {
Math;
for (var a, b = console.log("PASS"); b;);
}
expect_stdout: "PASS"
}

issue_1537: {
options = {
collapse_vars: true,
Expand Down Expand Up @@ -2667,6 +2701,24 @@ chained_4: {
expect_stdout: "foo undefined"
}

chained_5: {
options = {
collapse_vars: true,
}
input: {
var a = "PASS";
var a = (console, console.log(a));
a && ++a;
}
expect: {
var a = "PASS";
console;
var a;
(a = console.log(a)) && ++a;
}
expect_stdout: "PASS"
}

boolean_binary_1: {
options = {
collapse_vars: true,
Expand Down Expand Up @@ -2906,6 +2958,43 @@ compound_assignment_2: {
expect_stdout: "4"
}

compound_assignment_3: {
options = {
collapse_vars: true,
}
input: {
var a = 1;
a += (console.log("PASS"), 2);
a.p;
}
expect: {
var a = 1;
(a += (console.log("PASS"), 2)).p;
}
expect_stdout: "PASS"
}

compound_assignment_4: {
options = {
collapse_vars: true,
evaluate: true,
}
input: {
A = "PASS";
var a = "";
a += (a = "FAIL", A);
a.p;
console.log(a);
}
expect: {
A = "PASS";
var a = "";
(a += (a = "FAIL", A)).p;
console.log(a);
}
expect_stdout: "PASS"
}

issue_2187_1: {
options = {
collapse_vars: true,
Expand Down Expand Up @@ -3030,10 +3119,10 @@ issue_2203_2: {
a: "FAIL",
b: function() {
return function(c) {
return (String, (Object, function() {
return (Object, function() {
return this;
}())).a;
}();
}()).a;
}(String);
}
}.b());
}
Expand Down Expand Up @@ -3764,17 +3853,17 @@ issue_2437_1: {
console.log(foo());
}
expect: {
console.log(function() {
if (xhrDesc) {
var result = !!(req = new XMLHttpRequest()).onreadystatechange;
return Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}),
result;
}
var req = new XMLHttpRequest(), detectFunc = function(){};
return req.onreadystatechange = detectFunc,
result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc,
req.onreadystatechange = null, result;
}());
var req, detectFunc, result;
console.log((
xhrDesc ? (result = !!(req = new XMLHttpRequest).onreadystatechange,
Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc||{})
) : (
(req = new XMLHttpRequest).onreadystatechange = detectFunc = function(){},
result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc,
req.onreadystatechange = null
),
result
));
}
}

Expand Down Expand Up @@ -3815,15 +3904,15 @@ issue_2437_2: {
foo();
}
expect: {
!function() {
if (xhrDesc)
return (req = new XMLHttpRequest()).onreadystatechange,
Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {});
var req = new XMLHttpRequest();
req.onreadystatechange = function(){},
var req;
xhrDesc ? (
(req = new XMLHttpRequest).onreadystatechange,
Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {})
) : (
(req = new XMLHttpRequest).onreadystatechange = function(){},
req[SYMBOL_FAKE_ONREADYSTATECHANGE_1],
req.onreadystatechange = null;
}();
req.onreadystatechange = null
);
}
}

Expand Down Expand Up @@ -6445,7 +6534,8 @@ assign_undeclared: {
console.log(typeof B);
}
expect: {
B = new (console.log(42), function() {})();
console.log(42);
B = new function() {}();
console.log(typeof B);
}
expect_stdout: [
Expand Down Expand Up @@ -9141,9 +9231,7 @@ issue_4908: {
console.log(d[1]);
}
expect: {
var a = 0, b;
console || a++;
var c = a, d = [ (d = a) && d, d += 42 ];
var a = 0, b, c = (console || a++, a), d = [ (d = a) && d, d += 42 ];
console.log(d[1]);
}
expect_stdout: "42"
Expand Down
22 changes: 22 additions & 0 deletions test/compress/default-values.js
Expand Up @@ -158,6 +158,28 @@ process_boolean_returns: {
node_version: ">=6"
}

collapse_arg_sequence: {
options = {
collapse_vars: true,
unused: true,
}
input: {
(function(a = (console.log("bar"), console.log)) {
a("foo");
})();
}
expect: {
(function(a = console.log("bar")) {
(0, console.log)("foo");
})();
}
expect_stdout: [
"bar",
"foo",
]
node_version: ">=6"
}

collapse_value_1: {
options = {
collapse_vars: true,
Expand Down
3 changes: 2 additions & 1 deletion test/compress/destructured.js
Expand Up @@ -395,7 +395,8 @@ funarg_unused_6_keep_fargs: {
}
expect: {
(function() {
var {} = (console, 42);
console;
var {} = 42;
})();
console.log(typeof a);
}
Expand Down
11 changes: 6 additions & 5 deletions test/compress/drop-unused.js
Expand Up @@ -1728,7 +1728,8 @@ issue_2768: {
}
expect: {
var a = "FAIL";
var c = (d = a, void (d && (a = "PASS")));
d = a;
var c = void (d && (a = "PASS"));
var d;
console.log(a, typeof c);
}
Expand Down Expand Up @@ -2382,7 +2383,8 @@ issue_3664: {
}
expect: {
console.log(function() {
var a, b = (a = (a = [ b && console.log("FAIL") ]).p = 0, 0);
a = (a = [ b && console.log("FAIL") ]).p = 0;
var a, b = 0;
return "PASS";
}());
}
Expand Down Expand Up @@ -2551,10 +2553,9 @@ issue_3899: {
console.log(typeof a);
}
expect: {
function a() {
console.log(typeof function() {
return 2;
}
console.log(typeof a);
});
}
expect_stdout: "function"
}
Expand Down
4 changes: 2 additions & 2 deletions test/compress/exponentiation.js
Expand Up @@ -99,8 +99,8 @@ issue_4664: {
expect: {
(function f() {
new function(a) {
console.log(typeof f, a, typeof this);
}((A = 0, 2 ** 30));
console.log(typeof f, 2 ** 30, typeof this);
}(A = 0);
})();
}
expect_stdout: "function 1073741824 object"
Expand Down

0 comments on commit 7b2eb4b

Please sign in to comment.