Files
oxc/crates/oxc_codegen
camc314 3fd78d7451 fix(codegen): preserve names in duplicate source mappings (#26051)
When an unnamed parent node and a mangled identifier begin at the same source position, codegen deduplicated the identifier mapping. This retained location data but discarded the original identifier name, leaving consumers unable to restore inferred function names in source-mapped Node stack traces.

```mermaid
flowchart LR
    A[AssignmentExpression mapping<br/>unnamed] --> B[Token at source position P<br/>name: none]
    C[Identifier mapping<br/>name: longName] --> D{Same source position P?}
    D -->|before| E[Drop duplicate<br/>names: empty]
    D -->|after| F[Enrich existing token<br/>name: longName]
```

### Changes

- Cache whether the last emitted token has a name, keeping duplicate-position handling cheap in the source-map builder hot path.
- Enrich an unnamed duplicate token when a later identifier mapping supplies the original name.
- Add a unit regression test for the builder behavior.
- Extend the existing Node source-map stack-trace snapshot test with a mangled ES module fixture.

The Node fixture verifies:

```text
function longFunctionName(longName) { ... }
            ↓ mangled
function e(e) { ... }
```

and asserts that the source map includes both original names while Node restores the inferred inner frame as:

```text
at longName (/project/input.js:6:15)
```
2026-11-03 02:22:04 +00:00
..
…

Oxc Codegen

High-performance code generation from AST back to JavaScript/TypeScript source code.

Overview

This crate converts AST nodes back into source code strings, supporting JavaScript, TypeScript, and JSX. It's designed for speed and correctness, producing clean, readable output.

Key Features

  • Fast code generation: Optimized for performance with minimal allocations
  • Source map support: Generate accurate source maps during output
  • Configurable formatting: Control whitespace, semicolons, and other formatting options
  • Comment preservation: Maintain comments during code generation
  • Binary expression optimization: Intelligent parentheses insertion

Architecture

Code Generation Pipeline

  1. AST Traversal: Walk through AST nodes systematically
  2. Token Generation: Convert nodes to appropriate tokens/strings
  3. Formatting: Apply whitespace, indentation, and style rules
  4. Source Mapping: Track original source positions if enabled

Design Principles

  • Correctness: Generated code must be functionally equivalent to original
  • Performance: Minimize string allocations and copying
  • Readability: Produce clean, well-formatted output
  • Fidelity: Preserve semantic meaning and behavior

The codegen is adapted from esbuild's approach, optimized for Rust and oxc's AST structure.