replace single-use class definitions (#2524)

fixes #2416
This commit is contained in:
Alex Lam S.L
2017-11-28 20:57:15 +08:00
committed by GitHub
parent b84c99ef5c
commit 37cbd7080c
4 changed files with 44 additions and 12 deletions

View File

@@ -908,7 +908,7 @@ var AST_ConciseMethod = DEFNODE("ConciseMethod", "quote static is_generator asyn
$documentation: "An ES6 concise method inside an object or class"
}, AST_ObjectProperty);
var AST_Class = DEFNODE("Class", "name extends properties", {
var AST_Class = DEFNODE("Class", "name extends properties inlined", {
$propdoc: {
name: "[AST_SymbolClass|AST_SymbolDefClass?] optional class name.",
extends: "[AST_Node]? optional parent class",

View File

@@ -329,6 +329,7 @@ merge(Compressor.prototype, {
value = node.fixed_value();
if (value && ref_once(d) && !compressor.exposed(d)) {
d.single_use = value instanceof AST_Lambda
|| value instanceof AST_Class
|| d.scope === node.scope && value.is_constant_expression();
} else {
d.single_use = false;
@@ -386,7 +387,7 @@ merge(Compressor.prototype, {
}
}
}
if (node instanceof AST_Defun) {
if (node instanceof AST_DefClass || node instanceof AST_Defun) {
node.inlined = false;
var d = node.name.definition();
if (compressor.exposed(d) || safe_to_read(d)) {
@@ -403,6 +404,13 @@ merge(Compressor.prototype, {
safe_ids = save_ids;
return true;
}
if (node instanceof AST_ClassExpression) {
node.inlined = false;
push();
descend();
pop();
return true;
}
if (is_func_expr(node)) {
node.inlined = false;
push();
@@ -2385,9 +2393,8 @@ merge(Compressor.prototype, {
return false;
return true;
}
def(AST_Node, return_false);
def(AST_Constant, return_true);
def(AST_Lambda, function(scope){
function all_refs_local(scope) {
var self = this;
var result = true;
self.walk(new TreeWalker(function(node) {
@@ -2413,7 +2420,12 @@ merge(Compressor.prototype, {
}
}));
return result;
});
}
def(AST_Node, return_false);
def(AST_Constant, return_true);
def(AST_Class, all_refs_local);
def(AST_Lambda, all_refs_local);
def(AST_Unary, function(){
return this.expression.is_constant_expression();
});
@@ -4599,12 +4611,15 @@ merge(Compressor.prototype, {
&& is_lhs(self, compressor.parent()) !== self) {
var d = self.definition();
var fixed = self.fixed_value();
if (fixed instanceof AST_DefClass) {
d.fixed = fixed = make_node(AST_ClassExpression, fixed, fixed);
}
if (fixed instanceof AST_Defun) {
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
}
if (d.single_use && is_func_expr(fixed)) {
if (d.single_use && (is_func_expr(fixed) || fixed instanceof AST_ClassExpression)) {
if (d.scope !== self.scope
&& (!compressor.option("reduce_funcs")
&& (!compressor.option("reduce_funcs") && is_func_expr(fixed)
|| d.escaped
|| fixed.inlined)) {
d.single_use = false;