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,15 +12901,16 @@ 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 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 scope, values = [];
var call, scope, values = [];
for (var j = 0; j < props.length; j++) {
var value = props[j].value;
if (props[j] instanceof AST_ObjectMethod) {
@@ -12924,21 +12925,21 @@ Compressor.prototype.compress = function(node) {
var ctor;
if (arrow) {
ctor = is_async(value) ? AST_AsyncArrow : AST_Arrow;
} else if (i === j && !(compressor.parent() instanceof AST_Call)) {
return;
} else {
} else if (i != j
|| (call = compressor.parent()) instanceof AST_Call && call.expression === self) {
ctor = value.CTOR;
} else {
return;
}
value = make_node(ctor, value, value);
}
values.push(value);
}
return make_node(AST_Sub, this, {
return make_node(AST_Sub, self, {
expression: make_node(AST_Array, expr, { elements: values }),
property: make_node(AST_Number, this, { value: i }),
property: make_node(AST_Number, self, { value: i }),
});
}
}
});
OPT(AST_Dot, function(self, compressor) {

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