suppress unused on block variables (#1969)

fixes #1968
This commit is contained in:
Alex Lam S.L
2017-05-19 00:28:19 +08:00
committed by GitHub
parent aaba482e48
commit 3db2001633
2 changed files with 32 additions and 6 deletions

View File

@@ -545,11 +545,11 @@ merge(Compressor.prototype, {
return lhs instanceof AST_SymbolRef && lhs.definition().orig[0] instanceof AST_SymbolLambda; 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; if (!(ref instanceof AST_SymbolRef)) return false;
var orig = ref.definition().orig; var orig = ref.definition().orig;
for (var i = orig.length; --i >= 0;) { for (var i = orig.length; --i >= 0;) {
if (orig[i] instanceof AST_SymbolConst) return true; if (orig[i] instanceof type) return true;
} }
} }
@@ -828,7 +828,7 @@ merge(Compressor.prototype, {
} }
} else { } else {
var lhs = expr[expr instanceof AST_Assign ? "left" : "expression"]; var lhs = expr[expr instanceof AST_Assign ? "left" : "expression"];
return !is_reference_const(lhs) && lhs; return !is_ref_of(lhs, AST_SymbolConst) && lhs;
} }
} }
@@ -2056,7 +2056,7 @@ merge(Compressor.prototype, {
&& node instanceof AST_Assign && node instanceof AST_Assign
&& node.operator == "=" && node.operator == "="
&& node.left instanceof AST_SymbolRef && node.left instanceof AST_SymbolRef
&& !is_reference_const(node.left) && !is_ref_of(node.left, AST_SymbolBlockDeclaration)
&& scope === self) { && scope === self) {
node.right.walk(tw); node.right.walk(tw);
return true; return true;
@@ -3160,7 +3160,6 @@ merge(Compressor.prototype, {
// Symbol's argument is only used for debugging. // Symbol's argument is only used for debugging.
self.args = []; self.args = [];
return self; return self;
break;
} }
} }
else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) { else if (exp instanceof AST_Dot && exp.property == "toString" && self.args.length == 0) {
@@ -3343,7 +3342,7 @@ merge(Compressor.prototype, {
&& (left.operator == "++" || left.operator == "--")) { && (left.operator == "++" || left.operator == "--")) {
left = left.expression; left = left.expression;
} else left = null; } 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; expressions[++i] = cdr;
continue; continue;
} }

View File

@@ -1253,3 +1253,30 @@ reassign_const: {
} }
expect_stdout: true 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"
}