feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -176,6 +176,7 @@ import {
is_undefined,
is_lhs,
aborts,
is_used_in_expression,
} from "./inference.js";
import {
SQUEEZED,
@@ -195,6 +196,7 @@ import {
make_sequence,
best_of,
best_of_expression,
make_empty_function,
make_node_from_constant,
merge_sequence,
get_simple_key,
@@ -510,9 +512,8 @@ def_optimize(AST_Node, function(self) {
});
AST_Toplevel.DEFMETHOD("drop_console", function(options) {
var isArray = Array.isArray(options);
return this.transform(new TreeTransformer(function(self) {
const isArray = Array.isArray(options);
const tt = new TreeTransformer(function(self) {
if (self.TYPE !== "Call") {
return;
}
@@ -523,18 +524,35 @@ AST_Toplevel.DEFMETHOD("drop_console", function(options) {
return;
}
if (isArray && options.indexOf(exp.property) === -1) {
if (isArray && !options.includes(exp.property)) {
return;
}
var name = exp.expression;
var depth = 2;
while (name.expression) {
name = name.expression;
depth++;
}
if (is_undeclared_ref(name) && name.name == "console") {
return make_node(AST_Undefined, self);
if (
depth === 3
&& !["call", "apply"].includes(exp.property)
&& is_used_in_expression(tt)
) {
// a (used) call to Function.prototype methods (eg: console.log.bind(console))
// but not .call and .apply which would also return undefined.
exp.expression = make_empty_function(self);
set_flag(exp.expression, SQUEEZED);
self.args = [];
} else {
return make_node(AST_Undefined, self);
}
}
}));
});
return this.transform(tt);
});
AST_Node.DEFMETHOD("equivalent_to", function(node) {
@@ -1233,7 +1251,9 @@ def_optimize(AST_Switch, function(self, compressor) {
eliminate_branch(branch, body[body.length - 1]);
continue;
}
if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
if (exp instanceof AST_Node && !exp.has_side_effects(compressor)) {
exp = branch.expression.tail_node().evaluate(compressor);
}
if (exp === value) {
exact_match = branch;
if (default_branch) {
@@ -1415,12 +1435,9 @@ def_optimize(AST_Switch, function(self, compressor) {
break DEFAULT;
}
let sideEffect = body.find(branch => {
return (
branch !== default_or_exact
&& branch.expression.has_side_effects(compressor)
);
});
let sideEffect = body.find(
branch => branch !== default_or_exact && branch.expression.has_side_effects(compressor)
);
// If no cases cause a side-effect, we can eliminate the switch entirely.
if (!sideEffect) {
return make_node(AST_BlockStatement, self, {
@@ -1528,9 +1545,10 @@ def_optimize(AST_Switch, function(self, compressor) {
right: branch.expression,
}),
body: consequent,
alternative: null
})
].concat(always)
alternative: null,
}),
always,
],
}).optimize(compressor);
}
return self;
@@ -1553,13 +1571,12 @@ def_optimize(AST_Switch, function(self, compressor) {
let pblock = make_node(AST_BlockStatement, prev, { body: pbody });
return bblock.equivalent_to(pblock);
}
function statement(expression) {
return make_node(AST_SimpleStatement, expression, {
body: expression
});
function statement(body) {
return make_node(AST_SimpleStatement, body, { body });
}
function has_nested_break(root) {
let has_break = false;
let tw = new TreeWalker(node => {
if (has_break) return true;
if (node instanceof AST_Lambda) return true;
@@ -1912,10 +1929,7 @@ def_optimize(AST_Call, function(self, compressor) {
&& is_undeclared_ref(exp)
&& exp.name == "Function") {
// new Function() => function(){}
if (self.args.length == 0) return make_node(AST_Function, self, {
argnames: [],
body: []
}).optimize(compressor);
if (self.args.length == 0) return make_empty_function(self).optimize(compressor);
if (self.args.every((x) => x instanceof AST_String)) {
// quite a corner-case, but we can handle it:
// https://github.com/mishoo/UglifyJS2/issues/203
@@ -3675,10 +3689,7 @@ def_optimize(AST_Dot, function(self, compressor) {
});
break;
case "Function":
self.expression = make_node(AST_Function, self.expression, {
argnames: [],
body: []
});
self.expression = make_empty_function(self.expression);
break;
case "Number":
self.expression = make_node(AST_Number, self.expression, {