mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
cf4b7d7402
JavaScript strings can contain lone UTF-16 surrogates. Keep their full values through parsing, semantic analysis, transforms, linting, and output instead of losing names at fallible UTF-8 views. Build on the separate StaticName API preparation. Preserve canonical WTF-8 through syntax consumers and convert explicitly at UTF-8 output, identifier, pattern, and resolver boundaries. Keep enum constants in owned UTF-16 storage without adding a general owned JSString. Retain JSX character-reference output, generated CI files, and the consumer regressions. The prerequisite isolates the property-name API review and preserves allocation counts when formatting UTF-8 names. Part of #26242. Implemented with AI assistance.
Oxc Transform
See usage instructions.
TypeScript and React JSX Transform
import assert from "node:assert";
import { transformSync } from "oxc-transform";
const { code, declaration, errors } = transformSync("test.ts", "class A<T> {}", {
typescript: {
declaration: true, // With isolated declarations in a single step.
},
});
// or `await transform(filename, code, options)`
assert.equal(code, "class A {}\n");
assert.equal(declaration, "declare class A<T> {}\n");
assert(errors.length == 0);
Isolated Declarations for Standalone DTS Emit
Conforms to TypeScript compiler's --isolatedDeclarations .d.ts emit.
Usage
import assert from "node:assert";
import { isolatedDeclarationSync } from "oxc-transform";
const { map, code, errors } = isolatedDeclarationSync("test.ts", "class A {}");
// or `await isolatedDeclaration(filename, code, options)`
assert.equal(code, "declare class A {}\n");
assert(errors.length == 0);
API
Transform Functions
// Synchronous transform
transformSync(
filename: string,
sourceText: string,
options?: TransformOptions,
): TransformResult
// Asynchronous transform
transform(
filename: string,
sourceText: string,
options?: TransformOptions,
): Promise<TransformResult>
Isolated Declaration Functions
// Synchronous isolated declaration
isolatedDeclarationSync(
filename: string,
sourceText: string,
options?: IsolatedDeclarationsOptions,
): IsolatedDeclarationsResult
// Asynchronous isolated declaration
isolatedDeclaration(
filename: string,
sourceText: string,
options?: IsolatedDeclarationsOptions,
): Promise<IsolatedDeclarationsResult>
Use the Sync versions for synchronous operations. Use async versions for asynchronous operations, which can be beneficial in I/O-bound or concurrent scenarios, though they add async overhead.
See index.d.ts for complete type definitions.
Supports WASM
See https://stackblitz.com/edit/oxc-transform for usage example.