fix corner case in hoist_props (#5069)

This commit is contained in:
Alex Lam S.L
2021-07-11 03:59:57 +01:00
committed by GitHub
parent d147d5d7f0
commit 08391b8e1c
3 changed files with 55 additions and 24 deletions

View File

@@ -1345,16 +1345,10 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression optional property", {
optional: "[boolean] whether the expression is optional chaining",
property: "[AST_Node|string] the property to access. For AST_Dot this is always a plain string, while for AST_Sub it's an arbitrary AST_Node",
},
getProperty: function() {
get_property: function() {
var p = this.property;
if (p instanceof AST_Constant) {
return p.value;
}
if (p instanceof AST_UnaryPrefix
&& p.operator == "void"
&& p.expression instanceof AST_Constant) {
return;
}
if (p instanceof AST_Constant) return p.value;
if (p instanceof AST_UnaryPrefix && p.operator == "void" && p.expression instanceof AST_Constant) return;
return p;
},
_validate: function() {

View File

@@ -347,7 +347,7 @@ merge(Compressor.prototype, {
});
function read_property(obj, node) {
var key = node.getProperty();
var key = node.get_property();
if (key instanceof AST_Node) return;
var value;
if (obj instanceof AST_Array) {
@@ -7597,12 +7597,11 @@ merge(Compressor.prototype, {
}
}));
self.transform(new TreeTransformer(function(node, descend) {
if (node instanceof AST_Binary) return replace("right");
if (node instanceof AST_PropAccess) {
if (!(node.expression instanceof AST_SymbolRef)) return;
var defs = defs_by_id[node.expression.definition().id];
if (!defs) return;
var def = defs.get(node.getProperty());
var def = defs.get(node.get_property());
var sym = make_node(AST_SymbolRef, node, {
name: def.name,
scope: node.expression.scope,
@@ -7611,18 +7610,9 @@ merge(Compressor.prototype, {
sym.reference();
return sym;
}
if (node instanceof AST_Unary) {
if (unary_side_effects[node.operator]) return;
return replace("expression");
}
function replace(prop) {
var sym = node[prop];
if (!(sym instanceof AST_SymbolRef)) return;
if (!(sym.definition().id in defs_by_id)) return;
var opt = node.clone();
opt[prop] = make_node(AST_Object, sym, { properties: [] });
return opt;
if (node instanceof AST_SymbolRef) {
if (!(node.definition().id in defs_by_id)) return;
return make_node(AST_Object, node, { properties: [] });
}
}));

View File

@@ -777,6 +777,32 @@ issue_3046: {
}
issue_3071_1: {
options = {
evaluate: true,
hoist_props: true,
inline: true,
join_vars: true,
passes: 3,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
(function() {
var obj = {};
obj.one = 1;
obj.two = 2;
console.log(obj.one, obj.two);
})();
}
expect: {
console.log(1, 2);
}
expect_stdout: "1 2"
}
issue_3071_1_toplevel: {
options = {
evaluate: true,
hoist_props: true,
@@ -1094,3 +1120,24 @@ object_super: {
expect_stdout: "PASS"
node_version: ">=4"
}
issue_4985: {
options = {
hoist_props: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = { p: 42 };
console.log(function() {
a;
}());
}
expect: {
var a_p = 42;
console.log(function() {
({});
}());
}
expect_stdout: "undefined"
}