ast-visitor-pattern

bởi prisma

Sử dụng mẫu frozen-class/visitor cho các union phân biệt có nhiều điểm dispatch. Sử dụng khi tạo một tập hợp các biến thể mới (lệnh, nút IR,…)

npx skills add https://github.com/prisma/prisma-next --skill ast-visitor-pattern

AST Class/Visitor Pattern

When a discriminated union has 3+ variants and 2+ dispatch sites (renderers, serializers, classifiers, etc.), replace plain union + switch with frozen subclasses and a visitor interface. This makes adding a new variant a compiler error at every consumer, instead of a silent omission.

Structure

Four pieces, always in the same file:

// 1. Abstract base (not exported — consumers use the union type)
abstract class FooNode {
  abstract readonly kind: string;
  abstract accept<R>(visitor: FooVisitor<R>): R;
  protected freeze(): void { Object.freeze(this); }
}

// 2. Visitor interface
export interface FooVisitor<R> {
  bar(node: BarNode): R;
  baz(node: BazNode): R;
}

// 3. Concrete subclasses — readonly fields, freeze() in constructor
export class BarNode extends FooNode {
  readonly kind = 'bar' as const;
  readonly value: string;
  constructor(value: string) {
    super();
    this.value = value;
    this.freeze();
  }
  accept<R>(visitor: FooVisitor<R>): R { return visitor.bar(this); }
}

export class BazNode extends FooNode {
  readonly kind = 'baz' as const;
  readonly count: number;
  constructor(count: number) {
    super();
    this.count = count;
    this.freeze();
  }
  accept<R>(visitor: FooVisitor<R>): R { return visitor.baz(this); }
}

// 4. Union type
export type Foo = BarNode | BazNode;

Consuming

Define a visitor object (or class) per concern:

const renderVisitor: FooVisitor<string> = {
  bar(node) { return node.value; },
  baz(node) { return String(node.count); },
};

function render(node: Foo): string {
  return node.accept(renderVisitor);
}

Always construct instances, never frozen object literals

This holds everywhere a node is built — tests and production construction surfaces (contract-free factories, builders). A factory must return new BarNode(...), never Object.freeze({ kind: 'bar', value: 'x' }). A frozen plain object has no prototype, so instanceof fails, accept() is missing, and a downstream shallow-copy ({ ...node }) silently strips the type back to an anonymous bag; constructor-time invariants are skipped too.

// ✅
const call = new BarNode('x');
export function bar(value: string): BarNode { return new BarNode(value); }

// ❌
const call: Foo = { kind: 'bar', value: 'x' };
export function bar(value: string): Foo { return Object.freeze({ kind: 'bar', value }); }

When NOT to use

  • Single dispatch site → plain union + switch is simpler
  • Fewer than 3 variants with no expected growth → not worth the boilerplate

Codebase examples

  • MongoAstNode / MongoDdlCommandVisitor — packages/2-mongo-family/4-query/query-ast/src/ddl-commands.ts
  • OpFactoryCall / OpFactoryCallVisitor — packages/3-mongo-target/1-mongo-target/src/core/op-factory-call.ts

Thêm skills từ prisma

adr-review
prisma
Xem xét một hoặc nhiều ADR với góc nhìn mới (với tư cách thành viên nhóm không có ngữ cảnh trước đó), xác định các vấn đề về nội dung và cấu trúc, sau đó viết lại chúng. Sử dụng khi…
bumping-biome
prisma
Bumps `biome` package versions (e.g. `@biomejs/biome`) using `pnpm`, aligns `biome.jsonc` files with the new version/s across the repository and runs…
contrib-pr
prisma
Mở một PR đóng góp bên ngoài chất lượng cao nhằm vào prisma-next. Sử dụng khi người dùng là người đóng góp bên ngoài (không phải người bảo trì Prisma) và muốn gửi một…
create-pr
prisma
Tạo một GitHub PR với tiêu đề có tiền tố Linear-ticket và mô tả dạng tường thuật dẫn dắt quyết định cho prisma-next. Sử dụng khi người dùng muốn tạo một pull…
draft-release-notes
prisma
Author the committed release-notes file for a stable Prisma Next release by enumerating the merged PRs since the previous stable `v*` tag, resolving opaque…
drive-agent-personas
prisma
Thư viện các agent personas — các bias-frames có tên mà các kỹ năng khác tải để thay đổi các mặc định tại thời điểm thực thi. Các kỹ năng đặt tên một persona theo ID (ví dụ: "Adopt the architect…
drive-code-review
prisma
Generate local PR/branch review artifacts for the current branch vs its base — an in-repo canonical spec (if present) or an inferred review `spec.md`, plus…
drive-create-plan
prisma
Sử dụng khi người dùng muốn lập kế hoạch cho một dự án, chia nhỏ đặc tả thành các cột mốc, tạo nhiệm vụ từ đặc tả, hoặc tạo kế hoạch thực thi. Suy ra các trường hợp kiểm thử từ…