Commit bff710a
feat(es/compiler): Merge
This PR merges the `optional_chaining` visitor from `swc_ecma_compat_es2020` into `swc_ecma_compiler::Compiler` to reduce visitor overhead and improve performance by consolidating ECMAScript compatibility transformations.
## Changes
### 1. Created ES2020 Module
- Created `crates/swc_ecma_compiler/src/es2020/optional_chaining.rs` with transformation logic
- Updated `crates/swc_ecma_compiler/src/es2020/mod.rs` to include the new module
### 2. Integrated into CompilerImpl
Added transformation state to `CompilerImpl`:
- `es2020_optional_chaining_vars: Vec<VarDeclarator>` - stores generated variable declarators
- `es2020_optional_chaining_unresolved_ctxt: SyntaxContext` - tracks unresolved context
Implemented visitor methods:
- `transform_optional_chaining()` - transforms `?.` and `delete ?.` operators
- `visit_mut_expr()` - calls transformation before other transformations
- `visit_mut_block_stmt_or_expr()` - converts expressions to block statements when needed
- `visit_mut_pat()` - handles optional chaining in assignment patterns
- `visit_mut_module_items()` / `visit_mut_stmts()` - hoist generated variables
### 3. Updated swc_ecma_compat_es2020
- Modified `lib.rs` to use `Compiler` with `Features::OPTIONAL_CHAINING`
- Simplified `optional_chaining.rs` to a thin wrapper around `Compiler`
- Maintained backward compatibility and existing `Config` API
- Maps `pure_getter` config to `assumptions.pure_getters`
## Testing
All existing tests pass:
- ✅ `cargo test -p swc_ecma_compat_es2020`
- ✅ `cargo test -p swc_ecma_compiler`
## Performance Impact
This change is part of a larger effort to reduce visitor overhead by consolidating transformations into a single Compiler implementation, minimizing the number of AST traversals.
## Related PRs
Follows the same pattern as:
- #11157: Merged nullish_coalescing
- #10914: Merged logical_assignments
- #10909: Merged private_in_object and static_blocks
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>optional_chaining into Compiler
1 parent dd6f71b commit bff710a
File tree
5 files changed
+540
-24
lines changed- crates
- swc_ecma_compat_es2020/src
- swc_ecma_compiler/src
- es2020
5 files changed
+540
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
0 commit comments