Files
overlookmotel a4478e960a feat(codegen): add oxc-codegen package (#25488)
Add `oxc-codegen` package - a printer written in TS.

### Features

- Prints JS, JSX, TS, TSX.
- Matches `oxc_codegen` exactly on all Test262, TypeScript, Acorn-JSX fixtures.
- Very fast.

### Deficiencies

- Pretty-printing only (no compact/minified output).
- No support for printing comments.
- Source map support is semi-there, but largely untested, and poor API (design doc has some ideas on how to improve it).

### This PR adds

- The printer code.
- Conformance testing infra.
- Further tests for edge cases.
- README.
- Design doc - how it differs from `oxc_codegen` and why, what produces the perf.

### This PR doesn't add

- Not integrated into CI or release pipeline.

### Notes

This started as a slop port of `oxc_codegen`. I've iterated on that base, cleaned it up a lot, and at least skimmed through all of it. I think it's in fairly decent shape.

The design doc is I think worth a read. It explains the rationale for the choices, in particular the "1 codebase, 4 builds" approach.

I've put it in a new top-level directory called `packages`. It's not an app, so didn't seem to me to fit alongside `apps/oxlint` and `apps/oxfmt`. It's pure JS, so not like e.g. `napi/parser`. And it felt too heavyweight to put into `npm` dir. But maybe there's a better place for it.

Co-authored-by: Cameron <cameron.clark@hey.com>
2026-08-12 13:41:31 +00:00

48 lines
1.6 KiB
TypeScript

// Assertions.
//
// Both of these are compiled away - `tsdown_plugins/remove_asserts.ts` removes every call to them,
// and the expressions inside it, so neither costs a release build anything at all.
/**
* Assert a value is of a certain type.
*
* Has no runtime effect - only for guiding the type-checker.
* Minification removes this function and all calls to it, so it has zero runtime cost.
*
* @param value - Value
*/
// oxlint-disable-next-line no-unused-vars
export function typeAssertIs<T>(value: unknown): asserts value is T {}
/**
* Assert a condition.
*
* In release builds, is a no-op. Only does runtime checks in debug builds.
* Minification removes this function and all calls to it in release builds, so it has zero runtime cost.
*
* Use this for testing conditions which would indicate a bug in the code.
* Do NOT use this for validating user input.
*
* If creating the error message is expensive, or potentially creating the message itself can result in an error
* when the assertion passes, pass a function which returns the message.
*
* ```ts
* debugAssert(condition, () => `Condition failed: ${getErrorMessage()}`);
* ```
*
* @param condition - Condition which is expected to be `true`
* @param message - Message to include in error if condition is `false`,
* or a function which returns the message to include in error if condition is `false` (optional).
*/
export function debugAssert(
condition: boolean,
message?: string | (() => string),
): asserts condition {
if (!DEBUG) return;
if (!condition) {
if (typeof message === "function") message = message();
throw new Error(message ?? "Assertion failed");
}
}