Skip to content

Commit

Permalink
fix corner cases in unused (#3519)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Oct 22, 2019
1 parent 267bc70 commit 4240fba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3738,7 +3738,7 @@ merge(Compressor.prototype, {
head.push(def);
} else {
var value = def.value
&& (sym.references.length != 1 || !sym.replaced)
&& !def.value.single_use
&& def.value.drop_side_effect_free(compressor);
if (value) {
AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
Expand Down Expand Up @@ -6158,6 +6158,7 @@ merge(Compressor.prototype, {
if (single_use && fixed) {
def.single_use = false;
fixed._squeezed = true;
fixed.single_use = true;
if (fixed instanceof AST_Defun) {
fixed = make_node(AST_Function, fixed, fixed);
fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
Expand Down
61 changes: 60 additions & 1 deletion test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ issue_3497: {
expect_stdout: "undefined"
}

issue_3515: {
issue_3515_1: {
options = {
collapse_vars: true,
reduce_vars: true,
Expand All @@ -2127,3 +2127,62 @@ issue_3515: {
}
expect_stdout: "1"
}

issue_3515_2: {
options = {
side_effects: true,
toplevel: true,
unused: true,
}
input: {
var a = "FAIL";
function f() {
typeof b === "number";
delete a;
}
var b = f(a = "PASS");
console.log(a);
}
expect: {
var a = "FAIL";
function f() {
delete a;
}
f(a = "PASS");
console.log(a);
}
expect_stdout: "PASS"
}

issue_3515_3: {
options = {
collapse_vars: true,
unused: true,
}
input: {
var c = "FAIL";
(function() {
function f() {
c = "PASS";
}
var a = f();
var a = function g(b) {
b && (b.p = this);
}(a);
})();
console.log(c);
}
expect: {
var c = "FAIL";
(function() {
function f() {
c = "PASS";
}
(function(b) {
b && (b.p = this);
})(f());
})();
console.log(c);
}
expect_stdout: "PASS"
}

0 comments on commit 4240fba

Please sign in to comment.