ast-visitor-pattern

Standardize visitor pattern implementation for TypeScript discriminated unions.

47.5k|2.5k|Updated Jun 20, 2019
One-click install
npx skills add https://github.com/prisma/prisma --skill ast-visitor-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-visitor-pattern
Source: https://github.com/prisma/prisma/tree/main/skills-contrib/ast-visitor-pattern
Command: npx skills add https://github.com/prisma/prisma --skill ast-visitor-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill addresses the maintenance burden of discriminated unions in TypeScript by replacing fragile switch-case logic with a formal visitor pattern, ensuring that adding new variants triggers compile-time errors in all consumer locations.

Core Features & Use Cases

  • Type-Safe Dispatch: Enforces exhaustive handling of union variants across multiple dispatch sites like renderers or serializers.
  • Frozen Invariants: Provides a standardized structure for immutable nodes that prevents accidental mutation and ensures prototype integrity.
  • Use Case: When refactoring a complex AST with multiple node types, this pattern ensures that every time a new node type is added, the compiler forces you to update all visitors, preventing silent runtime failures.

Quick Start

Apply the ast-visitor-pattern structure to the current discriminated union by defining an abstract base class with an accept method and a corresponding visitor interface.

Frequently Asked Questions about ast-visitor-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce exhaustive type checking for discriminated unions in TypeScript?

To enforce exhaustive type checking for discriminated unions, implement a formal visitor pattern with abstract base classes and explicit visitor interfaces, replacing fragile switch-case logic to trigger compile-time errors when new variants are added.

What is the best way to handle complex AST node mutations safely during refactoring?

Handling complex AST node mutations safely requires using frozen subclasses to establish immutable nodes, preventing accidental mutation and ensuring prototype integrity while managing multiple dispatch sites like renderers or serializers.

How do I prevent silent runtime failures when adding new node types to an AST?

Preventing silent runtime failures when adding new AST node types involves standardizing dispatch logic through compiler-verified visitor interfaces, forcing updates across all consumer locations instead of relying on manual switch-case updates.

Does the visitor pattern work for managing multiple dispatch sites in TypeScript?

The visitor pattern effectively manages multiple dispatch sites in TypeScript by enforcing exhaustive variant handling across renderers and serializers, ensuring compiler-verified consistency during refactoring through explicit interfaces.

Why does using switch-case logic for TypeScript AST maintenance become fragile?

Switch-case logic becomes fragile because adding new union variants does not trigger compile-time errors in consumer locations, leading to silent runtime failures, whereas a formal visitor pattern enforces exhaustive handling across all dispatch sites.