Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize property access evaluation #1213

Merged
merged 1 commit into from Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 11 additions & 12 deletions lib/compress/evaluate.js
Expand Up @@ -342,7 +342,7 @@ const regexp_flags = new Set([
]);

def_eval(AST_PropAccess, function (compressor, depth) {
const obj = this.expression._eval(compressor, depth);
let obj = this.expression._eval(compressor, depth + 1);
if (obj === nullish || (this.optional && obj == null)) return nullish;
if (compressor.option("unsafe")) {
var key = this.property;
Expand All @@ -352,7 +352,6 @@ def_eval(AST_PropAccess, function (compressor, depth) {
return this;
}
var exp = this.expression;
var val;
if (is_undeclared_ref(exp)) {

var aa;
Expand All @@ -369,29 +368,29 @@ def_eval(AST_PropAccess, function (compressor, depth) {
}
if (!is_pure_native_value(exp.name, key))
return this;
val = global_objs[exp.name];
obj = global_objs[exp.name];
} else {
val = exp._eval(compressor, depth + 1);
if (val instanceof RegExp) {
if (obj instanceof RegExp) {
if (key == "source") {
return regexp_source_fix(val.source);
return regexp_source_fix(obj.source);
} else if (key == "flags" || regexp_flags.has(key)) {
return val[key];
return obj[key];
}
}
if (!val || val === exp || !HOP(val, key))
if (!obj || obj === exp || !HOP(obj, key))
return this;
if (typeof val == "function")

if (typeof obj == "function")
switch (key) {
case "name":
return val.node.name ? val.node.name.name : "";
return obj.node.name ? obj.node.name.name : "";
case "length":
return val.node.length_property();
return obj.node.length_property();
default:
return this;
}
}
return val[key];
return obj[key];
}
return this;
});
Expand Down
6 changes: 6 additions & 0 deletions lib/compress/index.js
Expand Up @@ -3799,6 +3799,12 @@ def_optimize(AST_Dot, function(self, compressor) {
const sub = self.flatten_object(self.property, compressor);
if (sub) return sub.optimize(compressor);
}

if (self.expression instanceof AST_PropAccess
&& parent instanceof AST_PropAccess) {
return self;
}

let ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
Expand Down
14 changes: 14 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -1851,3 +1851,17 @@ regexp_property_eval: {
console.log(false);
}
}


unsafe_deep_chain: {
options = {
evaluate: true,
unsafe: true,
}
input: {
a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z;
}
expect: {
a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z;
}
}