Files
oxc/packages/codegen
oxc-guard[bot]andDunqing 75efdca474 release(crates): oxc v0.156.0 (#28190)
### 💥 BREAKING CHANGES

- cf8296f ecmascript: [**BREAKING**] Fold surrogate strings with JSStr
constants (#27098) (Dunqing)

### 🚀 Features

- dd6cb2b minifier: Fold out-of-bounds string indices to `void 0`
(#28004) (Dunqing)

### 🐛 Bug Fixes

- f4dac9e semantic: Separate hoisted vars from block type bindings
(#27936) (limadog9)
- 9ec6617 minifier: Keep brackets on negative and non-finite numeric
keys (#27855) (Yunus Ozen)
- 520a26e minifier: Keep the default in shorthand assignment targets
(#28183) (waltu)
- def47b8 react-compiler: Keep manual memoization as written when its
dependencies can't be honoured (#28101) (Dunqing)
- 704d8cf transformer_plugins: Keep the bare inject when references to
it remain (#26965) (Bharadwaj Pendyala)
- 6d7b372 minifier: Handle sequence exprs when folding if to expr
statement (#27939) (Armano)
- e3eca41 runtime: Align async iterator throw with Babel (#28059)
(camc314)
- af0258d parser: Parse source imports named type (#28062) (camc314)
- 83633a7 parser: Parse escaped class field names after initializers
(#28061) (camc314)
- 90c58ac parser: Parse postfix updates of sloppy `let` identifiers
(#28060) (camc314)
- 0e2934f codegen: Preserve default import names in source maps (#28058)
(camc314)
- 09bdb80 minifier: Allow argument rewrite with with leading var
assignments (#28056) (Armano)
- ebcdcd9 minifier: Preserve rest destructuring side effects (#27981)
(camc314)
- 5c84098 semantic,linter: Report names with lone surrogates in enum and
lint checks (#27991) (Dunqing)
- bd70a9b ecmascript: Use ECMAScript whitespace when folding
`parseFloat`/`parseInt`/`trim` (#27719) (Dunqing)
- 4c0fa90 minifier,linter: Keep `delete` of constant members and later
JSX keys (#27990) (Dunqing)

### ⚡ Performance

- 3493ce3 minifier: Reuse is_error_constructor and simplify
substitute_global_new_expression (#28149) (Armano)
- a22c9f2 parser: Move annotation comment handling out of hot paths
(#28139) (camc314)
- 1ed93af data_structures: Copy short strings without a memcpy call
(#27953) (leaysgur)

### 🛡️ Security

- b93e169 deps: Bump .node-version to 26.12.1 (#28090) (leaysgur)

Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com>
2026-10-27 11:19:24 +08:00
..

oxc-codegen

Fast, synchronous code generation for JavaScript and TypeScript ASTs.

oxc-codegen turns an ESTree or TS-ESTree AST into formatted source code. It supports JavaScript, JSX, TypeScript, and TSX.

The printer is a port of Oxc's Rust oxc_codegen crate. With the default options, both printers produce byte-identical output: tab indentation, double-quoted strings, and no comments.

Installation

npm install oxc-codegen

oxc-codegen is ESM-only and requires Node.js ^20.19.0 or >=22.12.0.

Quick start

Pair it with oxc-parser to parse and print source code:

import { printSync } from "oxc-codegen";
import { parseSync } from "oxc-parser";

const { program } = parseSync("input.js", "const answer=6*7");
const { code } = printSync(program);

console.log(code);
// const answer = 6 * 7;

You can also print a manually constructed AST:

const program = {
  type: "Program",
  sourceType: "script",
  body: [
    {
      type: "ExpressionStatement",
      expression: {
        type: "CallExpression",
        callee: {
          type: "MemberExpression",
          object: { type: "Identifier", name: "console" },
          property: { type: "Identifier", name: "log" },
          computed: false,
          optional: false,
        },
        arguments: [{ type: "Literal", value: "Hello!" }],
        optional: false,
      },
    },
  ],
};

console.log(printSync(program).code);
// console.log("Hello!");

TypeScript and TSX

Set ts when the AST can contain TypeScript nodes. For TSX, set both ts and jsx:

const { program } = parseSync("component.tsx", "const Box = <T,>(value: T) => <div>{value}</div>");

const { code } = printSync(program, {
  ts: true,
  jsx: true,
});

API

printSync(node, options?)

function printSync(
  node: ESTree.Program | ESTree.Statement,
  options?: Options,
): {
  code: string;
  map: SourceMap | null;
};

Prints a complete Program or a single statement and returns the generated source code, and (when requested) a standard Source Map v3 object.

import { printSync } from "oxc-codegen";
import { parseSync } from "oxc-parser";

const sourceText = "const answer=6*7";
const { program } = parseSync("input.js", sourceText);
const { code, map } = printSync(program, {
  sourcemap: true,
  sourceFilename: "input.js",
  sourceText,
});

Source-map mappings require sourceText and nodes with valid Oxc start / end offsets. A manually constructed AST without offsets can still be printed, but its source map has an empty mappings string.

Options

Option Type Default Description
indent string "\t" Non-empty string of spaces and/or tabs used for one indent level
startingIndentLevel number 0 Starting indent level, from 0 to 1000
jsx boolean false Enable TSX-safe printing for ambiguous TypeScript syntax
ts boolean false Select the printer that supports TypeScript nodes
sourcemap boolean false Return a Source Map v3 object in map
sourceFilename string "" Original source filename recorded in the source map
sourceText string - Original text required for source-map mappings and content

Why pure JavaScript?

Most Oxc packages use native bindings. This package deliberately does not: when an AST already lives in JavaScript, passing the entire object graph across a JS/native boundary can cost more than printing it in place. oxc-codegen avoids that serialization and uses specialized printer builds for JavaScript and TypeScript workloads.

See DESIGN.md for the implementation details and performance constraints.

Current limitations

  • Comments are not printed.
  • Minified output is not supported.

Benchmarks

Representative time per printSync call:

Fixture Bytes Time
tiny.js 26 0.0001 ms
RadixUIAdoptionSection.jsx 2,518 0.0070 ms
react.development.js 72,141 0.1518 ms
binder.ts 193,077 0.3364 ms
App.tsx 415,340 1.2912 ms
lodash.js 544,096 0.7977 ms
kitchen-sink.tsx 732,222 4.2924 ms
antd.js 6,683,633 16.9652 ms

These figures come from one machine and are illustrative, not a regression baseline. Results vary between runs, most noticeably for large fixtures such as antd.js.