diff --git a/lib/compress.js b/lib/compress.js index 8b1ec1cf..9891e4be 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12901,43 +12901,44 @@ Compressor.prototype.compress = function(node) { AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) { if (!compressor.option("properties")) return; if (key === "__proto__") return; - var expr = this.expression; - if (expr instanceof AST_Object) { - var props = expr.properties; - for (var i = props.length; --i >= 0;) { - var prop = props[i]; - if (prop.key !== key) continue; - if (!all(props, can_hoist_property)) return; - if (!safe_to_flatten(prop.value, compressor)) return; - var scope, values = []; - for (var j = 0; j < props.length; j++) { - var value = props[j].value; - if (props[j] instanceof AST_ObjectMethod) { - var arrow = !(value.uses_arguments || is_generator(value) || value.contains_this()); - if (arrow) { - if (!scope) scope = compressor.find_parent(AST_Scope); - var avoid = avoid_await_yield(scope); - value.each_argname(function(argname) { - if (avoid[argname.name]) arrow = false; - }); - } - var ctor; - if (arrow) { - ctor = is_async(value) ? AST_AsyncArrow : AST_Arrow; - } else if (i === j && !(compressor.parent() instanceof AST_Call)) { - return; - } else { - ctor = value.CTOR; - } - value = make_node(ctor, value, value); + var self = this; + var expr = self.expression; + if (!(expr instanceof AST_Object)) return; + var props = expr.properties; + for (var i = props.length; --i >= 0;) { + var prop = props[i]; + if (prop.key !== key) continue; + if (!all(props, can_hoist_property)) return; + if (!safe_to_flatten(prop.value, compressor)) return; + var call, scope, values = []; + for (var j = 0; j < props.length; j++) { + var value = props[j].value; + if (props[j] instanceof AST_ObjectMethod) { + var arrow = !(value.uses_arguments || is_generator(value) || value.contains_this()); + if (arrow) { + if (!scope) scope = compressor.find_parent(AST_Scope); + var avoid = avoid_await_yield(scope); + value.each_argname(function(argname) { + if (avoid[argname.name]) arrow = false; + }); } - values.push(value); + var ctor; + if (arrow) { + ctor = is_async(value) ? AST_AsyncArrow : AST_Arrow; + } else if (i != j + || (call = compressor.parent()) instanceof AST_Call && call.expression === self) { + ctor = value.CTOR; + } else { + return; + } + value = make_node(ctor, value, value); } - return make_node(AST_Sub, this, { - expression: make_node(AST_Array, expr, { elements: values }), - property: make_node(AST_Number, this, { value: i }), - }); + values.push(value); } + return make_node(AST_Sub, self, { + expression: make_node(AST_Array, expr, { elements: values }), + property: make_node(AST_Number, self, { value: i }), + }); } }); diff --git a/test/compress/classes.js b/test/compress/classes.js index 2713a248..441d8bf0 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -2502,3 +2502,39 @@ issue_5352: { expect_stdout: "PASS" node_version: ">=12" } + +issue_5387: { + options = { + properties: true, + } + input: { + "use strict"; + (function(a) { + try { + class A extends a {} + } catch (e) { + console.log("PASS"); + } + })({ + f() { + return this; + } + }.f); + } + expect: { + "use strict"; + (function(a) { + try { + class A extends a {} + } catch (e) { + console.log("PASS"); + } + })({ + f() { + return this; + } + }.f); + } + expect_stdout: "PASS" + node_version: ">=4" +}