Skip to content
Skillpedia
General coding48k Β· #6309

ast-visitor-pattern

What it does

It guides your agent to structure a set of related types with frozen classes and a visitor, so every place that handles them is checked. It is for developers working in TypeScript with growing switch statements.

When to use it

  • βœ“When a set of variants is handled in two or more places, such as renderers or serializers.
  • βœ“When you are adding a new variant to a type that is already switched over in several places.
  • βœ“When a union type has grown and its switch statements are hard to keep consistent.

Skill link

https://github.com/prisma/orm/blob/main/skills-contrib/ast-visitor-pattern/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/prisma/orm/blob/main/skills-contrib/ast-visitor-pattern/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œCan you refactor this union type and its switch statements so the compiler catches any missing case?”

5 new skills a week. 1 minute to read.

How it works

The agent turns each variant into a frozen class with an accept method, and defines a visitor interface that every consumer must implement. Adding a variant then causes a compile error wherever the visitor is not updated.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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, usually in one file β€” the Mongo DDL set below spreads them over three:

// 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 base β€” packages/2-mongo-family/4-query/query-ast/src/ast-node.ts
  • MongoDdlCommandVisitor interface β€” packages/2-mongo-family/4-query/query-ast/src/ddl-visitors.ts
  • Concrete DDL commands and their union β€” packages/2-mongo-family/4-query/query-ast/src/ddl-commands.ts
FAQ

When is this pattern worth it?

The skill suggests it when a union has three or more variants and two or more places that switch on it.

What language is the example in?

The example is in TypeScript, so the pattern is written for TypeScript code.

What does the compiler check?

Adding a new variant causes an error at every consumer that does not handle it, instead of being silently missed.

Details

Original description by the author

Use the frozen-class/visitor pattern for discriminated unions that have multiple dispatch sites. Use when creating a new set of variants (commands, IR nodes, factory calls) that will be switched over in 2+ places, or when refactoring an existing union type that has grown multiple switch sites.

repo: prisma/orm

Popularity

GitHub stars
48k
Ranking position
#6309
License
Apache-2.0
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills