Files
oxc-project__oxc/packages/codegen/src-js/print/precedence.ts
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

30 lines
1.4 KiB
TypeScript

// Operator precedence levels, on the scale `oxc_syntax::precedence::Precedence` uses.
//
// A printer is told the precedence of the position it is printing into, and parenthesizes itself
// when its own precedence is lower. The numbering is Oxc's, so the two agree on what needs parens.
/** No precedence at all - nothing parenthesizes itself against this. */
export const PREC_LOWEST = 0;
/** The comma operator, as in a sequence expression. */
export const PREC_COMMA = 1;
/** `yield` and `yield*`. */
export const PREC_YIELD = 3;
/** Assignment, including the compound and logical forms, and arrow functions. */
export const PREC_ASSIGN = 4;
/** The `?:` conditional operator. */
export const PREC_CONDITIONAL = 5;
/** The equality operators, `==` through `!==`. */
export const PREC_EQUALS = 12;
/** The relational operators, including `in` and `instanceof`. */
export const PREC_COMPARE = 13;
/** `**`, the one right-associative binary operator. */
export const PREC_EXPONENTIATION = 17;
/** The prefix unary and update operators, `await`, and `!`. */
export const PREC_PREFIX = 18;
/** The postfix update operators, and the position a member expression's object prints at. */
export const PREC_POSTFIX = 19;
/** `new` without an argument list, which binds tighter than a call. */
export const PREC_NEW = 20;
/** Calls, member access and tagged templates - the tightest binding there is. */
export const PREC_CALL = 21;