fix(minifier): recognize object spread of object literals as side-effect-free#20299
fix(minifier): recognize object spread of object literals as side-effect-free#20299graphite-app[bot] merged 1 commit intomainfrom
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Merge activity
|
There was a problem hiding this comment.
Pull request overview
This PR fixes the minifier to recognize that spreading object literals (e.g., ({...{}}), ({...{a: 1}})) is side-effect-free when all the inner properties are side-effect-free. Previously, object spreads were conservatively treated as always having side effects (since they could invoke getters on unknown values), which prevented removing unused expressions like ({...{}}).
Changes:
- Add
ObjectExpressionhandling inObjectPropertyKind::SpreadProperty'smay_have_side_effectsto recursively check inner properties - Fix
remove_unused_object_exprto check side effects for all-spread objects instead of unconditionally bailing out - Add tests for both the side-effects analysis and peephole optimization
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/oxc_ecmascript/src/side_effects/expressions.rs |
Adds ObjectExpression case in spread property side-effect analysis, recursively checking inner properties |
crates/oxc_minifier/src/peephole/remove_unused_expression.rs |
Changes the all-spread early return from return false to checking if any spread has side effects |
crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs |
Updates existing tests and adds new test cases for spreading object literals |
crates/oxc_minifier/tests/peephole/remove_unused_expression.rs |
Adds peephole optimization tests for spreading object literals |
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c92399b05e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Expression::ObjectExpression(obj) => obj | ||
| .properties | ||
| .iter() | ||
| .any(|property| property.may_have_side_effects(ctx)), |
There was a problem hiding this comment.
Treat getter-backed object spreads as side-effectful
When property_read_side_effects is All (the default), spreading an object literal with accessor properties must be considered effectful because object spread performs a property read (Get) for each enumerable key. This new branch only checks property.may_have_side_effects(ctx), which does not account for PropertyKind::Get, so expressions like ({...{ get a() { foo() } }}) can be misclassified as side-effect-free and then removed by remove_unused_object_expr, dropping the getter invocation.
Useful? React with 👍 / 👎.
…ect-free (#20299) ## Summary - Add `ObjectExpression` handling in `ObjectPropertyKind::SpreadProperty`'s `may_have_side_effects` — spreading an object literal is side-effect-free when all its properties are - Fix `remove_unused_object_expr` to check side effects for all-spread objects instead of unconditionally bailing Closes rolldown/rolldown#8582
c92399b to
5c97b14
Compare
### 🚀 Features - e7163b6 ecmascript: Add known-globals to side-effect-free property reads (#20212) (Dunqing) - 139ab68 ecmascript: Add `property_write_side_effects` to `MayHaveSideEffectsContext` (#20217) (Dunqing) ### 🐛 Bug Fixes - 78c264a parser: Fix conditional expressions with arrow-function alternates in TS (#20356) (camc314) - 5c97b14 minifier: Recognize object spread of object literals as side-effect-free (#20299) (Boshen) - 1ff5c1d transformer/typescript: Rewrite extensions in dynamic `import()` expressions (#20121) (Sverre Johansen) - 1c07b3b diagnostics: Handle `WouldBlock` in stdout writes to prevent panic (#20295) (Boshen) - ade14d4 ecmascript: Enhance side-effect detection for classes, TypedArrays, computed members, and spread (#20213) (Dunqing) ### ⚡ Performance - 5474d0a semantic: V8-style walk-up reference resolution (#20292) (Boshen) ### 📚 Documentation - e4aa5b5 parser/napi, linter/plugins: Add JSDoc comments to raw transfer constants (#20286) (overlookmotel) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Summary
ObjectExpressionhandling inObjectPropertyKind::SpreadProperty'smay_have_side_effects— spreading an object literal is side-effect-free when all its properties areremove_unused_object_exprto check side effects for all-spread objects instead of unconditionally bailingCloses rolldown/rolldown#8582