From: Alex Lam S.L Date: Sat, 22 Aug 2020 21:45:35 +0000 (+0800) Subject: simplify traversal logic (#4063) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=35fe1092d368f3a79df54120d5e1c1dcb268af1a;p=UglifyJS.git simplify traversal logic (#4063) --- diff --git a/lib/output.js b/lib/output.js index a9e0ca7b..19c37b29 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1174,10 +1174,7 @@ function OutputStream(options) { }); /* -----[ other expressions ]----- */ - DEFPRINT(AST_Call, function(self, output) { - self.expression.print(output); - if (self instanceof AST_New && !need_constructor_parens(self, output)) - return; + function print_call_args(self, output) { if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) { output.add_mapping(self.start); } @@ -1187,11 +1184,16 @@ function OutputStream(options) { expr.print(output); }); }); + } + DEFPRINT(AST_Call, function(self, output) { + self.expression.print(output); + print_call_args(self, output); }); DEFPRINT(AST_New, function(self, output) { output.print("new"); 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) { self.expressions.forEach(function(node, index) { diff --git a/lib/scope.js b/lib/scope.js index 3297b8e6..450c1062 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -559,21 +559,23 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) { options = _default_mangler_options(options); base54.reset(); try { - AST_Node.prototype.print = function(stream, force_parens) { - this._print(stream, force_parens); - if (this instanceof AST_Symbol && !this.unmangleable(options)) { - 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); - } - } + var fn = AST_Symbol.prototype.add_source_map; + AST_Symbol.prototype.add_source_map = function() { + if (!this.unmangleable(options)) base54.consider(this.name, -1); }; + 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); } 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();