fix corner case in inline (#3343)

This commit is contained in:
Alex Lam S.L
2019-03-17 05:31:40 +08:00
committed by GitHub
parent 9707ccdc9f
commit 4430a436eb
2 changed files with 152 additions and 12 deletions

View File

@@ -5005,8 +5005,7 @@ merge(Compressor.prototype, {
} }
function can_inject_vars(catches, safe_to_inject) { function can_inject_vars(catches, safe_to_inject) {
var len = fn.body.length; for (var i = 0, len = fn.body.length; i < len; i++) {
for (var i = 0; i < len; i++) {
var stat = fn.body[i]; var stat = fn.body[i];
if (!(stat instanceof AST_Var)) continue; if (!(stat instanceof AST_Var)) continue;
if (!safe_to_inject) return false; if (!safe_to_inject) return false;
@@ -5035,7 +5034,8 @@ merge(Compressor.prototype, {
if (scope.fixed_value() instanceof AST_Scope) return false; if (scope.fixed_value() instanceof AST_Scope) return false;
} }
} while (!(scope instanceof AST_Scope)); } while (!(scope instanceof AST_Scope));
var safe_to_inject = !(scope instanceof AST_Toplevel) || compressor.toplevel.vars; var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars)
&& fn.parent_scope === compressor.find_parent(AST_Scope);
var inline = compressor.option("inline"); var inline = compressor.option("inline");
if (!can_inject_vars(catches, inline >= 3 && safe_to_inject)) return false; if (!can_inject_vars(catches, inline >= 3 && safe_to_inject)) return false;
if (!can_inject_args(catches, inline >= 2 && safe_to_inject)) return false; if (!can_inject_args(catches, inline >= 2 && safe_to_inject)) return false;

View File

@@ -1398,6 +1398,8 @@ recursive_inline_2: {
issue_2657: { issue_2657: {
options = { options = {
inline: true, inline: true,
passes: 2,
reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
sequences: true, sequences: true,
unused: true, unused: true,
@@ -2467,6 +2469,7 @@ issue_3297_3: {
inline: true, inline: true,
join_vars: true, join_vars: true,
passes: 3, passes: 3,
reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
sequences: true, sequences: true,
side_effects: true, side_effects: true,
@@ -2505,18 +2508,18 @@ issue_3297_3: {
}).processBulk([1, 2, 3]); }).processBulk([1, 2, 3]);
} }
expect: { expect: {
function function1(c) { function function1(u) {
return { return {
processBulk: function n(o) { processBulk: function n(r) {
var r, t, u = c(); var o, t = u();
o && 0 < o.length && (r = { r && 0 < r.length && (o = {
param1: o.shift(), param1: r.shift(),
param2: { param2: {
subparam1: u subparam1: t
} }
}, t = function() { },
n(o); console.log(JSON.stringify(o)),
}, console.log(JSON.stringify(r)), t()); n(r));
} }
}; };
} }
@@ -2530,3 +2533,140 @@ issue_3297_3: {
'{"param1":3,"param2":{"subparam1":42}}', '{"param1":3,"param2":{"subparam1":42}}',
] ]
} }
cross_references_1: {
options = {
inline: true,
reduce_vars: true,
unused: true,
}
input: {
var Math = {
square: function(n) {
return n * n;
}
};
console.log((function(factory) {
return factory();
})(function() {
return function(Math) {
return function(n) {
return Math.square(n);
};
}(Math);
})(3));
}
expect: {
var Math = {
square: function(n) {
return n * n;
}
};
console.log(function(Math) {
return function(n) {
return Math.square(n);
};
}(Math)(3));
}
expect_stdout: "9"
}
cross_references_2: {
options = {
collapse_vars: true,
evaluate: true,
hoist_props: true,
inline: true,
passes: 4,
pure_getters: true,
reduce_vars: true,
sequences: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
var Math = {
square: function(n) {
return n * n;
}
};
console.log((function(factory) {
return factory();
})(function() {
return function(Math) {
return function(n) {
return Math.square(n);
};
}(Math);
})(3));
}
expect: {
console.log(9);
}
expect_stdout: "9"
}
cross_references_3: {
options = {
inline: true,
reduce_vars: true,
unused: true,
}
input: {
var Math = {
square: function(n) {
return n * n;
},
cube: function(n) {
return n * n * n;
}
};
console.log(function(factory) {
return factory();
}(function() {
return function(Math) {
return function(n) {
Math = {
square: function(x) {
return "(SQUARE" + x + ")";
},
cube: function(x) {
return "(CUBE" + x + ")";
}
};
return Math.square(n) + Math.cube(n);
};
}(Math);
})(2));
console.log(Math.square(3), Math.cube(3));
}
expect: {
var Math = {
square: function(n) {
return n * n;
},
cube: function(n) {
return n * n * n;
}
};
console.log(function(Math) {
return function(n) {
Math = {
square: function(x) {
return "(SQUARE" + x + ")";
},
cube: function(x) {
return "(CUBE" + x + ")";
}
};
return Math.square(n) + Math.cube(n);
};
}(Math)(2));
console.log(Math.square(3), Math.cube(3));
}
expect_stdout: [
"(SQUARE2)(CUBE2)",
"9 27",
]
}