Skip to content

Commit

Permalink
fix corner case in hoist_props (#5069)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 11, 2021
1 parent d147d5d commit 08391b8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
12 changes: 3 additions & 9 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 5 additions & 15 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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: [] });
}
}));

Expand Down
47 changes: 47 additions & 0 deletions test/compress/hoist_props.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
}

0 comments on commit 08391b8

Please sign in to comment.