ast-visitor-pattern
它能做什么
这份指南指导你的智能体用冻结类和访问者来组织一组相关类型,使每处处理它们的代码都能被检查。适合使用 TypeScript、并且 switch 语句不断增多的开发者。
适用场景
- ✓当一组变体在两个或更多位置被处理,例如渲染器或序列化器的时候。
- ✓当你要为一个已经在多处被 switch 的类型添加新变体的时候。
- ✓当联合类型不断增长、其 switch 语句难以保持一致的时候。
技能链接
https://github.com/prisma/orm/blob/main/skills-contrib/ast-visitor-pattern/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请重构这个联合类型及其 switch 语句,让编译器能捕获任何遗漏的分支。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会把每个变体转换为带有 accept 方法的冻结类,并定义每个使用方都必须实现的访问者接口。这样,添加新变体后,任何未更新访问者的地方都会产生编译错误。
技术内容(原始 SKILL.md)
作者在 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
MongoAstNodebase —packages/2-mongo-family/4-query/query-ast/src/ast-node.tsMongoDdlCommandVisitorinterface —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
常见问题
什么时候适合使用这种模式?
该技能建议在联合类型有三个或以上变体,并且有两处或以上地方对其进行 switch 时使用。
示例使用什么语言?
示例使用 TypeScript,因此这种模式是针对 TypeScript 代码编写的。
编译器检查什么?
添加新变体后,每个未处理它的使用方都会报错,而不会被悄悄遗漏。
详情
作者的原始描述
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
热度
- GitHub 星标
- 48k
- 排名
- #6309
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
adr-review
48k教你的智能体以新鲜的视角审阅架构决策记录,然后重写,让新读者也能清楚理解其中的决定。
markdown-no-artificial-line-wraps
48k帮你的智能体把整段文字写在同一行中,让文字在屏幕上自然换行,并保持文件改动清晰。
multiline-commit-messages
48k向你的智能体展示一种在 shell 中安全编写多行 git 提交信息的方法,使信息不会出现乱码。
product-direction
48k帮你的智能体依据项目已书面记录的产品方向来评判功能请求,给出有理由的结论,并记录每一次否决。