Skip to content

Commit

Permalink
fix corner case in inline
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Apr 11, 2020
1 parent c810ecd commit 3fb2f52
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5820,7 +5820,7 @@ merge(Compressor.prototype, {
}
}
if (is_func) {
var def, value;
var def, value, var_assigned = false;
if (can_inline
&& !fn.uses_arguments
&& !fn.pinned()
Expand Down Expand Up @@ -5909,10 +5909,12 @@ merge(Compressor.prototype, {
for (var i = 0; i < len; i++) {
var line = fn.body[i];
if (line instanceof AST_Var) {
if (stat && !all(line.definitions, function(var_def) {
var assigned = var_assigned || !all(line.definitions, function(var_def) {
return !var_def.value;
})) {
return false;
});
if (assigned) {
var_assigned = true;
if (stat) return false;
}
} else if (line instanceof AST_Defun || line instanceof AST_EmptyStatement) {
continue;
Expand All @@ -5932,15 +5934,11 @@ merge(Compressor.prototype, {
}

function can_substitute_directly() {
if (var_assigned) return;
if (compressor.option("inline") <= 1 && fn.argnames.length) return;
var var_count = fn.variables.size() - 1;
if (var_count > fn.argnames.length) return;
var var_names = [];
if (!all(fn.argnames, function(argname) {
push_uniq(var_names, argname.name);
return argname.definition().references.length < 2;
if (!fn.variables.all(function(def) {
return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg;
})) return;
if (var_count > var_names.length) return;
var abort = false;
var begin;
var in_order = [];
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ Dictionary.prototype = {
return this;
},
has: function(key) { return ("$" + key) in this._values },
all: function(predicate) {
for (var i in this._values)
if (!predicate(this._values[i], i.substr(1)))
return false;
return true;
},
each: function(f) {
for (var i in this._values)
f(this._values[i], i.substr(1));
Expand Down
23 changes: 23 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4014,3 +4014,26 @@ issue_3772: {
}
expect_stdout: "PASS"
}

issue_3777: {
options = {
inline: true,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
}
input: {
ff(ff.p);
function ff(a) {
var a = console.log("PASS");
}
}
expect: {
ff(ff.p);
function ff(a) {
var a = console.log("PASS");
}
}
expect_stdout: "PASS"
}

0 comments on commit 3fb2f52

Please sign in to comment.