enhance reduce_vars (#5248)

This commit is contained in:
Alex Lam S.L
2021-12-31 23:53:47 +00:00
committed by GitHub
parent e7ce1051fe
commit 8d0422b6f3
4 changed files with 15 additions and 15 deletions

View File

@@ -492,7 +492,6 @@ Compressor.prototype.compress = function(node) {
def.reassigned = 0;
def.recursive_refs = 0;
def.references = [];
def.should_replace = undefined;
def.single_use = undefined;
}
@@ -11599,8 +11598,8 @@ Compressor.prototype.compress = function(node) {
def.replaced++;
return value;
}
var local = self.fixed !== def.fixed;
if (fixed && (local || def.should_replace !== false)) {
var state;
if (fixed && (state = self.fixed || def.fixed).should_replace !== false) {
var ev, init;
if (fixed instanceof AST_This) {
if (!is_funarg(def) && same_scope(def)) init = fixed;
@@ -11613,20 +11612,22 @@ Compressor.prototype.compress = function(node) {
init = make_node_from_constant(ev, fixed);
}
if (init) {
if (!local && def.should_replace === undefined) {
if (state.should_replace === undefined) {
var value_length = init.optimize(compressor).print_to_string().length;
if (!has_symbol_ref(fixed)) {
value_length = Math.min(value_length, fixed.print_to_string().length);
}
var name_length = def.name.length;
if (compressor.option("unused") && !compressor.exposed(def)) {
var referenced = def.references.length - def.replaced;
name_length += (name_length + 2 + value_length) / (referenced - def.assignments);
var refs = def.references.length - def.replaced - def.assignments;
refs = Math.min(refs, def.references.filter(function(ref) {
return ref.fixed === state;
}).length);
name_length += (name_length + 2 + value_length) / Math.max(1, refs);
}
var delta = value_length - Math.floor(name_length);
def.should_replace = delta < compressor.eval_threshold;
state.should_replace = value_length - Math.floor(name_length) < compressor.eval_threshold;
}
if (local || def.should_replace) {
if (state.should_replace) {
var value;
if (has_symbol_ref(fixed)) {
value = init.optimize(compressor);

View File

@@ -2752,8 +2752,7 @@ issue_3944: {
}
expect: {
void function f() {
while (a = 0 == (a = void 0), console.log(a), void 0);
var a;
while (0 == void 0, console.log(false), void 0);
f;
}();
}

View File

@@ -3228,10 +3228,10 @@ issue_4653: {
f(a++ + (b = b), b |= console.log(a));
}
expect: {
var b = 1;
var b, a = 1;
(function(c, d) {
c || console.log(d);
})(+b + (b = void 0), b |= console.log(2));
})(+a + (b = b), b |= console.log(2));
}
expect_stdout: [
"2",

View File

@@ -905,7 +905,7 @@ use_before_var: {
}
expect: {
function f(){
console.log(void 0);
console.log(t);
var t = 1;
}
}
@@ -981,7 +981,7 @@ inner_var_for_1: {
expect: {
function f() {
var a = 1;
x(1, void 0, d);
x(1, b, d);
for (var b = 2, c = 3; x(1, 2, 3, d); x(1, 2, 3, d)) {
var d = 4, e = 5;
x(1, 2, 3, d, e);