reduce this within functions

fixes #2420
This commit is contained in:
alexlamsl 2017-11-02 01:11:06 +08:00
parent a48f87abf2
commit 68d3a1718d
2 changed files with 85 additions and 31 deletions

View File

@ -285,7 +285,7 @@ merge(Compressor.prototype, {
self.transform(tt); self.transform(tt);
}); });
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) { AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
var reduce_vars = compressor.option("reduce_vars"); var reduce_vars = compressor.option("reduce_vars");
var unused = compressor.option("unused"); var unused = compressor.option("unused");
// Stack of look-up tables to keep track of whether a `SymbolDef` has been // Stack of look-up tables to keep track of whether a `SymbolDef` has been
@ -564,7 +564,10 @@ merge(Compressor.prototype, {
} }
function is_immutable(value) { function is_immutable(value) {
return value && (value.is_constant() || value instanceof AST_Lambda); if (!value) return false;
return value.is_constant()
|| value instanceof AST_Lambda
|| value instanceof AST_This;
} }
function read_property(obj, key) { function read_property(obj, key) {
@ -4211,11 +4214,21 @@ merge(Compressor.prototype, {
var value = fixed.optimize(compressor); var value = fixed.optimize(compressor);
return value === fixed ? fixed.clone(true) : value; return value === fixed ? fixed.clone(true) : value;
} }
if (compressor.option("evaluate") && fixed) { if (fixed && d.should_replace === undefined) {
if (d.should_replace === undefined) { var init;
var init = fixed.evaluate(compressor); if (fixed instanceof AST_This) {
if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) { if (all(d.references, function(ref) {
init = make_node_from_constant(init, fixed); return d.scope === ref.scope;
})) {
init = fixed;
}
} else {
var ev = fixed.evaluate(compressor);
if (ev !== fixed && (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))) {
init = make_node_from_constant(ev, fixed);
}
}
if (init) {
var value_length = init.optimize(compressor).print_to_string().length; var value_length = init.optimize(compressor).print_to_string().length;
var fn; var fn;
if (has_symbol_ref(fixed)) { if (has_symbol_ref(fixed)) {
@ -4244,7 +4257,6 @@ merge(Compressor.prototype, {
return d.should_replace(); return d.should_replace();
} }
} }
}
return self; return self;
function has_symbol_ref(value) { function has_symbol_ref(value) {

View File

@ -3295,3 +3295,45 @@ escaped_prop: {
} }
expect_stdout: "2" expect_stdout: "2"
} }
issue_2420: {
options = {
reduce_vars: true,
unused: true,
}
input: {
function run() {
var self = this;
if (self.count++)
self.foo();
else
self.bar();
}
var o = {
count: 0,
foo: function() { console.log("foo"); },
bar: function() { console.log("bar"); },
};
run.call(o);
run.call(o);
}
expect: {
function run() {
if (this.count++)
this.foo();
else
this.bar();
}
var o = {
count: 0,
foo: function() { console.log("foo"); },
bar: function() { console.log("bar"); },
};
run.call(o);
run.call(o);
}
expect_stdout: [
"bar",
"foo",
]
}