simplify traversal logic (#4063)

This commit is contained in:
Alex Lam S.L
2020-08-23 05:45:35 +08:00
committed by GitHub
parent f2d486e771
commit 35fe1092d3
2 changed files with 21 additions and 17 deletions

View File

@@ -1174,10 +1174,7 @@ function OutputStream(options) {
}); });
/* -----[ other expressions ]----- */ /* -----[ other expressions ]----- */
DEFPRINT(AST_Call, function(self, output) { function print_call_args(self, output) {
self.expression.print(output);
if (self instanceof AST_New && !need_constructor_parens(self, output))
return;
if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) { if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
output.add_mapping(self.start); output.add_mapping(self.start);
} }
@@ -1187,11 +1184,16 @@ function OutputStream(options) {
expr.print(output); expr.print(output);
}); });
}); });
}
DEFPRINT(AST_Call, function(self, output) {
self.expression.print(output);
print_call_args(self, output);
}); });
DEFPRINT(AST_New, function(self, output) { DEFPRINT(AST_New, function(self, output) {
output.print("new"); output.print("new");
output.space(); output.space();
AST_Call.prototype._codegen(self, output); self.expression.print(output);
if (need_constructor_parens(self, output)) print_call_args(self, output);
}); });
DEFPRINT(AST_Sequence, function(self, output) { DEFPRINT(AST_Sequence, function(self, output) {
self.expressions.forEach(function(node, index) { self.expressions.forEach(function(node, index) {

View File

@@ -559,21 +559,23 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
options = _default_mangler_options(options); options = _default_mangler_options(options);
base54.reset(); base54.reset();
try { try {
AST_Node.prototype.print = function(stream, force_parens) { var fn = AST_Symbol.prototype.add_source_map;
this._print(stream, force_parens); AST_Symbol.prototype.add_source_map = function() {
if (this instanceof AST_Symbol && !this.unmangleable(options)) { if (!this.unmangleable(options)) base54.consider(this.name, -1);
base54.consider(this.name, -1);
} else if (options.properties) {
if (this instanceof AST_Dot) {
base54.consider(this.property, -1);
} else if (this instanceof AST_Sub) {
skip_string(this.property);
}
}
}; };
if (options.properties) {
AST_Dot.prototype.add_source_map = function() {
base54.consider(this.property, -1);
};
AST_Sub.prototype.add_source_map = function() {
skip_string(this.property);
};
}
base54.consider(this.print_to_string(), 1); base54.consider(this.print_to_string(), 1);
} finally { } finally {
AST_Node.prototype.print = AST_Node.prototype._print; AST_Symbol.prototype.add_source_map = fn;
delete AST_Dot.prototype.add_source_map;
delete AST_Sub.prototype.add_source_map;
} }
base54.sort(); base54.sort();