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

198 lines
6.1 KiB
TypeScript

// Binding patterns.
import { typeAssertIs } from "../asserts.ts";
import {
CAT_IDENT,
CAT_OTHER,
CAT_QUESTION,
write,
writeWithMap,
writeWithMapNoLast,
} from "./write.ts";
import { printExpression } from "./expression.ts";
import { printSpaceBeforeIdentifier } from "./space.ts";
import { printLiteral } from "./literal.ts";
import { CTX_NONE } from "./operators.ts";
import { PREC_COMMA } from "./precedence.ts";
import { printString } from "./string.ts";
import { printTypeAnnotation } from "./typescript.ts";
import type { State } from "../state.ts";
import type { LiteralExtras, UnknownNode } from "./types.ts";
import type * as ESTree from "../../../../npm/oxc-types/types.d.ts";
/**
* A binding pattern, or one of the nodes which can stand in for one.
*
* `RestElement` appears in object and array patterns.
*/
type BindingPatternNode =
| ESTree.BindingPattern
| ESTree.BindingRestElement
| ESTree.FormalParameterRest;
/**
* Print anything which can be bound to - a name, a destructuring pattern, or one carrying a default.
*/
export function printBindingPattern(node: BindingPatternNode | UnknownNode, state: State): void {
switch (node.type) {
case "Identifier":
printSpaceBeforeIdentifier(state);
writeWithMap(state, node.name, CAT_IDENT, node);
if (TS && node.optional) write(state, "?", CAT_QUESTION);
if (TS && node.typeAnnotation != null) printTypeAnnotation(node.typeAnnotation, state);
break;
case "ObjectPattern":
printObjectBindingPattern(node, state);
if (TS && node.optional) write(state, "?", CAT_QUESTION);
if (TS && node.typeAnnotation != null) printTypeAnnotation(node.typeAnnotation, state);
break;
case "ArrayPattern":
printArrayBindingPattern(node, state);
if (TS && node.optional) write(state, "?", CAT_QUESTION);
if (TS && node.typeAnnotation != null) printTypeAnnotation(node.typeAnnotation, state);
break;
case "AssignmentPattern":
printBindingPattern(node.left, state);
write(state, " = ", CAT_OTHER);
printExpression(node.right, state, PREC_COMMA, CTX_NONE);
break;
case "RestElement":
writeWithMap(state, "...", CAT_OTHER, node);
printBindingPattern(node.argument, state);
if (TS && node.typeAnnotation != null) printTypeAnnotation(node.typeAnnotation, state);
break;
default:
throw new Error(`Unknown binding pattern type: ${node.type}`);
}
}
/**
* Print an object destructuring pattern, as in `const { a, b } = obj`.
*/
function printObjectBindingPattern(node: ESTree.ObjectPattern, state: State): void {
const { properties } = node;
const { length } = properties;
if (length === 0) {
writeWithMap(state, "{}", CAT_OTHER, node);
return;
}
writeWithMap(state, "{ ", CAT_OTHER, node);
for (let i = 0; i < length; i++) {
if (i > 0) write(state, ", ", CAT_OTHER);
const property = properties[i];
if (property.type === "RestElement") {
write(state, "...", CAT_OTHER);
printBindingPattern(property.argument, state);
} else {
printBindingProperty(property, state);
}
}
write(state, " }", CAT_OTHER);
}
/**
* Print one property of an object pattern, in shorthand where the key and the binding agree.
*/
function printBindingProperty(node: ESTree.BindingProperty, state: State): void {
// Shorthand is re-derived from names (matching Oxc), not from the flag.
const { key, value } = node;
let shorthand = false;
if (!node.computed && key.type === "Identifier") {
if (value.type === "Identifier" && key.name === value.name) {
shorthand = true;
} else if (
value.type === "AssignmentPattern" &&
value.left.type === "Identifier" &&
key.name === value.left.name
) {
shorthand = true;
}
}
if (!shorthand) {
if (node.computed) {
write(state, "[", CAT_OTHER);
typeAssertIs<ESTree.Expression>(key);
printExpression(key, state, PREC_COMMA, CTX_NONE);
write(state, "]", CAT_OTHER);
} else {
printPropertyKey(key, state);
}
write(state, ": ", CAT_OTHER);
}
printBindingPattern(value, state);
}
/**
* Print a property key which is not computed - a name, a private name, a string or a number.
*
* The caller decides between this and printing the key as an expression in brackets,
* because whether a key must be computed depends on what it is, not on how the source wrote it.
*/
export function printPropertyKey(key: ESTree.PropertyKey, state: State): void {
switch (key.type) {
case "Identifier":
printSpaceBeforeIdentifier(state);
writeWithMap(state, key.name, CAT_IDENT, key);
break;
case "PrivateIdentifier":
writeWithMapNoLast(state, "#", key);
write(state, key.name, CAT_IDENT);
break;
case "Literal":
typeAssertIs<LiteralExtras>(key);
if (typeof key.value === "string") {
printString(state, key.value, key);
} else {
printLiteral(key, state, PREC_COMMA, CTX_NONE);
}
break;
default:
printExpression(key, state, PREC_COMMA, CTX_NONE);
}
}
/**
* Print an array destructuring pattern, holes and rest element included.
*/
function printArrayBindingPattern(node: ESTree.ArrayPattern, state: State): void {
// Oxc stores the rest element separately from the other elements.
// A trailing comma is printed before it, and a space always precedes it.
const { elements } = node;
let { length } = elements;
let rest: ESTree.BindingRestElement | ESTree.AssignmentTargetRest | null = null;
if (length > 0) {
const lastElement = elements[length - 1];
if (lastElement != null && lastElement.type === "RestElement") {
rest = lastElement;
length--;
}
}
writeWithMap(state, "[", CAT_OTHER, node);
for (let i = 0; i < length; i++) {
if (i !== 0) write(state, ", ", CAT_OTHER);
const element = elements[i];
if (element != null) printBindingPattern(element, state);
if (i === length - 1 && (element == null || rest !== null)) write(state, ",", CAT_OTHER);
}
if (rest !== null) {
write(state, " ", CAT_OTHER);
printBindingPattern(rest, state);
}
write(state, "]", CAT_OTHER);
}