Skip to content

Commit

Permalink
fix corner case in ie8 (#4929)
Browse files Browse the repository at this point in the history
fixes #4928
  • Loading branch information
alexlamsl committed May 13, 2021
1 parent 60f3b55 commit e044293
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6092,6 +6092,30 @@ merge(Compressor.prototype, {
});
tw.directives = Object.create(compressor.directives);
self.walk(tw);
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) {
if (!(def.id in in_use_ids)) return true;
if (def.orig.length < 2) return false;
// function argument will always overshadow its name
if (def.orig[1] instanceof AST_SymbolFunarg) return true;
// retain if referenced within destructured object of argument
return all(def.references, function(ref) {
return !ref.in_arg;
});
};
if (compressor.option("ie8")) initializations.each(function(init, id) {
if (id in in_use_ids) return;
init.forEach(function(init) {
init.walk(new TreeWalker(function(node) {
if (node instanceof AST_Function && node.name && !drop_fn_name(node.name.definition())) {
node.walk(tw);
return true;
}
if (node instanceof AST_Scope) return true;
}));
});
});
// pass 2: for every used symbol we need to walk its
// initialization code to figure out if it uses other
// symbols (that may not be in_use).
Expand Down Expand Up @@ -6129,18 +6153,6 @@ merge(Compressor.prototype, {
delete assign_in_use[id];
}
});
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) {
if (!(def.id in in_use_ids)) return true;
if (def.orig.length < 2) return false;
// function argument will always overshadow its name
if (def.orig[1] instanceof AST_SymbolFunarg) return true;
// retain if referenced within destructured object of argument
return all(def.references, function(ref) {
return !ref.in_arg;
});
};
// pass 3: we should drop declarations not in_use
var trim_defns = [];
var unused_fn_names = [];
Expand Down
50 changes: 50 additions & 0 deletions test/compress/ie8.js
Original file line number Diff line number Diff line change
Expand Up @@ -2947,3 +2947,53 @@ issue_4729: {
}
expect_stdout: "PASS"
}

issue_4928_1: {
options = {
ie8: true,
toplevel: true,
unused: true,
}
input: {
var a = function f() {
f(a);
};
console.log(typeof f);
}
expect: {
var a = function f() {
f(a);
};
console.log(typeof f);
}
expect_stdout: "undefined"
}

issue_4928_2: {
options = {
ie8: true,
toplevel: true,
unused: true,
}
input: {
switch (42) {
case console:
var a = function f() {
f(a);
};
case 42:
var a = console.log("PASS");
}
}
expect: {
switch (42) {
case console:
var a = function f() {
f(a);
};
case 42:
a = console.log("PASS");
}
}
expect_stdout: "PASS"
}
22 changes: 22 additions & 0 deletions test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,25 @@ issue_4906: {
expect_stdout: "PASS"
node_version: ">=14"
}

issue_4928: {
options = {
ie8: true,
toplevel: true,
unused: true,
}
input: {
var a = a?.[function f() {
f(a);
}];
console.log(typeof f);
}
expect: {
var a = a?.[function f() {
f(a);
}];
console.log(typeof f);
}
expect_stdout: "undefined"
node_version: ">=14"
}

0 comments on commit e044293

Please sign in to comment.