ast-visitor-pattern
यह क्या करती है
यह आपके एजेंट को संबंधित types के समूह को frozen classes और एक visitor के साथ ढालना सिखाती है, ताकि हर जगह जो उन्हें संभालती है, वह जाँची जाए। यह TypeScript में काम करने वाले डेवलपर्स के लिए है, जिनके switch statements बढ़ते जा रहे हैं।
कब इस्तेमाल करें
- ✓जब variants के एक समूह को दो या अधिक जगहों पर संभाला जाता है, जैसे renderers या serializers में।
- ✓जब आप किसी ऐसे type में नया variant जोड़ रहे हैं जिस पर पहले से कई जगह switch होता है।
- ✓जब union type बड़ा हो गया है और उसके switch statements में एकरूपता बनाए रखना मुश्किल है।
Skill का लिंक
https://github.com/prisma/orm/blob/main/skills-contrib/ast-visitor-pattern/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“क्या आप इस union type और उसके switch statements को इस तरह बदल सकते हैं कि compiler कोई छूटा case पकड़ ले?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
एजेंट हर variant को एक frozen class में बदलता है जिसमें एक accept method होता है, और एक visitor interface परिभाषित करता है जिसे हर उपभोक्ता लागू करता है। फिर नया variant जोड़ने पर, जहाँ visitor अपडेट नहीं हुआ, वहाँ compile error आता है।
तकनीकी सामग्री (मूल 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
अक्सर पूछे जाने वाले सवाल
यह पैटर्न कब फ़ायदेमंद है?
स्किल इसे तब सुझाती है जब किसी union में तीन या अधिक variants हों और दो या अधिक जगह उस पर switch होता हो।
उदाहरण किस भाषा में है?
उदाहरण TypeScript में है, इसलिए यह पैटर्न TypeScript कोड के लिए लिखा गया है।
compiler क्या जाँचता है?
नया variant जोड़ने पर हर उस उपभोक्ता पर error आता है जो उसे संभालता नहीं, बजाय इसके कि वह चुपचाप छूट जाए।
जानकारी
लेखक का मूल विवरण
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
- अपडेट की गई
- 2/11/2026
कमेंट
- …
इस repository की और skills
adr-review
48kआपके एजेंट को architecture decision records को नई नज़र से जाँचना और फिर उन्हें दोबारा लिखना सिखाती है, ताकि नए पाठकों को फ़ैसला साफ़ समझ आए।
markdown-no-artificial-line-wraps
48kआपके एजेंट को Markdown में पूरे पैराग्राफ़ एक ही पंक्ति में लिखने में मदद करती है, ताकि स्क्रीन पर टेक्स्ट साफ़ लिपटे और फ़ाइल के बदलाव भी साफ़ रहें।
multiline-commit-messages
48kआपके एजेंट को shell में बहु-पंक्ति git commit संदेश सुरक्षित तरीके से लिखना दिखाती है, ताकि संदेश गड़बड़ न हो।
product-direction
48kआपके एजेंट को किसी फ़ीचर अनुरोध को प्रोजेक्ट की लिखित product दिशा से मिलाने, तर्क सहित फ़ैसला देने और हर 'ना' को दर्ज करने में मदद करती है।
मिलती-जुलती skills
diffs
394kयह आपके एजेंट को बदलावों का असली diff बनाने देती है, जिसे व्यूअर लिंक या इमेज फ़ाइल के रूप में साझा किया जा सकता है, हाथ से लिखे सारांश के बजाय।
coding-agent
394kयह आपके एजेंट को बड़े कोडिंग काम Codex, Claude Code या OpenCode को बैकग्राउंड वर्कर के रूप में सौंपने, फिर उसकी प्रगति जांचने में मदद करती है।
gh-issues
394kयह आपके एजेंट को GitHub issues खोजने, उनमें से काम करने लायक चुनने, और सुधार के साथ pull requests खोलने में मदद करती है, जिसमें समीक्षा टिप्पणियों का फ़ॉलो-अप भी शामिल है।