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

Minify property accesses on object literals #1179

Merged
merged 1 commit into from
Apr 23, 2021
Merged

Minify property accesses on object literals #1179

merged 1 commit into from
Apr 23, 2021

Conversation

evanw
Copy link
Owner

@evanw evanw commented Apr 23, 2021

The code {a: {b: 1}}.a.b will now be minified to 1. This optimization is relatively complex and hard to do safely. Here are some tricky cases that are correctly handled:

var obj = {a: 1}
assert({a: 1, a: 2}.a === 2)
assert({a: 1, [String.fromCharCode(97)]: 2}.a === 2)
assert({__proto__: obj}.a === 1)
assert({__proto__: null}.a === undefined)
assert({__proto__: null}.__proto__ === undefined)
assert({a: function() { return this.b }, b: 1}.a() === 1)
assert(({a: 1}.a = 2) === 2)
assert(++{a: 1}.a === 2)
assert.throws(() => { new ({ a() {} }.a) })

Fixes #1166

@evanw evanw merged commit 4109a81 into master Apr 23, 2021
@evanw evanw deleted the issues/1166 branch April 23, 2021 00:12
@@ -2930,6 +2930,18 @@ func TestMangleObject(t *testing.T) {
expectPrintedMangle(t, "x = {a, ...'123', b}", "x = {a, ...\"123\", b};\n")
expectPrintedMangle(t, "x = {a, ...[1, 2, 3], b}", "x = {a, ...[1, 2, 3], b};\n")
expectPrintedMangle(t, "x = {a, ...(()=>{})(), b}", "x = {a, ...(() => {\n})(), b};\n")

// Check simple cases of object simplification (advanced cases are checked in end-to-end tests)
expectPrintedMangle(t, "x = {['y']: z}.y", "x = {y: z}.y;\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

These, by themselves, are amazing! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify constant object access
2 participants