Skip to content

Guide · Agent skill

How to use the AST Class/Visitor Pattern agent skill

Updated · written with AI from the project's own documentation and checked against our catalogue data

The AST Class/Visitor Pattern skill is a set of instructions that tells an AI agent how to structure a family of related variants in TypeScript, such as commands or syntax-tree nodes, as frozen classes with a shared visitor interface. It is written for developers maintaining compilers, query builders, or code generators where several switch statements handle the same union. After reading this guide you will know what the skill changes in the agent's output, how to install it, how to ask for it in plain language, and when to leave it off.

AST Class/Visitor Patternprisma/orm · Apache-2.0

Quick start

  1. 1Download the ZIP. Download the ZIP file for the ast-visitor-pattern skill from this page.
  2. 2Unzip into a skills folder. Extract the archive so SKILL.md sits in ~/.claude/skills/ for a personal install, or in a project's .claude/skills/ folder for a project install.
  3. 3Or upload it in the Claude apps. In the Claude apps, open Settings, go to Capabilities, and upload the skill under Skills.
  4. 4Describe the union in plain terms. Ask for help with a discriminated union that has three or more variants and is switched over in two or more places.
  5. 5Review the constructors. Check that new instances are built with constructors and that no factory returns a frozen plain object literal.
  6. 6Confirm compile errors. Add a test variant or check the visitor interface to confirm that every consumer now fails to compile until it handles the new case.

What the skill makes the agent do

When the agent works on a discriminated union with three or more variants that is handled in two or more places, such as renderers, serializers, or classifiers, the skill tells it to stop using a plain union checked by switch statements. Instead, each variant becomes a frozen subclass of an abstract base, and a visitor interface lists one method per variant.

The practical effect is that adding a new variant produces a compiler error at every consumer that must handle it, instead of a silent omission. The skill also tells the agent to always build these objects with constructors, never as frozen plain object literals, because a plain object loses its prototype, its accept() method, and its type under a shallow copy.

Install the skill from the ZIP download

Download the ZIP file from this page. The skill is a single file, SKILL.md, so the archive is small. Unzip it so that the folder containing SKILL.md sits inside a skills directory.

For a personal install across all your projects, unzip into ~/.claude/skills/. For a single project, unzip into that project's .claude/skills/ folder so the skill travels with the repository.

If you use the Claude apps instead of the file system, open Settings, go to Capabilities, and upload the skill under Skills.

  • Personal install: ~/.claude/skills/
  • Project install: .claude/skills/ inside your repository
  • Claude apps: Settings → Capabilities → Skills

Trigger the skill with natural requests

You do not need a special command. The skill activates when your request matches its description, which covers creating a new set of variants that will be switched over in two or more places, or refactoring a union type that has grown several switch sites.

Name the situation in plain terms. Mention the union, the switch sites, and the kind of variants involved, so the agent recognises the pattern.

  • Refactor the Command union in this compiler so each command is a frozen class with a visitor interface.
  • We have a discriminated union for query nodes and six switch statements over it. Convert it to the visitor pattern.
  • Add a new DDL command type and make sure every place that handles commands fails to compile until it is covered.
  • Build a factory function that returns node instances for these IR nodes instead of object literals.

Example: what the generated structure looks like

The skill's reference structure uses four pieces, usually in one file: an abstract base class that is not exported, a visitor interface with one method per variant, concrete subclasses with readonly fields that call freeze() in their constructors, and an exported union type. Each subclass implements accept() by calling the matching visitor method.

Consumers then define a visitor object for each concern, such as rendering, and call node.accept(visitor) rather than switching on node.kind. The skill's own example shows the constructor style to use and the object-literal style to avoid.

abstract class FooNode { abstract readonly kind: string; abstract accept<R>(visitor: FooVisitor<R>): R; protected freeze(): void { Object.freeze(this); } } export interface FooVisitor<R> { bar(node: BarNode): R; baz(node: BazNode): R; } export type Foo = BarNode | BazNode;

Know when not to use the pattern

The skill is explicit that the pattern is not worth it in some cases. If a union has only a single dispatch site, a plain union with a switch is simpler. If it has fewer than three variants and no expected growth, the boilerplate does not pay off.

Use the skill for code that is growing, not for a small, stable union. Check the variant count and the number of switch sites before asking for the conversion.

  • Single dispatch site: plain union and switch is simpler
  • Fewer than three variants with no expected growth: skip the pattern

Review the output and the codebase examples

The skill points to examples from the Prisma codebase: the MongoAstNode base in packages/2-mongo-family/4-query/query-ast/src/ast-node.ts, the MongoDdlCommandVisitor interface in ddl-visitors.ts, and the concrete DDL commands and their union in ddl-commands.ts. Note that the Mongo DDL set spreads the four pieces across three files, while the reference structure keeps them in one.

When reviewing, check that every constructed node comes from new ClassName(...), including in tests and factories, and that no code path returns Object.freeze on a plain literal.

  • Check for new BarNode(...) calls rather than object literals cast to the union type
  • Confirm that adding a variant produces compile errors at each consumer

What people use it for

  • Converting a compiler's command union into frozen classes with a visitor interface so each renderer and serializer is checked when a new command is added.
  • Refactoring a query builder's node union that has grown several switch statements across validation, printing, and transformation code.
  • Building factory functions for a code generator's intermediate representation that return class instances instead of frozen object literals.
  • Working on Prisma's Mongo query or DDL AST code, using the referenced files as the model for new variants.

Frequently asked questions

How do I install the ast-visitor-pattern agent skill?

Download the ZIP from this page and unzip it so SKILL.md sits in ~/.claude/skills/ for all your projects, or in a project's .claude/skills/ folder for one repository. In the Claude apps, you can instead upload it under Settings → Capabilities → Skills.

Does the ast-visitor-pattern skill require anything else to run?

The listing shows no requirements. The skill does not run scripts and does not use the network, and it is a single SKILL.md file.

Is the AST Class/Visitor Pattern skill free?

The source material for this listing is licensed under Apache-2.0. The page does not list any price, so check the listing's licence terms for the exact conditions of use.

Can I use this skill in a JavaScript or Python project?

No. The skill is TypeScript-specific, and its examples and guidance assume TypeScript classes, interfaces, and union types. Non-TypeScript codebases are not a good fit.

When should I not use the visitor pattern?

Skip it when a union has only one dispatch site, or when it has fewer than three variants with no expected growth. The skill itself says the boilerplate is not worth it in those cases.

What is the difference between this skill and a plain switch statement?

A plain switch lets you forget a case silently. With the visitor pattern, adding a new variant makes the compiler flag every consumer that must handle it, which is the main reason the skill recommends it.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Use this skill if you maintain a TypeScript compiler, query builder, or code generator where a growing union is switched over in several places, and you want the compiler to catch missing cases. Skip it for small or stable unions, single switch sites, or non-TypeScript code, where a plain union and switch is simpler.

Download the skill