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

fix(es/minifier): Prevent removing side effects from accessing getter #9530

Merged
merged 7 commits into from
Sep 4, 2024

Conversation

CPunisher
Copy link
Member

Closes #9500

Caused by

self.ignore_return_value(obj, opts);
. When the object with getters pass to self.ignore_return_value,
self.optimize_expr_in_bool_ctx(e, true);

converts the object to 0 because the object is side-effect-free according to
Expr::Object(ObjectLit { props, .. }) => props.iter().any(|node| match node {

We should skip this process to fix the issue.

As is known only accessing getters and setters may cause side effect, we can safely do the transformation when none of them appears in the object. More precision is possible if comparing the lit prop names. I also collect computed keys of getters and setters in the object, is there any bad case?

The reason why only numeric (string) key removes the statement is that string key (Computed) is converted to Ident in other phases, e.g. {}['a'] => {}.a, which does not matching the pattern.

@CPunisher CPunisher requested a review from a team as a code owner September 4, 2024 04:12
Copy link

changeset-bot bot commented Sep 4, 2024

🦋 Changeset detected

Latest commit: 78cb61a

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kdy1 kdy1 added this to the Planned milestone Sep 4, 2024
Copy link

codspeed-hq bot commented Sep 4, 2024

CodSpeed Performance Report

Merging #9530 will degrade performances by 3.52%

Comparing CPunisher:fix/member-expression (78cb61a) with main (84b0043)

Summary

❌ 1 regressions
✅ 177 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main CPunisher:fix/member-expression Change
es/full/bugs-1 529.3 µs 548.7 µs -3.52%

Copy link
Member

@Austaras Austaras left a comment

Choose a reason for hiding this comment

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

Better to add tests at issues/9500, otherwise good.

@Austaras Austaras changed the title fix(es/minifier): prevent removing side effects from accessing getter with numeric string key fix(es/minifier): prevent removing side effects from accessing getter Sep 4, 2024
Austaras
Austaras previously approved these changes Sep 4, 2024
@kdy1 kdy1 requested a review from a team as a code owner September 4, 2024 11:52
@kdy1 kdy1 changed the title fix(es/minifier): prevent removing side effects from accessing getter fix(es/minifier): Prevent removing side effects from accessing getter Sep 4, 2024
@kdy1 kdy1 enabled auto-merge (squash) September 4, 2024 11:53
@kdy1 kdy1 merged commit 8513816 into swc-project:main Sep 4, 2024
154 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.7.24 Sep 8, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Minifier removes side effects from accessing getter with numeric string key
3 participants