
# API

```ts
import { Analyzer, BindingFlags, analyze, generate, parse } from "@tsrx/yuku";
```

| Export | Signature |
| --- | --- |
| `parse` | `parse(source: string, options?: ParseOptions): ParseResult` |
| `analyze` | `analyze(source: string, options?: AnalyzeOptions): Module` |
| `Analyzer` | `new Analyzer(options?: AnalyzerOptions)`, with `setFile`, `deleteFile`, `module`, `modules`, `diagnostics`, and `link` |
| `generate` | `generate(node: Node, options?: GenerateOptions): GenerateResult` |
| `BindingFlags` | The flags of a `Binding`, for `binding.has(BindingFlags.Const)` |

The options are Yuku's, without `lang` and `wasm`, and `parse` defaults `preserveParens` to `false`. The guides cover each: [Parse](/guide/parse), [Analyze](/guide/analyze), and [Generate](/guide/generate). `generate` takes every option of Yuku's but `print`, which `@tsrx/yuku` sets to its TSRX printers.

## Types

`@tsrx/yuku` exports Yuku's types, `yuku/types` for the tree and `yuku/analyzer` for the semantic model, and its own for TSRX. It adds TSRX's nodes to Yuku's `DialectNodes`, so `Node`, `node.type` checks, and visitors know them.

```ts
/** What a template renders. */
type TemplateOutput =
  | JSXElement
  | JSXFragment
  | JSXStyleElement
  | JSXCodeBlock
  | JSXIfExpression
  | JSXForExpression
  | JSXSwitchExpression
  | JSXTryExpression;

/** `@{ statements output }`, a template with the code that sets it up. */
interface JSXCodeBlock extends BaseNode {
  type: "JSXCodeBlock";
  body: Statement[];
  render: TemplateOutput | null;
}

/** `@if (test) { } @else { }`, an `@else if` being an `IfStatement`. */
interface JSXIfExpression extends BaseNode {
  type: "JSXIfExpression";
  statementType: "IfStatement";
  test: Expression;
  consequent: BlockStatement;
  alternate: BlockStatement | IfStatement | null;
}

/** `@for (left of right; index i; key k) { } @empty { }`. */
interface JSXForExpression extends BaseNode {
  type: "JSXForExpression";
  statementType: "ForOfStatement";
  await: boolean;
  left: VariableDeclaration | Expression;
  right: Expression;
  index: Identifier | null;
  key: Expression | null;
  body: BlockStatement;
  empty: BlockStatement | null;
}

/** `@switch (discriminant) { @case test: { } @default: { } }`. */
interface JSXSwitchExpression extends BaseNode {
  type: "JSXSwitchExpression";
  statementType: "SwitchStatement";
  discriminant: Expression;
  cases: SwitchCase[];
}

/** `@try { } @pending { } @catch (error, reset) { }`. */
interface JSXTryExpression extends BaseNode {
  type: "JSXTryExpression";
  statementType: "TryStatement";
  block: BlockStatement;
  pending: BlockStatement | null;
  handler: (CatchClause & { resetParam: BindingPattern | null }) | null;
  finalizer: null;
}

/** `<style>`, its CSS kept as written. */
interface JSXStyleElement extends BaseNode {
  type: "JSXStyleElement";
  openingElement: JSXOpeningElement;
  /** The CSS, or nothing for `<style />`. */
  children: JSXText[];
  closingElement: JSXClosingElement | null;
}
```

These are read from [`src/ast.ts`](https://github.com/tsrx-org/yuku/blob/main/src/ast.ts). The `statementType` of each directive names the standard statement it reads like.
