implement --expression (#5607)

This commit is contained in:
Alex Lam S.L
2022-08-07 20:42:18 +01:00
committed by GitHub
parent 07953b36b0
commit f451a7ad79
10 changed files with 143 additions and 50 deletions

View File

@@ -343,6 +343,35 @@ Compressor.prototype.compress = function(node) {
});
self.transform(tt);
});
AST_Toplevel.DEFMETHOD("unwrap_expression", function() {
var self = this;
switch (self.body.length) {
case 0:
return make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: make_node(AST_Number, self, { value: 0 }),
});
case 1:
var stat = self.body[0];
if (stat instanceof AST_Directive) return make_node(AST_String, self, stat);
if (stat instanceof AST_SimpleStatement) return stat.body;
default:
return make_node(AST_Call, self, {
expression: make_node(AST_Function, self, {
argnames: [],
body: self.body,
}).init_vars(self),
args: [],
});
}
return self;
});
AST_Node.DEFMETHOD("wrap_expression", function() {
var self = this;
if (!is_statement(self)) self = make_node(AST_SimpleStatement, self, { body: self });
if (!(self instanceof AST_Toplevel)) self = make_node(AST_Toplevel, self, { body: [ self ] });
return self;
});
function read_property(obj, node) {
var key = node.get_property();