Skip to content

Commit

Permalink
enhance unused (#5064)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 10, 2021
1 parent 450aaba commit f67dd31
Show file tree
Hide file tree
Showing 7 changed files with 497 additions and 186 deletions.
308 changes: 222 additions & 86 deletions lib/compress.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ reduce_array: {
console.log(a, b, c);
}
expect: {
var [ , , c = "baz" ] = [ void 0, null ];
var [ c = "baz" ] = [];
console.log("foo", null, c);
}
expect_stdout: "foo null baz"
Expand All @@ -299,7 +299,7 @@ reduce_object: {
console.log(a, b, c);
}
expect: {
var { c = "baz" } = { a: void 0, b: null };
var { c = "baz" } = {};
console.log("foo", null, c);
}
expect_stdout: "foo null baz"
Expand Down Expand Up @@ -719,7 +719,7 @@ unused_value_var_2: {
console.log(a);
}
expect: {
var [ a ] = [ "PASS" ];
var a = [ "PASS" ][0];
console.log(a);
}
expect_stdout: "PASS"
Expand Down Expand Up @@ -1300,7 +1300,7 @@ issue_4468: {
expect: {
(function() {
var {
[console.log("PASS")]: b = 0,
[console.log("PASS")]: b,
} = 0;
})();
}
Expand Down Expand Up @@ -1743,7 +1743,7 @@ issue_4854: {
}());
}
expect: {
console.log(void ([] = "foo"));
console.log(void 0);
}
expect_stdout: "undefined"
node_version: ">=6"
Expand Down
138 changes: 137 additions & 1 deletion test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ drop_unused_1: {
try {
throw 42;
} catch (a) {
var [ a ] = [];
var a = [][0];
}
}
}
Expand Down Expand Up @@ -1138,6 +1138,142 @@ drop_unused_2: {
node_version: ">=6"
}

drop_hole: {
options = {
unused: true,
}
input: {
var [ a ] = [ , ];
console.log(a);
}
expect: {
var a = [][0];
console.log(a);
}
expect_stdout: "undefined"
node_version: ">=6"
}

keep_key: {
options = {
evaluate: true,
side_effects: true,
unused: true,
}
input: {
({} = {
[(console.log("PASS"), 42)]: null,
});
}
expect: {
({} = {
[(console.log("PASS"), 42)]: 0,
});
}
expect_stdout: "PASS"
node_version: ">=6"
}

keep_reference: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = [ {}, 42 ];
var [ b, c ] = a;
console.log(a[0] === b ? "PASS" : "FAIL");
}
expect: {
var a = [ {}, 42 ];
var [ b ] = a;
console.log(a[0] === b ? "PASS" : "FAIL");
}
expect_stdout: "PASS"
node_version: ">=6"
}

maintain_position_assign: {
options = {
unused: true,
}
input: {
console.log(([ , ] = [ , "PASS" ])[1]);
}
expect: {
console.log([ , "PASS" ][1]);
}
expect_stdout: "PASS"
node_version: ">=6"
}

maintain_position_var: {
options = {
toplevel: true,
unused: true,
}
input: {
A = "FAIL";
var [ a, b ] = [ A ];
console.log(b || "PASS");
}
expect: {
A = "FAIL";
var [ , b ] = [ A ];
console.log(b || "PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}

side_effects_array: {
options = {
unused: true,
}
input: {
try {
var [ a ] = 42;
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
var [ a ] = 42;
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

side_effects_object: {
options = {
toplevel: true,
unused: true,
}
input: {
var a = null, b = console, { c } = 42;
try {
c[a = "PASS"];
} catch (e) {
console.log(a);
}
}
expect: {
var a = null, c = (console, 42["c"]);
try {
c[a = "PASS"];
} catch (e) {
console.log(a);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

join_vars: {
options = {
conditionals: true,
Expand Down
59 changes: 40 additions & 19 deletions test/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,26 +684,47 @@ prototype_function: {
side_effects: true,
}
input: {
var a = ({valueOf: 0}) < 1;
var b = ({toString: 0}) < 1;
var c = ({valueOf: 0}) + "";
var d = ({toString: 0}) + "";
var e = (({valueOf: 0}) + "")[2];
var f = (({toString: 0}) + "")[2];
var g = ({valueOf: 0}).valueOf();
var h = ({toString: 0}).toString();
}
expect: {
var a = ({valueOf: 0}) < 1;
var b = ({toString: 0}) < 1;
var c = ({valueOf: 0}) + "";
var d = ({toString: 0}) + "";
var e = (({valueOf: 0}) + "")[2];
var f = (({toString: 0}) + "")[2];
var g = 0();
var h = 0();
function v() {
return this.valueOf === v ? "PASS" : "FAIL";
}
console.log(({ valueOf: v }) < 1);
console.log(({ valueOf: v }) + "");
console.log((( {valueOf: v }) + "")[2]);
console.log(({ valueOf: v }).valueOf());
function t() {
return this.toString === t ? "PASS" : "FAIL";
}
console.log(({ toString: t }) < 1);
console.log(({ toString: t }) + "");
console.log((( {toString: t }) + "")[2]);
console.log(({ toString: t }).toString());
}
expect_stdout: true
expect: {
function v() {
return this.valueOf === v ? "PASS" : "FAIL";
}
console.log(({ valueOf: v }) < 1);
console.log(({ valueOf: v }) + "");
console.log((( {valueOf: v }) + "")[2]);
console.log(({ valueOf: v }).valueOf());
function t() {
return this.toString === t ? "PASS" : "FAIL";
}
console.log(({ toString: t }) < 1);
console.log(({ toString: t }) + "");
console.log((( {toString: t }) + "")[2]);
console.log(({ toString: t }).toString());
}
expect_stdout: [
"false",
"PASS",
"S",
"PASS",
"false",
"PASS",
"S",
"PASS",
]
}

call_args: {
Expand Down
2 changes: 1 addition & 1 deletion test/compress/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ hoist_exports_2: {
}
}
expect: {
let e, { foo: a } = 42;
let e, a = 42["foo"];
function f(t, { [e]: o }) {
t(o, f);
}
Expand Down
94 changes: 92 additions & 2 deletions test/compress/rests.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ retain_destructured_array: {
console.log.apply(console, b);
}
expect: {
var [ , ...b ] = [ "FAIL", "PASS", 42 ];
var [ ...b ] = [ "PASS", 42 ];
console.log.apply(console, b);
}
expect_stdout: "PASS 42"
Expand Down Expand Up @@ -284,7 +284,7 @@ retain_destructured_object_2: {
console.log(k, b[k]);
}
expect: {
var { foo: [], ...b } = { foo: [ "FAIL" ], bar: "PASS", baz: 42 };
var { foo: {}, ...b } = { foo: 0, bar: "PASS", baz: 42 };
for (var k in b)
console.log(k, b[k]);
}
Expand Down Expand Up @@ -407,6 +407,24 @@ drop_unused_call_args_2: {
node_version: ">=6"
}

maintain_position: {
options = {
unused: true,
}
input: {
A = "FAIL";
var [ , ...a ] = [ A, "PASS" ];
console.log(a[0]);
}
expect: {
A = "FAIL";
var [ , ...a ] = [ A, "PASS" ];
console.log(a[0]);
}
expect_stdout: "PASS"
node_version: ">=6"
}

merge_funarg: {
options = {
merge_vars: true,
Expand Down Expand Up @@ -723,6 +741,78 @@ issue_4544_2: {
node_version: ">=6"
}

issue_4560_1: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = 0;
(function(...{
[a++]: {},
}) {})(2);
console.log(a);
}
expect: {
var a = 0;
(function(...{
[a++]: {},
}) {})(2);
console.log(a);
}
expect_stdout: "1"
node_version: ">=6"
}

issue_4560_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = 0;
(function(...{
[a++]: {},
}) {})(2);
console.log(a);
}
expect: {
var a = 0;
(function(...{
[a++]: {},
}) {})(2);
console.log(a);
}
expect_stdout: "1"
node_version: ">=6"
}

issue_4560_3: {
options = {
collapse_vars: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = 0, b;
[ ...{
[a++]: b,
} ] = [ "PASS" ];
console.log(b);
}
expect: {
var a = 0, b;
[ ...{
[a++]: b,
} ] = [ "PASS" ];
console.log(b);
}
expect_stdout: "PASS"
node_version: ">=6"
}

issue_4562: {
options = {
evaluate: true,
Expand Down

0 comments on commit f67dd31

Please sign in to comment.