fix corner case in reduce_funcs (#3592)

This commit is contained in:
Alex Lam S.L
2019-11-17 11:19:42 +08:00
committed by GitHub
parent 10c1a78772
commit 8504a4ea0e
2 changed files with 120 additions and 32 deletions

View File

@@ -5369,6 +5369,7 @@ merge(Compressor.prototype, {
&& !fn.contains_this() && !fn.contains_this()
&& can_inject_symbols()) { && can_inject_symbols()) {
fn._squeezed = true; fn._squeezed = true;
if (exp !== fn) fn.parent_scope = exp.scope;
return make_sequence(self, flatten_fn()).optimize(compressor); return make_sequence(self, flatten_fn()).optimize(compressor);
} }
if (compressor.option("side_effects") if (compressor.option("side_effects")
@@ -6382,6 +6383,7 @@ merge(Compressor.prototype, {
} while (scope = scope.parent_scope); } while (scope = scope.parent_scope);
} }
} }
if (single_use) fixed.parent_scope = self.scope;
} }
if (single_use && fixed) { if (single_use && fixed) {
def.single_use = false; def.single_use = false;

View File

@@ -2416,7 +2416,7 @@ issue_3297_2: {
doProcessOne({ doProcessOne({
param1: param1, param1: param1,
param2: param2, param2: param2,
}, function () { }, function() {
processBulk(bulk); processBulk(bulk);
}); });
}; };
@@ -2497,7 +2497,7 @@ issue_3297_3: {
doProcessOne({ doProcessOne({
param1: param1, param1: param1,
param2: param2, param2: param2,
}, function () { }, function() {
processBulk(bulk); processBulk(bulk);
}); });
}; };
@@ -2514,18 +2514,21 @@ issue_3297_3: {
}).processBulk([1, 2, 3]); }).processBulk([1, 2, 3]);
} }
expect: { expect: {
function function1(u) { function function1(c) {
return { return {
processBulk: function n(r) { processBulk: function n(o) {
var o, t = u(); var r, t, u = c();
r && 0 < r.length && (o = { o && 0 < o.length && (r = {
param1: r.shift(), param1: o.shift(),
param2: { param2: {
subparam1: t subparam1: u
} }
}, },
console.log(JSON.stringify(o)), t = function() {
n(r)); n(o);
},
console.log(JSON.stringify(r)),
t());
} }
}; };
} }
@@ -3097,23 +3100,20 @@ issue_3400_1: {
}); });
} }
expect: { expect: {
void console.log(function() { void console.log(function g() {
function g() { function h(u) {
function h(u) { var o = {
var o = { p: u
p: u };
}; return console.log(o[g]), o;
return console.log(o[g]), o;
}
function e() {
return [ 42 ].map(function(v) {
return h(v);
});
}
return e();
} }
return g; function e() {
}()()[0].p); return [ 42 ].map(function(v) {
return h(v);
});
}
return e();
}()[0].p);
} }
expect_stdout: [ expect_stdout: [
"undefined", "undefined",
@@ -3154,12 +3154,10 @@ issue_3400_2: {
expect: { expect: {
void console.log(function g() { void console.log(function g() {
return [ 42 ].map(function(v) { return [ 42 ].map(function(v) {
return function(u) { return o = {
var o = { p: v
p: u }, console.log(o[g]), o;
}; var o;
return console.log(o[g]), o;
}(v);
}); });
}()[0].p); }()[0].p);
} }
@@ -3401,3 +3399,91 @@ issue_3562: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
hoisted_inline: {
options = {
inline: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f() {
console.log("PASS");
}
function g() {
for (var console in [ 0 ])
h();
}
function h() {
f();
}
g();
}
expect: {
function f() {
console.log("PASS");
}
(function() {
for (var console in [ 0 ])
void f();
})();
}
expect_stdout: "PASS"
}
hoisted_single_use: {
options = {
reduce_funcs: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function f(a) {
for (var r in a) g(r);
}
function g(a) {
console.log(a);
}
function h(a) {
var g = a.bar;
g();
g();
i(a);
}
function i(b) {
f(b);
}
h({
bar: function() {
console.log("foo");
}
});
}
expect: {
function f(a) {
for (var r in a) g(r);
}
function g(a) {
console.log(a);
}
(function(a) {
var g = a.bar;
g();
g();
(function(b) {
f(b);
})(a);
})({
bar: function() {
console.log("foo");
}
});
}
expect_stdout: [
"foo",
"foo",
"bar",
]
}