Skip to content

Commit

Permalink
enhance reduce_vars (#5038)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jun 28, 2021
1 parent b23b333 commit 798121c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
81 changes: 39 additions & 42 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,6 @@ merge(Compressor.prototype, {
&& !compressor.exposed(def)
&& !(def.init instanceof AST_LambdaExpression && def.init !== def.scope)
&& def.init;
if (def.fixed instanceof AST_LambdaDefinition && !all(def.references, function(ref) {
var scope = ref.scope.resolve();
do {
if (def.scope === scope) return true;
} while (scope instanceof AST_LambdaExpression && (scope = scope.parent_scope.resolve()));
})) {
tw.defun_ids[def.id] = false;
}
def.reassigned = 0;
def.recursive_refs = 0;
def.references = [];
Expand Down Expand Up @@ -526,23 +518,25 @@ merge(Compressor.prototype, {
var marker = tw.defun_ids[def.id];
if (!marker) return;
var visited = tw.defun_visited[def.id];
if (marker === tw.safe_ids) {
if (!visited) return def.fixed;
} else if (visited) {
if (marker === tw.safe_ids) return !visited && def.fixed;
if (visited) {
def.init.enclosed.forEach(function(d) {
if (def.init.variables.get(d.name) === d) return;
if (!safe_to_read(tw, d)) d.fixed = false;
});
} else {
tw.defun_ids[def.id] = false;
}
} else {
if (!tw.in_loop) {
tw.defun_ids[def.id] = tw.safe_ids;
return def.fixed;
return;
}
tw.defun_ids[def.id] = false;
} else if (!tw.in_loop) {
var scope = def.scope;
var s = tw.find_parent(AST_Scope);
do {
if (s === scope) {
tw.defun_ids[def.id] = tw.safe_ids;
return def.fixed;
}
} while (s instanceof AST_LambdaExpression && (s = s.parent_scope.resolve()));
}
tw.defun_ids[def.id] = false;
}

function walk_defuns(tw, scope) {
Expand Down Expand Up @@ -967,11 +961,11 @@ merge(Compressor.prototype, {
});
});
def(AST_Call, function(tw, descend) {
tw.find_parent(AST_Scope).may_call_this();
var exp = this.expression;
var node = this;
var exp = node.expression;
if (exp instanceof AST_LambdaExpression) {
var iife = is_iife_single(this);
this.args.forEach(function(arg) {
var iife = is_iife_single(node);
node.args.forEach(function(arg) {
arg.walk(tw);
if (arg instanceof AST_Spread) iife = false;
});
Expand All @@ -980,28 +974,28 @@ merge(Compressor.prototype, {
if (iife) delete exp.reduce_vars;
return true;
}
if (exp instanceof AST_SymbolRef) {
var def = exp.definition();
if (this.TYPE == "Call" && tw.in_boolean_context()) def.bool_fn++;
if (def.fixed instanceof AST_LambdaDefinition) {
var defun = mark_defun(tw, def);
if (defun) {
descend();
defun.walk(tw);
return true;
}
var def = exp instanceof AST_SymbolRef && exp.definition();
if (node.TYPE == "Call" && tw.in_boolean_context()) {
if (def) {
def.bool_fn++;
} else if (exp instanceof AST_Assign && exp.operator == "=" && exp.left instanceof AST_SymbolRef) {
exp.left.definition().bool_fn++;
}
} else if (this.TYPE == "Call"
&& exp instanceof AST_Assign
&& exp.operator == "="
&& exp.left instanceof AST_SymbolRef
&& tw.in_boolean_context()) {
exp.left.definition().bool_fn++;
}
if (!this.optional) return;
if (def && def.fixed instanceof AST_LambdaDefinition) {
var defun = mark_defun(tw, def);
if (defun) {
descend();
defun.walk(tw);
return true;
}
} else {
tw.find_parent(AST_Scope).may_call_this();
}
if (!node.optional) return;
exp.walk(tw);
push(tw);
this.args.forEach(function(arg) {
node.args.forEach(function(arg) {
arg.walk(tw);
});
pop(tw);
Expand Down Expand Up @@ -10911,7 +10905,10 @@ merge(Compressor.prototype, {
single_use = false;
}
if (single_use) fixed.parent_scope = self.scope;
} else if (!fixed || !fixed.is_constant_expression() || fixed.drop_side_effect_free(compressor)) {
} else if (!fixed
|| def.recursive_refs > 0
|| !fixed.is_constant_expression()
|| fixed.drop_side_effect_free(compressor)) {
single_use = false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/compress/merge_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2735,8 +2735,7 @@ issue_4135: {
0;
a++;
if (!a)
c = (a++, c = 0, void (c && c.p));
var c;
var c = void a++;
console.log(a, -1, c);
}
expect_stdout: "1 -1 undefined"
Expand Down

0 comments on commit 798121c

Please sign in to comment.