mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
32d00c55bc
Preserve parentheses around TypeScript instantiation expressions in postfix contexts. Previously, `(f<T>).x` and `(f<T>)[x]` were emitted as `f<T>.x` and `f<T>[x]`, producing invalid syntax or changing how the expression is parsed. The printer now respects the surrounding precedence while retaining grouping for lower-precedence operands.
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
- AST Traversal: Walk through AST nodes systematically
- Token Generation: Convert nodes to appropriate tokens/strings
- Formatting: Apply whitespace, indentation, and style rules
- 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.