Skip to content

Commit

Permalink
fix corner case in inline & reduce_vars (#5579)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 28, 2022
1 parent db6fd6d commit 513995f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
18 changes: 12 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8387,9 +8387,8 @@ Compressor.prototype.compress = function(node) {
var scopes = [ this ];
if (orig instanceof AST_SymbolDeclaration) orig.definition().references.forEach(function(ref) {
var s = ref.scope;
if (member(s, scopes)) return;
do {
push_uniq(scopes, s);
if (!push_uniq(scopes, s)) return;
s = s.parent_scope;
} while (s && s !== this);
});
Expand Down Expand Up @@ -12251,12 +12250,19 @@ Compressor.prototype.compress = function(node) {
if (fixed instanceof AST_DefClass) fixed = to_class_expr(fixed);
if (fixed instanceof AST_LambdaDefinition) fixed = to_func_expr(fixed);
if (is_lambda(fixed)) {
var scope = self.scope.resolve();
var scopes = [];
var scope = self.scope;
do {
scopes.push(scope);
if (scope === def.scope) break;
} while (scope = scope.parent_scope);
fixed.enclosed.forEach(function(def) {
if (fixed.variables.has(def.name)) return;
if (scope.var_names().has(def.name)) return;
scope.enclosed.push(def);
scope.var_names().set(def.name, true);
for (var i = 0; i < scopes.length; i++) {
var scope = scopes[i];
if (!push_uniq(scope.enclosed, def)) return;
scope.var_names().set(def.name, true);
}
});
}
var value;
Expand Down
4 changes: 2 additions & 2 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
// ensure mangling works if `catch` reuses a scope variable
var redef = def.redefined();
if (redef) for (var s = node.scope; s; s = s.parent_scope) {
push_uniq(s.enclosed, redef);
if (!push_uniq(s.enclosed, redef)) break;
if (s === redef.scope) break;
}
} else if (node instanceof AST_SymbolConst) {
Expand Down Expand Up @@ -480,7 +480,7 @@ AST_Lambda.DEFMETHOD("init_vars", function(parent_scope) {
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
var def = this.definition();
for (var s = this.scope; s; s = s.parent_scope) {
push_uniq(s.enclosed, def);
if (!push_uniq(s.enclosed, def)) break;
if (!options) {
s._var_names = undefined;
} else {
Expand Down
39 changes: 39 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8646,3 +8646,42 @@ module_inline: {
}
expect_stdout: "true"
}

single_use_inline_collision: {
options = {
inline: true,
reduce_funcs: true,
reduce_vars: true,
unused: true,
}
input: {
var a = "PASS";
(function() {
var f = function() {
while (console.log(a));
};
(function() {
(function() {
f();
})();
(function(a) {
a || a("FAIL");
})(console.log);
})();
})();
}
expect: {
var a = "PASS";
(function() {
(function() {
while (console.log(a));
return;
})();
(function(a) {
a || a("FAIL");
})(console.log);
return;
})();
}
expect_stdout: "PASS"
}
5 changes: 5 additions & 0 deletions test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
return expr instanceof U.AST_Spread ? expr.expression : expr;
}
}
else if (node instanceof U.AST_Await) {
node.start._permute++;
CHANGED = true;
return node.expression;
}
else if (node instanceof U.AST_Binary) {
var permute = ((node.start._permute += step) * steps | 0) % 4;
var expr = [
Expand Down

0 comments on commit 513995f

Please sign in to comment.