fix corner case in ie8 (#3216)

fixes #3215
This commit is contained in:
Alex Lam S.L
2018-07-19 14:45:36 +08:00
committed by GitHub
parent 8d4b5344f4
commit cea685f8d9
7 changed files with 349 additions and 99 deletions

View File

@@ -1264,6 +1264,9 @@ merge(Compressor.prototype, {
if (node instanceof AST_Exit) { if (node instanceof AST_Exit) {
return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs); return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs);
} }
if (node instanceof AST_Function) {
return compressor.option("ie8") && node.name && node.name.name in lvalues;
}
if (node instanceof AST_PropAccess) { if (node instanceof AST_PropAccess) {
return side_effects || node.expression.may_throw_on_access(compressor); return side_effects || node.expression.may_throw_on_access(compressor);
} }
@@ -1610,10 +1613,7 @@ merge(Compressor.prototype, {
if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false; if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun) return false;
if (def.scope !== scope) return true; if (def.scope !== scope) return true;
return !all(def.references, function(ref) { return !all(def.references, function(ref) {
var s = ref.scope; return ref.scope.resolve() === scope;
// "block" scope within AST_Catch
if (s.TYPE == "Scope") s = s.parent_scope;
return s === scope;
}); });
} }
@@ -2704,35 +2704,30 @@ merge(Compressor.prototype, {
if (right === this.right) return this; if (right === this.right) return this;
var result; var result;
switch (this.operator) { switch (this.operator) {
case "&&" : result = left && right; break; case "&&" : result = left && right; break;
case "||" : result = left || right; break; case "||" : result = left || right; break;
case "|" : result = left | right; break; case "|" : result = left | right; break;
case "&" : result = left & right; break; case "&" : result = left & right; break;
case "^" : result = left ^ right; break; case "^" : result = left ^ right; break;
case "+" : result = left + right; break; case "+" : result = left + right; break;
case "*" : result = left * right; break; case "*" : result = left * right; break;
case "/" : result = left / right; break; case "/" : result = left / right; break;
case "%" : result = left % right; break; case "%" : result = left % right; break;
case "-" : result = left - right; break; case "-" : result = left - right; break;
case "<<" : result = left << right; break; case "<<" : result = left << right; break;
case ">>" : result = left >> right; break; case ">>" : result = left >> right; break;
case ">>>" : result = left >>> right; break; case ">>>": result = left >>> right; break;
case "==" : result = left == right; break; case "==" : result = left == right; break;
case "===" : result = left === right; break; case "===": result = left === right; break;
case "!=" : result = left != right; break; case "!=" : result = left != right; break;
case "!==" : result = left !== right; break; case "!==": result = left !== right; break;
case "<" : result = left < right; break; case "<" : result = left < right; break;
case "<=" : result = left <= right; break; case "<=" : result = left <= right; break;
case ">" : result = left > right; break; case ">" : result = left > right; break;
case ">=" : result = left >= right; break; case ">=" : result = left >= right; break;
default: default : return this;
return this;
} }
if (isNaN(result) && compressor.find_parent(AST_With)) { return isNaN(result) && compressor.find_parent(AST_With) ? this : result;
// leave original expression as is
return this;
}
return result;
}); });
def(AST_Conditional, function(compressor, cached, depth) { def(AST_Conditional, function(compressor, cached, depth) {
var condition = this.condition._eval(compressor, cached, depth); var condition = this.condition._eval(compressor, cached, depth);
@@ -3412,6 +3407,14 @@ merge(Compressor.prototype, {
init.walk(tw); init.walk(tw);
}); });
} }
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
return !compressor.exposed(def) && !def.references.length;
} : function(def) {
// any declarations with same name will overshadow
// name of this anonymous function and can therefore
// never be used anywhere
return !(def.id in in_use_ids) || def.orig.length > 1;
};
// pass 3: we should drop declarations not in_use // pass 3: we should drop declarations not in_use
var tt = new TreeTransformer(function(node, descend, in_list) { var tt = new TreeTransformer(function(node, descend, in_list) {
var parent = tt.parent(); var parent = tt.parent();
@@ -3439,15 +3442,8 @@ merge(Compressor.prototype, {
} }
} }
if (scope !== self) return; if (scope !== self) return;
if (node instanceof AST_Function if (node instanceof AST_Function && node.name && drop_fn_name(node.name.definition())) {
&& node.name node.name = null;
&& !compressor.option("ie8")
&& !compressor.option("keep_fnames")) {
var def = node.name.definition();
// any declarations with same name will overshadow
// name of this anonymous function and can therefore
// never be used anywhere
if (!(def.id in in_use_ids) || def.orig.length > 1) node.name = null;
} }
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) { if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
var trim = !compressor.option("keep_fargs"); var trim = !compressor.option("keep_fargs");
@@ -5726,10 +5722,10 @@ merge(Compressor.prototype, {
if (def) { if (def) {
return def.optimize(compressor); return def.optimize(compressor);
} }
// testing against !self.scope.uses_with first is an optimization
if (!compressor.option("ie8") if (!compressor.option("ie8")
&& is_undeclared_ref(self) && is_undeclared_ref(self)
&& (!self.scope.uses_with || !compressor.find_parent(AST_With))) { // testing against `self.scope.uses_with` is an optimization
&& !(self.scope.uses_with && compressor.find_parent(AST_With))) {
switch (self.name) { switch (self.name) {
case "undefined": case "undefined":
return make_node(AST_Undefined, self).optimize(compressor); return make_node(AST_Undefined, self).optimize(compressor);

View File

@@ -118,11 +118,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
descend(); descend();
scope = save_scope; scope = save_scope;
defun = save_defun; defun = save_defun;
return true; // don't descend again in TreeWalker return true;
} }
if (node instanceof AST_With) { if (node instanceof AST_With) {
for (var s = scope; s; s = s.parent_scope) for (var s = scope; s; s = s.parent_scope) s.uses_with = true;
s.uses_with = true;
return; return;
} }
if (node instanceof AST_Symbol) { if (node instanceof AST_Symbol) {
@@ -132,18 +131,13 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
node.thedef = node; node.thedef = node;
node.references = []; node.references = [];
} }
if (node instanceof AST_SymbolDefun || options.ie8 && node instanceof AST_SymbolLambda) { if (node instanceof AST_SymbolDefun) {
// Careful here, the scope where this should be defined is // This should be defined in the parent scope, as we encounter the
// the parent scope. The reason is that we enter a new // AST_Defun node before getting to its AST_Symbol.
// scope when we encounter the AST_Defun node (which is (node.scope = defun.parent_scope.resolve()).def_function(node, defun);
// instanceof AST_Scope) but we get to the symbol a bit } else if (node instanceof AST_SymbolLambda) {
// later.
(node.scope = defun.parent_scope).def_function(node, defun);
}
else if (node instanceof AST_SymbolLambda) {
defun.def_function(node, node.name == "arguments" ? undefined : defun); defun.def_function(node, node.name == "arguments" ? undefined : defun);
} } else if (node instanceof AST_SymbolVar) {
else if (node instanceof AST_SymbolVar) {
defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined); defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
if (defun !== scope) { if (defun !== scope) {
node.mark_enclosed(options); node.mark_enclosed(options);
@@ -153,8 +147,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
} }
node.reference(options); node.reference(options);
} }
} } else if (node instanceof AST_SymbolCatch) {
else if (node instanceof AST_SymbolCatch) {
scope.def_variable(node).defun = defun; scope.def_variable(node).defun = defun;
} }
}); });
@@ -162,9 +155,9 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
// pass 2: find back references and eval // pass 2: find back references and eval
self.globals = new Dictionary(); self.globals = new Dictionary();
var tw = new TreeWalker(function(node, descend) { var tw = new TreeWalker(function(node) {
if (node instanceof AST_LoopControl && node.label) { if (node instanceof AST_LoopControl) {
node.label.thedef.references.push(node); if (node.label) node.label.thedef.references.push(node);
return true; return true;
} }
if (node instanceof AST_SymbolRef) { if (node instanceof AST_SymbolRef) {
@@ -185,35 +178,43 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
return true; return true;
} }
// ensure mangling works if catch reuses a scope variable // ensure mangling works if catch reuses a scope variable
var def; if (node instanceof AST_SymbolCatch) {
if (node instanceof AST_SymbolCatch && (def = node.definition().redefined())) { var def = node.definition().redefined();
var s = node.scope; if (def) for (var s = node.scope; s; s = s.parent_scope) {
while (s) {
push_uniq(s.enclosed, def); push_uniq(s.enclosed, def);
if (s === def.scope) break; if (s === def.scope) break;
s = s.parent_scope;
} }
return true;
} }
}); });
self.walk(tw); self.walk(tw);
// pass 3: fix up any scoping issue with IE8 // pass 3: fix up any scoping issue with IE8
if (options.ie8) { if (options.ie8) self.walk(new TreeWalker(function(node) {
self.walk(new TreeWalker(function(node, descend) { if (node instanceof AST_SymbolCatch) {
if (node instanceof AST_SymbolCatch) { redefine(node, node.thedef.defun);
var name = node.name; return true;
var refs = node.thedef.references; }
var scope = node.thedef.defun; if (node instanceof AST_SymbolLambda) {
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node); var def = node.thedef;
refs.forEach(function(ref) { if (def.orig.length == 1) {
ref.thedef = def; redefine(node, node.scope.parent_scope);
ref.reference(options); node.thedef.init = def.init;
});
node.thedef = def;
node.reference(options);
return true;
} }
})); return true;
}
}));
function redefine(node, scope) {
var name = node.name;
var refs = node.thedef.references;
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
refs.forEach(function(ref) {
ref.thedef = def;
ref.reference(options);
});
node.thedef = def;
node.reference(options);
} }
}); });
@@ -252,8 +253,7 @@ AST_Lambda.DEFMETHOD("init_scope_vars", function() {
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) { AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
var def = this.definition(); var def = this.definition();
var s = this.scope; for (var s = this.scope; s; s = s.parent_scope) {
while (s) {
push_uniq(s.enclosed, def); push_uniq(s.enclosed, def);
if (options.keep_fnames) { if (options.keep_fnames) {
s.functions.each(function(d) { s.functions.each(function(d) {
@@ -261,7 +261,6 @@ AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
}); });
} }
if (s === def.scope) break; if (s === def.scope) break;
s = s.parent_scope;
} }
}); });
@@ -298,6 +297,12 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, init) {
return symbol.thedef = def; return symbol.thedef = def;
}); });
AST_Lambda.DEFMETHOD("resolve", return_this);
AST_Scope.DEFMETHOD("resolve", function() {
return this.parent_scope;
});
AST_Toplevel.DEFMETHOD("resolve", return_this);
function names_in_use(scope, options) { function names_in_use(scope, options) {
var names = scope.names_in_use; var names = scope.names_in_use;
if (!names) { if (!names) {
@@ -410,7 +415,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
var save_nesting = lname; var save_nesting = lname;
descend(); descend();
lname = save_nesting; lname = save_nesting;
return true; // don't descend again in TreeWalker return true;
} }
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
descend(); descend();
@@ -422,7 +427,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
} }
if (node instanceof AST_Label) { if (node instanceof AST_Label) {
var name; var name;
do name = base54(++lname); while (!is_identifier(name)); do {
name = base54(++lname);
} while (!is_identifier(name));
node.mangled_name = name; node.mangled_name = name;
return true; return true;
} }

View File

@@ -172,9 +172,8 @@ function string_template(text, props) {
} }
function remove(array, el) { function remove(array, el) {
for (var i = array.length; --i >= 0;) { var index = array.indexOf(el);
if (array[i] === el) array.splice(i, 1); if (index >= 0) array.splice(index, 1);
}
} }
function makePredicate(words) { function makePredicate(words) {

View File

@@ -5730,3 +5730,100 @@ issue_3096: {
} }
expect_stdout: "ab" expect_stdout: "ab"
} }
issue_3215_1: {
options = {
collapse_vars: true,
evaluate: true,
ie8: false,
inline: true,
passes: 2,
side_effects: true,
unused: true,
}
input: {
console.log(function a() {
var a = 42;
return typeof a;
}());
}
expect: {
console.log("number");
}
expect_stdout: "number"
}
issue_3215_2: {
options = {
collapse_vars: true,
evaluate: true,
ie8: true,
inline: true,
passes: 2,
side_effects: true,
unused: true,
}
input: {
console.log(function a() {
var a = 42;
return typeof a;
}());
}
expect: {
console.log(function a() {
var a = 42;
return typeof a;
}());
}
expect_stdout: "number"
}
issue_3215_3: {
options = {
collapse_vars: true,
evaluate: true,
ie8: false,
inline: true,
passes: 2,
side_effects: true,
unused: true,
}
input: {
console.log(function() {
var a = 42;
(function a() {});
return typeof a;
}());
}
expect: {
console.log("number");
}
expect_stdout: "number"
}
issue_3215_4: {
options = {
collapse_vars: true,
evaluate: true,
ie8: true,
inline: true,
passes: 2,
side_effects: true,
unused: true,
}
input: {
console.log(function() {
var a = 42;
(function a() {});
return typeof a;
}());
}
expect: {
console.log(function() {
var a = 42;
(function a() {});
return typeof a;
}());
}
expect_stdout: "number"
}

View File

@@ -458,8 +458,8 @@ issue_2976_2: {
} }
expect: { expect: {
console.log(function f() { console.log(function f() {
var n; var o;
return n === f ? "FAIL" : "PASS"; return o === f ? "FAIL" : "PASS";
}()); }());
} }
expect_stdout: "PASS" expect_stdout: "PASS"
@@ -477,9 +477,9 @@ issue_2976_3: {
}()); }());
} }
expect: { expect: {
console.log(function o() { console.log(function r() {
var n; var o;
return n === o ? "FAIL" : "PASS"; return o === r ? "FAIL" : "PASS";
}()); }());
} }
expect_stdout: "PASS" expect_stdout: "PASS"
@@ -678,6 +678,7 @@ issue_3206_1: {
input: { input: {
console.log(function() { console.log(function() {
var foo = function bar() {}; var foo = function bar() {};
var baz = function moo() {};
return "function" == typeof bar; return "function" == typeof bar;
}()); }());
} }
@@ -700,6 +701,7 @@ issue_3206_2: {
input: { input: {
console.log(function() { console.log(function() {
var foo = function bar() {}; var foo = function bar() {};
var baz = function moo() {};
return "function" == typeof bar; return "function" == typeof bar;
}()); }());
} }
@@ -711,3 +713,151 @@ issue_3206_2: {
} }
expect_stdout: "false" expect_stdout: "false"
} }
issue_3215_1: {
mangle = {
ie8: false,
}
input: {
console.log(function foo() {
var bar = function bar(name) {
return "PASS";
};
try {
"moo";
} catch (e) {
bar = function bar(name) {
return "FAIL";
};
}
return bar;
}()());
}
expect: {
console.log(function n() {
var o = function n(o) {
return "PASS";
};
try {
"moo";
} catch (n) {
o = function n(o) {
return "FAIL";
};
}
return o;
}()());
}
expect_stdout: "PASS"
}
issue_3215_2: {
mangle = {
ie8: true,
}
input: {
console.log(function foo() {
var bar = function bar(name) {
return "PASS";
};
try {
"moo";
} catch (e) {
bar = function bar(name) {
return "FAIL";
};
}
return bar;
}()());
}
expect: {
console.log(function foo() {
var r = function r(o) {
return "PASS";
};
try {
"moo";
} catch (o) {
r = function r(o) {
return "FAIL";
};
}
return r;
}()());
}
expect_stdout: "PASS"
}
issue_3215_3: {
mangle = {
ie8: false,
}
input: {
console.log(function foo() {
var bar = function bar(name) {
return "FAIL";
};
try {
moo;
} catch (e) {
bar = function bar(name) {
return "PASS";
};
}
return bar;
}()());
}
expect: {
console.log(function n() {
var o = function n(o) {
return "FAIL";
};
try {
moo;
} catch (n) {
o = function n(o) {
return "PASS";
};
}
return o;
}()());
}
expect_stdout: "PASS"
}
issue_3215_4: {
mangle = {
ie8: true,
}
input: {
console.log(function foo() {
var bar = function bar(name) {
return "FAIL";
};
try {
moo;
} catch (e) {
bar = function bar(name) {
return "PASS";
};
}
return bar;
}()());
}
expect: {
console.log(function foo() {
var r = function r(o) {
return "FAIL";
};
try {
moo;
} catch (o) {
r = function r(o) {
return "PASS";
};
}
return r;
}()());
}
expect_stdout: "PASS"
}

View File

@@ -5231,11 +5231,11 @@ defun_catch_4: {
try { try {
throw 42; throw 42;
} catch (a) { } catch (a) {
function a() {}
console.log(a); console.log(a);
} }
} }
expect_stdout: true expect_stdout: "42"
node_version: "<=4"
} }
defun_catch_5: { defun_catch_5: {
@@ -5257,10 +5257,10 @@ defun_catch_5: {
throw 42; throw 42;
} catch (a) { } catch (a) {
console.log(a); console.log(a);
function a() {}
} }
} }
expect_stdout: true expect_stdout: "42"
node_version: "<=4"
} }
defun_catch_6: { defun_catch_6: {

View File

@@ -16,6 +16,7 @@
}, },
{}, {},
{ {
"ie8": true,
"toplevel": true "toplevel": true
}, },
{ {