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

164 lines
5.3 KiB
TypeScript

// Assignment targets.
import { typeAssertIs } from "../asserts.ts";
import { printPropertyKey } from "./binding_pattern.ts";
import { CAT_IDENT, CAT_OTHER, write, writeWithMap } from "./write.ts";
import { printExpression, printMemberExpression } from "./expression.ts";
import { printSpaceBeforeIdentifier } from "./space.ts";
import { CTX_NONE } from "./operators.ts";
import { PREC_COMMA, PREC_COMPARE, PREC_PREFIX } from "./precedence.ts";
import type { State } from "../state.ts";
import type * as ESTree from "../../../../npm/oxc-types/types.d.ts";
/**
* Print the left hand side of an assignment.
*
* Destructuring targets look like object and array literals but are a separate set of node types,
* with their own rules - which is why they are printed here rather than by the expression printer.
*/
export function printAssignmentTarget(node: ESTree.AssignmentTarget, state: State): void {
switch (node.type) {
// A simple target is only ever a name, a member access, or (in TS) one of the assertion expressions below.
// For speed, the two common ones are printed here instead of through the expression printer's own dispatch.
case "Identifier":
printSpaceBeforeIdentifier(state);
writeWithMap(state, node.name, CAT_IDENT, node);
break;
case "MemberExpression":
printMemberExpression(node, state, CTX_NONE);
break;
case "ObjectPattern":
printObjectAssignmentTarget(node, state);
break;
case "ArrayPattern":
printArrayAssignmentTarget(node, state);
break;
/* IF TS */
case "TSAsExpression":
case "TSSatisfiesExpression":
// `(x as T) = y` - the target must be parenthesized
printExpression(node, state, PREC_COMPARE, CTX_NONE);
break;
case "TSTypeAssertion":
printExpression(node, state, PREC_PREFIX, CTX_NONE);
break;
/* END_IF */
default:
printExpression(node, state, PREC_COMMA, CTX_NONE);
}
}
/**
* Print an object destructuring target, as in `({ a, b: c } = obj)`.
*/
function printObjectAssignmentTarget(node: ESTree.ObjectAssignmentTarget, state: State): void {
writeWithMap(state, "{", CAT_OTHER, node);
const { properties } = node;
const { length } = properties;
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);
printAssignmentTarget(property.argument, state);
} else {
printAssignmentTargetProperty(property, state);
}
}
write(state, "}", CAT_OTHER);
}
/**
* Print one property of an object destructuring target, in shorthand where the AST says so.
*/
function printAssignmentTargetProperty(node: ESTree.AssignmentTargetProperty, state: State): void {
// Shorthand from the AST flag (assignment targets are not re-derived).
if (node.shorthand) {
// `{ a }` or `{ a = 1 }`
const { value } = node;
if (value.type === "AssignmentPattern") {
typeAssertIs<ESTree.IdentifierReference>(value.left);
printSpaceBeforeIdentifier(state);
writeWithMap(state, value.left.name, CAT_IDENT, value.left);
write(state, " = ", CAT_OTHER);
printExpression(value.right, state, PREC_COMMA, CTX_NONE);
} else {
printSpaceBeforeIdentifier(state);
writeWithMap(state, value.name, CAT_IDENT, value);
}
} else {
const { key } = node;
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);
printAssignmentTargetMaybeDefault(node.value, state);
}
}
/**
* Print a destructuring target which may carry a default, as the `a = 1` in `({ a = 1 } = obj)`.
*/
function printAssignmentTargetMaybeDefault(
node: ESTree.AssignmentTargetMaybeDefault,
state: State,
): void {
if (node.type === "AssignmentPattern") {
printAssignmentTarget(node.left, state);
write(state, " = ", CAT_OTHER);
printExpression(node.right, state, PREC_COMMA, CTX_NONE);
} else {
printAssignmentTarget(node, state);
}
}
/**
* Print an array destructuring target, holes and rest element included.
*/
function printArrayAssignmentTarget(node: ESTree.ArrayAssignmentTarget, state: State): void {
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) {
typeAssertIs<ESTree.AssignmentTargetMaybeDefault>(element);
printAssignmentTargetMaybeDefault(element, state);
}
if (i === length - 1 && (element == null || rest !== null)) {
write(state, ",", CAT_OTHER);
}
}
if (rest !== null) {
if (length > 0) write(state, " ", CAT_OTHER);
write(state, "...", CAT_OTHER);
printAssignmentTarget(rest.argument, state);
}
write(state, "]", CAT_OTHER);
}