enhance functions (#5052)
This commit is contained in:
@@ -519,6 +519,13 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function walk_fn_def(tw, fn) {
|
||||||
|
var was_scanning = tw.fn_scanning;
|
||||||
|
tw.fn_scanning = fn;
|
||||||
|
fn.walk(tw);
|
||||||
|
tw.fn_scanning = was_scanning;
|
||||||
|
}
|
||||||
|
|
||||||
function mark_fn_def(tw, def, fn) {
|
function mark_fn_def(tw, def, fn) {
|
||||||
if (!HOP(fn, "safe_ids")) return;
|
if (!HOP(fn, "safe_ids")) return;
|
||||||
var marker = fn.safe_ids;
|
var marker = fn.safe_ids;
|
||||||
@@ -526,29 +533,25 @@ merge(Compressor.prototype, {
|
|||||||
if (fn.parent_scope.resolve().may_call_this === return_true) return;
|
if (fn.parent_scope.resolve().may_call_this === return_true) return;
|
||||||
if (marker) {
|
if (marker) {
|
||||||
var visited = member(fn, tw.fn_visited);
|
var visited = member(fn, tw.fn_visited);
|
||||||
if (marker === tw.safe_ids) return !visited && walk_fn_def(tw, fn);
|
if (marker === tw.safe_ids) {
|
||||||
if (visited) {
|
if (!visited) walk_fn_def(tw, fn);
|
||||||
fn.enclosed.forEach(function(d) {
|
} else if (!visited) {
|
||||||
if (fn.variables.get(d.name) === d) return;
|
fn.safe_ids = false;
|
||||||
if (safe_to_read(tw, d)) return;
|
} else fn.enclosed.forEach(function(d) {
|
||||||
d.single_use = false;
|
if (fn.variables.get(d.name) === d) return;
|
||||||
if (d.fixed instanceof AST_LambdaDefinition) return;
|
if (safe_to_read(tw, d)) return;
|
||||||
d.fixed = false;
|
d.single_use = false;
|
||||||
});
|
var fixed = d.fixed;
|
||||||
return;
|
if (typeof fixed == "function") fixed = fixed();
|
||||||
}
|
if (fixed instanceof AST_Lambda && HOP(fixed, "safe_ids")) return;
|
||||||
} else if (!tw.in_loop && !(tw.fn_scanning && tw.fn_scanning !== def.scope.resolve())) {
|
d.fixed = false;
|
||||||
|
});
|
||||||
|
} else if (tw.fn_scanning && tw.fn_scanning !== def.scope.resolve()) {
|
||||||
|
fn.safe_ids = false;
|
||||||
|
} else {
|
||||||
fn.safe_ids = tw.safe_ids;
|
fn.safe_ids = tw.safe_ids;
|
||||||
return walk_fn_def(tw, fn);
|
walk_fn_def(tw, fn);
|
||||||
}
|
}
|
||||||
fn.safe_ids = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function walk_fn_def(tw, fn) {
|
|
||||||
var was_scanning = tw.fn_scanning;
|
|
||||||
tw.fn_scanning = fn;
|
|
||||||
fn.walk(tw);
|
|
||||||
tw.fn_scanning = was_scanning;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pop_scope(tw, scope) {
|
function pop_scope(tw, scope) {
|
||||||
|
|||||||
@@ -2826,6 +2826,36 @@ functions_use_strict: {
|
|||||||
expect_stdout: "a true 42 function function function"
|
expect_stdout: "a true 42 function function function"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
functions_cross_scope_reference: {
|
||||||
|
options = {
|
||||||
|
functions: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
log = function(fn) {
|
||||||
|
console.log(typeof fn());
|
||||||
|
};
|
||||||
|
var a = function() {};
|
||||||
|
function f() {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
while (log(f));
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
log = function(fn) {
|
||||||
|
console.log(typeof fn());
|
||||||
|
};
|
||||||
|
function a() {}
|
||||||
|
function f() {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
while (log(f));
|
||||||
|
}
|
||||||
|
expect_stdout: "function"
|
||||||
|
}
|
||||||
|
|
||||||
functions_inner_var: {
|
functions_inner_var: {
|
||||||
options = {
|
options = {
|
||||||
functions: true,
|
functions: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user