Skip to content

Commit

Permalink
suppress unused on block variables (#1969)
Browse files Browse the repository at this point in the history
fixes #1968
  • Loading branch information
alexlamsl authored May 18, 2017
1 parent aaba482 commit 3db2001
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,11 @@ merge(Compressor.prototype, {
return lhs instanceof AST_SymbolRef && lhs.definition().orig[0] instanceof AST_SymbolLambda;
}

function is_reference_const(ref) {
function is_ref_of(ref, type) {
if (!(ref instanceof AST_SymbolRef)) return false;
var orig = ref.definition().orig;
for (var i = orig.length; --i >= 0;) {
if (orig[i] instanceof AST_SymbolConst) return true;
if (orig[i] instanceof type) return true;
}
}

Expand Down Expand Up @@ -828,7 +828,7 @@ merge(Compressor.prototype, {
}
} else {
var lhs = expr[expr instanceof AST_Assign ? "left" : "expression"];
return !is_reference_const(lhs) && lhs;
return !is_ref_of(lhs, AST_SymbolConst) && lhs;
}
}

Expand Down Expand Up @@ -2056,7 +2056,7 @@ merge(Compressor.prototype, {
&& node instanceof AST_Assign
&& node.operator == "="
&& node.left instanceof AST_SymbolRef
&& !is_reference_const(node.left)
&& !is_ref_of(node.left, AST_SymbolBlockDeclaration)
&& scope === self) {
node.right.walk(tw);
return true;
Expand Down Expand Up @@ -3160,7 +3160,6 @@ merge(Compressor.prototype, {
// Symbol's argument is only used for debugging.
self.args = [];
return self;
break;
}
}
else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {
Expand Down Expand Up @@ -3343,7 +3342,7 @@ merge(Compressor.prototype, {
&& (left.operator == "++" || left.operator == "--")) {
left = left.expression;
} else left = null;
if (!left || is_lhs_read_only(left) || is_reference_const(left)) {
if (!left || is_lhs_read_only(left) || is_ref_of(left, AST_SymbolConst)) {
expressions[++i] = cdr;
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -1253,3 +1253,30 @@ reassign_const: {
}
expect_stdout: true
}

issue_1968: {
options = {
unused: true,
}
input: {
function f(c) {
var a;
if (c) {
let b;
return (a = 2) + (b = 3);
}
}
console.log(f(1));
}
expect: {
function f(c) {
if (c) {
let b;
return 2 + (b = 3);
}
}
console.log(f(1));
}
expect_stdout: "5"
node_version: ">=6"
}

0 comments on commit 3db2001

Please sign in to comment.