enhance functions (#3368)
This commit is contained in:
@@ -3596,13 +3596,17 @@ merge(Compressor.prototype, {
|
|||||||
} else if (compressor.option("functions")
|
} else if (compressor.option("functions")
|
||||||
&& def.value === def.name.fixed_value()
|
&& def.value === def.name.fixed_value()
|
||||||
&& def.value instanceof AST_Function
|
&& def.value instanceof AST_Function
|
||||||
&& !def.value.name
|
&& can_rename(def.value, def.name.name)
|
||||||
&& !def.value.variables.get(def.name.name)
|
|
||||||
&& (!compressor.has_directive("use strict") || parent instanceof AST_Scope)) {
|
&& (!compressor.has_directive("use strict") || parent instanceof AST_Scope)) {
|
||||||
compressor.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
|
compressor.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
|
||||||
var defun = make_node(AST_Defun, def, def.value);
|
var defun = make_node(AST_Defun, def, def.value);
|
||||||
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
|
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
|
||||||
def.name.scope.resolve().def_function(defun.name);
|
var name_def = def.name.scope.resolve().def_function(defun.name);
|
||||||
|
if (def.value.name) def.value.name.definition().references.forEach(function(ref) {
|
||||||
|
ref.name = name_def.name;
|
||||||
|
ref.thedef = name_def;
|
||||||
|
ref.reference({});
|
||||||
|
});
|
||||||
body.push(defun);
|
body.push(defun);
|
||||||
} else {
|
} else {
|
||||||
if (side_effects.length > 0) {
|
if (side_effects.length > 0) {
|
||||||
@@ -3633,6 +3637,11 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
sym.eliminated++;
|
sym.eliminated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function can_rename(fn, name) {
|
||||||
|
var def = fn.variables.get(name);
|
||||||
|
return !def || fn.name && def === fn.name.definition();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if (head.length > 0 || tail.length > 0) {
|
if (head.length > 0 || tail.length > 0) {
|
||||||
node.definitions = head.concat(tail);
|
node.definitions = head.concat(tail);
|
||||||
|
|||||||
@@ -2712,8 +2712,8 @@ functions: {
|
|||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
!function() {
|
!function() {
|
||||||
var a = function() {
|
var a = function a() {
|
||||||
return "a";
|
return a && "a";
|
||||||
};
|
};
|
||||||
var b = function x() {
|
var b = function x() {
|
||||||
return !!x;
|
return !!x;
|
||||||
@@ -2736,19 +2736,19 @@ functions: {
|
|||||||
expect: {
|
expect: {
|
||||||
!function() {
|
!function() {
|
||||||
function a() {
|
function a() {
|
||||||
return "a";
|
return a && "a";
|
||||||
|
}
|
||||||
|
function b() {
|
||||||
|
return !!b;
|
||||||
}
|
}
|
||||||
var b = function x() {
|
|
||||||
return !!x;
|
|
||||||
};
|
|
||||||
var c = function(c) {
|
var c = function(c) {
|
||||||
return c;
|
return c;
|
||||||
};
|
};
|
||||||
if (c(b(a()))) {
|
if (c(b(a()))) {
|
||||||
function d() {}
|
function d() {}
|
||||||
var e = function y() {
|
function e() {
|
||||||
return typeof y;
|
return typeof e;
|
||||||
};
|
}
|
||||||
var f = function(f) {
|
var f = function(f) {
|
||||||
return f;
|
return f;
|
||||||
};
|
};
|
||||||
@@ -2768,8 +2768,8 @@ functions_use_strict: {
|
|||||||
input: {
|
input: {
|
||||||
"use strict";
|
"use strict";
|
||||||
!function() {
|
!function() {
|
||||||
var a = function() {
|
var a = function a() {
|
||||||
return "a";
|
return a && "a";
|
||||||
};
|
};
|
||||||
var b = function x() {
|
var b = function x() {
|
||||||
return !!x;
|
return !!x;
|
||||||
@@ -2793,11 +2793,11 @@ functions_use_strict: {
|
|||||||
"use strict";
|
"use strict";
|
||||||
!function() {
|
!function() {
|
||||||
function a() {
|
function a() {
|
||||||
return "a";
|
return a && "a";
|
||||||
|
}
|
||||||
|
function b() {
|
||||||
|
return !!b;
|
||||||
}
|
}
|
||||||
var b = function x() {
|
|
||||||
return !!x;
|
|
||||||
};
|
|
||||||
var c = function(c) {
|
var c = function(c) {
|
||||||
return c;
|
return c;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user