Files
Donny/강동윤andGitHub 8007df067f feat: emit ECMA-426 source map scopes behind experimental flag (#11582)
## Summary

Implements ECMA-426 source map scope emission behind a new opt-in flag:

- Adds `jsc.experimental.emitSourceMapScopes` (default `false`)
- Emits `scopes` metadata in output source maps when enabled
- Includes both original scope trees and generated range trees
- Includes variable lists and generated range bindings (`G`)
- Keeps behavior unchanged unless explicitly enabled

## What changed

### Config and API wiring

- Added `emit_source_map_scopes` to SWC config/build pipeline
- Added `emit_source_map_scopes` to compiler-base `PrintArgs`
- Added `emitSourceMapScopes?: boolean` to `packages/types`

### Codegen scope collection

- Extended `WriteJs` with scope hooks:
  - `start_scope`
  - `end_scope`
  - `add_scope_variable`
- Added forwarding in wrappers (`Box<W>`, `&mut W`, omit-trailing-semi
writer)
- Added scope model (`ScopeKind`, `BindingStorage`, `ScopeRecord`,
`ScopeBindingRecord`)
- Implemented collection in `JsWriter` with:
  - nested scope stack tracking
  - hoisted vs lexical placement
  - per-scope variable dedup by name

### Emitter instrumentation

- Instrumented module/script roots, function-like scopes, block scopes,
and catch scopes
- Collected bindings for params, var/let/const declarations,
function/class declarations
- Added helper module for pattern/parameter binding extraction

### Source map integration

- Added `SourceMapGenConfig::for_each_additional_name` hook
- Pre-registers additional scope-related names before mappings are
encoded
- Added scope encoder in compiler-base (`source_map_scopes.rs`) to
encode ECMA-426 tags (`B/C/D/E/F/G`)
- `scopes` is only emitted when:
  - source maps are enabled
  - `emitSourceMapScopes` is enabled
  - no input sourcemap composition (`orig.is_none()`)

## Tests

Added/updated tests for:

- codegen writer scope collection and wrapper forwarding
- compiler-base scope encoder behavior
- compiler-base print integration (enabled/disabled/orig composition)
- swc e2e source-map test for this issue
(`issue_11424_emit_source_map_scopes_opt_in`)

## Verification run

- `git submodule update --init --recursive`
- `cargo fmt --all`
- `cargo clippy --all --all-targets -- -D warnings`
- `cargo test -p swc_ecma_codegen`
- `cargo test -p swc_compiler_base`
- `cargo test -p swc_sourcemap`
- `cargo test -p swc`

Note: `cargo test -p swc` still has pre-existing environment-dependent
failures in `tests/source_map.rs` where Node cannot resolve
`sourcemap-validator` in this environment. The new
`issue_11424_emit_source_map_scopes_opt_in` test passes.

Fixes #11424
2026-02-25 12:25:42 -08:00
..