Skip to content

Commit

Permalink
fix function inlining after reduce_vars (#3340)
Browse files Browse the repository at this point in the history
fixes #3297
  • Loading branch information
alexlamsl authored Mar 14, 2019
1 parent b3ef5e5 commit 8d2e6f3
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5830,6 +5830,14 @@ merge(Compressor.prototype, {
lambda_def.references.push(node);
} else {
def.single_use = false;
var fn = node.fixed_value();
if (!(fn instanceof AST_Lambda)) return;
if (!fn.name) return;
if (!fixed.variables.get(fn.name.name)) return;
fn.name = fn.name.clone();
var value_def = value.variables.get(fn.name.name) || value.def_function(fn.name);
node.thedef = value_def;
value_def.references.push(node);
}
}));
} else {
Expand Down
185 changes: 185 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,188 @@ issue_3274: {
}
expect_stdout: "PASS"
}

issue_3297_1: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
unused: true,
}
mangle = {}
input: {
function function1() {
var r = {
function2: function2
};
function function2() {
alert(1234);
function function3() {
function2();
};
function3();
}
return r;
}
}
expect: {
function function1() {
return {
function2: function n() {
alert(1234);
function t() {
n();
}
t();
}
};
}
}
}

issue_3297_2: {
options = {
collapse_vars: true,
conditionals: true,
inline: true,
reduce_vars: true,
unused: true,
}
mangle = {}
input: {
function function1(session) {
var public = {
processBulk: processBulk
};
return public;
function processBulk(bulk) {
var subparam1 = session();
function processOne(param1) {
var param2 = {
subparam1: subparam1
};
doProcessOne({
param1: param1,
param2: param2,
}, function () {
processBulk(bulk);
});
};
if (bulk && bulk.length > 0)
processOne(bulk.shift());
}
function doProcessOne(config, callback) {
console.log(JSON.stringify(config));
callback();
}
}
function1(function session() {
return 42;
}).processBulk([1, 2, 3]);
}
expect: {
function function1(o) {
return {
processBulk: function t(u) {
var r = o();
function n(n) {
var o = {
subparam1: r
};
c({
param1: n,
param2: o
}, function() {
t(u);
});
}
u && u.length > 0 && n(u.shift());
}
};
function c(n, o) {
console.log(JSON.stringify(n));
o();
}
}
function1(function() {
return 42;
}).processBulk([ 1, 2, 3 ]);
}
expect_stdout: [
'{"param1":1,"param2":{"subparam1":42}}',
'{"param1":2,"param2":{"subparam1":42}}',
'{"param1":3,"param2":{"subparam1":42}}',
]
}

issue_3297_3: {
options = {
collapse_vars: true,
comparisons: true,
conditionals: true,
inline: true,
join_vars: true,
passes: 3,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
mangle = {}
input: {
function function1(session) {
var public = {
processBulk: processBulk
};
return public;
function processBulk(bulk) {
var subparam1 = session();
function processOne(param1) {
var param2 = {
subparam1: subparam1
};
doProcessOne({
param1: param1,
param2: param2,
}, function () {
processBulk(bulk);
});
};
if (bulk && bulk.length > 0)
processOne(bulk.shift());
}
function doProcessOne(config, callback) {
console.log(JSON.stringify(config));
callback();
}
}
function1(function session() {
return 42;
}).processBulk([1, 2, 3]);
}
expect: {
function function1(c) {
return {
processBulk: function n(o) {
var r, t, u = c();
o && 0 < o.length && (r = {
param1: o.shift(),
param2: {
subparam1: u
}
}, t = function() {
n(o);
}, console.log(JSON.stringify(r)), t());
}
};
}
function1(function() {
return 42;
}).processBulk([ 1, 2, 3 ]);
}
expect_stdout: [
'{"param1":1,"param2":{"subparam1":42}}',
'{"param1":2,"param2":{"subparam1":42}}',
'{"param1":3,"param2":{"subparam1":42}}',
]
}

0 comments on commit 8d2e6f3

Please sign in to comment.