Files
Samuel Attard 9ef028c2ad feat(codegen): add ascii_only option (#25994)
Closes #17068. Supersedes #18095 / #18118.

Adds `CodegenOptions::ascii_only` (and `codegen.asciiOnly` in
`oxc_minify_napi`): escape every non-ASCII character in string literals,
untagged template literals, regexp literals, directives and identifier
names so the output is 7-bit clean — like esbuild `--charset=ascii` /
terser `format.ascii_only`. Off by default; the default path is
untouched (codegen bench and conformance unchanged).

```js
// Input
let café = "naïve 😀";
let re = /[–—]/u;

// Output with ascii_only: true
let caf\u00E9 = "na\u00EFve \u{1F600}";
let re = /[\u2013\u2014]/u;
```

Characters above U+FFFF use ES2015 code-point escapes. Regex patterns
use escaped surrogate pairs instead; RegExp.prototype.source reflects
the escaped spelling.
Tagged template quasis, JSX names/text/attribute strings, hashbangs and
preserved comments may still contain non-ASCII characters. JavaScript
expressions inside tagged templates and JSX are escaped normally.

Performance; with the option disabled, codegen codspeed benchmarks are
withing noise (<1%). With the option on, performance is only affected
for files that contain non ascii characters.

---

Our use case is a large Vite/Rolldown app served through proxies that
mangle non-ASCII JS (previously handled by terser's `ascii_only`);
#17068 also lists deno_core snapshots and Chrome extension content
scripts.

AI-assisted (Claude Code); I reviewed the code, wrote the tests, and
have been running this in production builds via a vendored
`oxc_codegen`.

---------

Co-authored-by: Samuel Attard <6634592+MarshallOfSound@users.noreply.github.com>
Co-authored-by: Cameron Clark <cameron.clark@hey.com>
Co-authored-by: Dunqing <dengqing0821@gmail.com>
2026-09-10 17:10:02 +01: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.