-
-
Notifications
You must be signed in to change notification settings - Fork 634
feat(ecmascript): handle typeof
guarded global access as side effect free
#12981
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
feat(ecmascript): handle typeof
guarded global access as side effect free
#12981
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
is_side_effect_free_unbound_identifier_ref
check from @rolldown/rolldown/files/crates/rolldown/src/ast_scanner/side_effect_detector/utils.rs and @rolldown/rolldown/files/crates/rolldown/src/ast_scanner/side_effect_detector/mod.rs to @oxc-project...is_side_effect_free_unbound_identifier_ref
from Rolldown to Oxc
crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Outdated
Show resolved
Hide resolved
is_side_effect_free_unbound_identifier_ref
from Rolldown to Oxc
Added comprehensive tests for the typeof guard patterns in may_have_side_effects.rs. The tests cover all the enhanced side effect analysis patterns including logical AND/OR patterns, conditional expressions, and negative cases to ensure the optimization works correctly. Tests committed in 3b385d6. |
CodSpeed Instrumentation Performance ReportMerging #12981 will not alter performanceComparing Summary
|
crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Outdated
Show resolved
Hide resolved
crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Outdated
Show resolved
Hide resolved
Co-authored-by: sapphi-red <[email protected]>
…tations Instead of exposing is_side_effect_free_unbound_identifier_ref as a public function, integrate the logic directly into LogicalExpression and ConditionalExpression implementations as suggested in the review feedback. Co-authored-by: sapphi-red <[email protected]>
…ysis Co-authored-by: sapphi-red <[email protected]>
…, add missing tests Co-authored-by: sapphi-red <[email protected]>
2b96c5c
to
f3cea22
Compare
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
typeof
guarded global access as side effect free
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.
Pull Request Overview
This PR enhances Oxc's side effect analysis to recognize typeof
guard patterns that make accessing potentially undefined global variables safe. The implementation integrates sophisticated guard pattern detection directly into the MayHaveSideEffects
trait implementations.
Key changes:
- Enhanced
LogicalExpression
andConditionalExpression
to analyzetypeof
guard patterns - Added comprehensive helper function for detecting safe unbound identifier access patterns
- Extensive test coverage for various guard patterns including equality checks and string comparisons
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs | Implements core guard pattern analysis logic in trait implementations and adds helper function |
crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs | Adds comprehensive test cases covering all supported guard patterns and edge cases |
This PR integrates sophisticated side effect analysis for unbound identifier references directly into Oxc's MayHaveSideEffects trait implementations, rather than exposing it as a separate public function.
What this implements
The enhanced analysis recognizes guard patterns that make accessing potentially undefined global variables safe:
Implementation approach
Following code review feedback, the logic is integrated directly into the trait implementations:
Testing
Comprehensive tests have been added to
crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs
covering:typeof x !== 'undefined' && x
typeof x === 'undefined' || x
typeof x === 'undefined' ? fallback : x
typeof x < 'u' && x
Use cases
This enhancement is particularly valuable for:
The implementation maintains full compatibility with Oxc's existing side effects analysis framework while providing enhanced capabilities for modern JavaScript bundling and optimization tools.
Ported from: https://github.com/evanw/esbuild/blob/d34e79e2a998c21bb71d57b92b0017ca11756912/internal/js_ast/js_ast_helpers.go#L2594-L2639
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.