Skip to content

Commit

Permalink
fix unused on var of the same name within catch (#1716)
Browse files Browse the repository at this point in the history
fixes #1715
  • Loading branch information
alexlamsl authored Mar 28, 2017
1 parent f71f490 commit c909ffb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,13 @@ merge(Compressor.prototype, {
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
var def = node.definitions.filter(function(def){
if (def.value) def.value = def.value.transform(tt);
if (def.name.definition().id in in_use_ids) return true;
var sym = def.name.definition();
if (sym.id in in_use_ids) return true;
if (sym.orig[0] instanceof AST_SymbolCatch
&& sym.scope.parent_scope.find_variable(def.name).orig[0] === def.name) {
def.value = def.value && def.value.drop_side_effect_free(compressor);
return true;
}
var w = {
name : def.name.name,
file : def.name.start.file,
Expand Down
87 changes: 87 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,90 @@ issue_1709: {
}
expect_stdout: true
}

issue_1715_1: {
options = {
unused: true,
}
input: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a;
}
}
f();
console.log(a);
}
expect: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a;
}
}
f();
console.log(a);
}
expect_stdout: "1"
}

issue_1715_2: {
options = {
unused: true,
}
input: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a = 2;
}
}
f();
console.log(a);
}
expect: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a;
}
}
f();
console.log(a);
}
expect_stdout: "1"
}

issue_1715_3: {
options = {
unused: true,
}
input: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a = 2 + x();
}
}
f();
console.log(a);
}
expect: {
var a = 1;
function f() {
a++;
try {} catch (a) {
var a = x();
}
}
f();
console.log(a);
}
expect_stdout: "1"
}

0 comments on commit c909ffb

Please sign in to comment.