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

Add unsafe optimization to inline undefined for missing objlit props #589

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions lib/compress/index.js
Expand Up @@ -6878,6 +6878,7 @@ def_optimize(AST_Sub, function(self, compressor) {
if (key !== prop) {
var sub = self.flatten_object(property, compressor);
if (sub) {
if (sub instanceof AST_Undefined) return sub;
expr = self.expression = sub.expression;
prop = self.property = sub.property;
}
Expand Down Expand Up @@ -6941,14 +6942,37 @@ AST_Lambda.DEFMETHOD("contains_this", function() {
});
});

const objectPrototypeProperties = new Set([
"__count__",
"__defineGetter__",
"__defineSetter__",
"__lookupGetter__",
"__lookupSetter__",
"__noSuchMethod__",
"__parent__",
"__proto__",
"constructor",
"eval",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toLocaleString",
"toSource",
"toString",
"unwatch",
"valueOf",
"watch",
]);
AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
if (!compressor.option("properties")) return;
var arrows = compressor.option("unsafe_arrows") && compressor.option("ecma") >= 2015;
var expr = this.expression;
if (expr instanceof AST_Object) {
var props = expr.properties;
var collapse_missing_props = compressor.option("unsafe");
for (var i = props.length; --i >= 0;) {
var prop = props[i];
if (prop.value instanceof AST_Accessor) collapse_missing_props = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a need for this condition here. If any of the properties is an accessor, it can still be doomed to oblivion :)

if ("" + (prop instanceof AST_ConciseMethod ? prop.key.name : prop.key) == key) {
if (!props.every((prop) => {
return prop instanceof AST_ObjectKeyVal
Expand All @@ -6973,6 +6997,9 @@ AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
});
}
}
if (i < 0 && collapse_missing_props && !objectPrototypeProperties.has(key)) {
return make_node(AST_Undefined, this);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will get rid of side effects in other properties of the object. Picture this (too contrived) use case:

const x = {
  iHaveSideEffects: sideEffects(),
  foo
}.bar

If we turned this object into undefined outright here, the call with side effects disappears. There's a method which can fix this, AST_Node#drop_side_effect_free which when called on the object X is being initialised to, would return simply sideEffects(). Be careful because when nothing has side effects this method just returns null.

But! We want the result of the expression to really be undefined.

So we can call make_sequence with an array of two which results in (sideEffects(), undefined). The sequence expression is terrible in terms of readability, but it's used extensively in Terser for being an expression instead of a statement.

There is, however, some calling code of flatten_object which expects the return to be either null or a AST_Sub with a number. I'll leave that one up to you but maybe returning a pair would work ;)

}
});

Expand Down
17 changes: 17 additions & 0 deletions test/compress/properties.js
Expand Up @@ -1224,6 +1224,23 @@ prop_side_effects_2: {
]
}

missing_prop: {
options = {
inline: true,
properties: true,
unsafe: true,
}
input: {
console.log({x: 42}.y);
}
expect: {
console.log(void 0);
}
expect_stdout: [
"undefined",
]
}

accessor_1: {
options = {
properties: true,
Expand Down