aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-07-11 03:59:57 +0100
committerGitHub <noreply@github.com>2021-07-11 10:59:57 +0800
commit08391b8e1c35da8b094aa4eebe6e6b6bbf5f3019 (patch)
tree59b2c7ef1f67a407e1564efc2c89559f026d126a
parentd147d5d7f0bc61b1284a18a31eaa41c208a278ea (diff)
downloadtracifyjs-08391b8e1c35da8b094aa4eebe6e6b6bbf5f3019.tar.gz
tracifyjs-08391b8e1c35da8b094aa4eebe6e6b6bbf5f3019.zip
fix corner case in `hoist_props` (#5069)
-rw-r--r--lib/ast.js12
-rw-r--r--lib/compress.js20
-rw-r--r--test/compress/hoist_props.js47
3 files changed, 55 insertions, 24 deletions
diff --git a/lib/ast.js b/lib/ast.js
index 6057c9b5..76db9f95 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -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() {
diff --git a/lib/compress.js b/lib/compress.js
index 947ca6cd..e9fe51b9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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: [] });
}
}));
diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js
index 597db4bc..2b4abff1 100644
--- a/test/compress/hoist_props.js
+++ b/test/compress/hoist_props.js
@@ -786,6 +786,32 @@ issue_3071_1: {
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,
+ inline: true,
+ join_vars: true,
+ passes: 3,
+ reduce_vars: true,
+ sequences: true,
+ side_effects: true,
toplevel: true,
unused: 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"
+}