You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(semantic): add scope tracking for with statements (#14652)
Fixes#8365
## Summary
The `with` statement was not creating a scope in the semantic analysis, causing `is_global_reference` to incorrectly return `true` for references inside `with` blocks. This could lead to incorrect minification.
## Problem
According to the ECMAScript specification, `with` statements should create an Object Environment Record that extends the lexical environment. Without proper scope tracking:
- `is_global_reference()` incorrectly returns `true` for identifiers inside `with` blocks
- This can cause minification to incorrectly treat local references as global
- The scope tree doesn't accurately represent the program structure
## Solution
### Changes Made
1. **AST Definition** (`crates/oxc_ast/src/ast/js.rs`):
- Added `#[scope]` attribute to `WithStatement` struct
- Added `scope_id: Cell<Option<ScopeId>>` field
2. **Semantic Builder** (`crates/oxc_semantic/src/builder.rs`):
- Modified `visit_with_statement` to call `enter_scope()` before visiting the body
- Added `leave_scope()` call after visiting the body
- Uses `ScopeFlags::empty()` similar to block statements
3. **Test Coverage** (`crates/oxc_semantic/tests/integration/scopes.rs`):
- Added `test_with_statement` to verify scope creation
- Tests both simple expressions and block statement bodies
- Verifies proper scope tree structure
### Example
```javascript
const foo = {
Object: class {
constructor() { console.log('Constructor') }
}
}
with (foo) {
console.log(new Object()) // should print "Constructor"
}
```
Before this fix, `Object` inside the `with` block would be incorrectly identified as a global reference, potentially causing minification errors.
## Implementation Notes
While this implementation doesn't fully model the complex object environment semantics (where property lookups can dynamically affect identifier resolution at runtime), it ensures:
- References inside `with` blocks are no longer incorrectly marked as global
- The scope tree properly represents the lexical structure
- Provides a foundation for future improvements to object environment handling
## Testing
All existing tests pass, plus the new test verifies:
- `with` statements create scopes
- Scopes are properly nested in the scope tree
- Works correctly with both simple and block statement bodies
0 commit comments