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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
if ("" + (prop instanceof AST_ConciseMethod ? prop.key.name : prop.key) == key) { | ||
if (!props.every((prop) => { | ||
return prop instanceof AST_ObjectKeyVal | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
If we turned this object into But! We want the result of the expression to really be So we can call There is, however, some calling code of |
||
} | ||
}); | ||
|
||
|
There was a problem hiding this comment.
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 :)