fix corner case in properties (#5388)

fixes #5387
This commit is contained in:
Alex Lam S.L
2022-03-20 16:01:42 +00:00
committed by GitHub
parent 5e30f3a48b
commit a8e040b133
2 changed files with 71 additions and 34 deletions

View File

@@ -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 }),
});
}
});

View File

@@ -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"
}