> For the complete documentation index, see [llms.txt](https://gofast.disasm.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gofast.disasm.dev/basics/visitor-api.md).

# Visitor API

The visitor API is the core mechanism for traversing and transforming ASTs in **go-fAST**. It allows you to declaratively walk the tree and perform logic at each node type.

This page explains how the visitor pattern works, how to define your own visitor, and gives examples of common traversal use cases.

***

### 🧱 Visitor Interface

The `Visitor` interface defines a method for every AST node type. Implement any subset of these methods in your own struct to perform custom logic when those node types are encountered.

```go
type Visitor interface {
    VisitProgram(*ast.Program)
    VisitIdentifier(*ast.Identifier)
    VisitExpression(*ast.Expression)
    VisitIfStatement(*ast.IfStatement)
    VisitVariableDeclaration(*ast.VariableDeclaration)
    // ...and so on for all supported nodes
}
```

You don’t need to implement all methods—only those you're interested in. You can embed `ast.NoopVisitor` to automatically forward unimplemented visits to children.

### ✨ Example: Logging All Identifiers

```go
type IdentifierPrinter struct {
    ast.NoopVisitor
}

func (v *IdentifierPrinter) VisitIdentifier(n *ast.Identifier) {
    fmt.Println("Found identifier:", n.Name)
}

func main() {
    src := `let a = b + c;`
    p, _ := parser.ParseFile(src)
    
    visitor := &IdentifierPrinter{}
    visitor.V = visitor
    p.VisitWith(visitor)
}
```

This will print:

```
a
b
c
```

***

### 🪄 Example: Collecting All Literals

```go
type LiteralCollector struct {
    ast.NoopVisitor
    Strings []string
    Numbers []float64
}

func (v *LiteralCollector) VisitStringLiteral(n *ast.StringLiteral) {
    v.Strings = append(v.Strings, n.Value)
}

func (v *LiteralCollector) VisitNumberLiteral(n *ast.NumberLiteral) {
    v.Numbers = append(v.Numbers, n.Value)
}
```

### 🔁 Recursive Traversal

The `VisitChildrenWith(visitor)` method is available on every node, and called automatically by the default `NoopVisitor` implementation.

You may override it if you need to:

* Modify traversal order (e.g. pre-order vs post-order)
* Short-circuit based on conditions

### 🔍 Full List of Visitor Methods

The following are all available visitor methods you can override in your own struct:

```go
func (v *ExampleVisitor) VisitArrayLiteral(s *ast.ArrayLiteral) {}
func (v *ExampleVisitor) VisitArrayPattern(s *ast.ArrayPattern) {}
func (v *ExampleVisitor) VisitArrowFunctionLiteral(s *ast.ArrowFunctionLiteral) {}
func (v *ExampleVisitor) VisitAssignExpression(s *ast.AssignExpression) {}
func (v *ExampleVisitor) VisitAwaitExpression(s *ast.AwaitExpression) {}
func (v *ExampleVisitor) VisitBadStatement(s *ast.BadStatement) {}
func (v *ExampleVisitor) VisitBinaryExpression(s *ast.BinaryExpression) {}
func (v *ExampleVisitor) VisitBindingTarget(s *ast.BindingTarget) {}
func (v *ExampleVisitor) VisitBlockStatement(s *ast.BlockStatement) {}
func (v *ExampleVisitor) VisitBooleanLiteral(s *ast.BooleanLiteral) {}
func (v *ExampleVisitor) VisitBreakStatement(s *ast.BreakStatement) {}
func (v *ExampleVisitor) VisitCallExpression(s *ast.CallExpression) {}
func (v *ExampleVisitor) VisitCaseStatement(s *ast.CaseStatement) {}
func (v *ExampleVisitor) VisitCaseStatements(s *ast.CaseStatements) {}
func (v *ExampleVisitor) VisitCatchStatement(s *ast.CatchStatement) {}
func (v *ExampleVisitor) VisitClassDeclaration(s *ast.ClassDeclaration) {}
func (v *ExampleVisitor) VisitClassElement(s *ast.ClassElement) {}
func (v *ExampleVisitor) VisitClassElements(s *ast.ClassElements) {}
func (v *ExampleVisitor) VisitClassLiteral(s *ast.ClassLiteral) {}
func (v *ExampleVisitor) VisitClassStaticBlock(s *ast.ClassStaticBlock) {}
func (v *ExampleVisitor) VisitComputedProperty(s *ast.ComputedProperty) {}
func (v *ExampleVisitor) VisitConciseBody(s *ast.ConciseBody) {}
func (v *ExampleVisitor) VisitConditionalExpression(s *ast.ConditionalExpression) {}
func (v *ExampleVisitor) VisitContinueStatement(s *ast.ContinueStatement) {}
func (v *ExampleVisitor) VisitDebuggerStatement(s *ast.DebuggerStatement) {}
func (v *ExampleVisitor) VisitDoWhileStatement(s *ast.DoWhileStatement) {}
func (v *ExampleVisitor) VisitEmptyStatement(s *ast.EmptyStatement) {}
func (v *ExampleVisitor) VisitExpression(s *ast.Expression) {}
func (v *ExampleVisitor) VisitExpressionStatement(s *ast.ExpressionStatement) {}
func (v *ExampleVisitor) VisitExpressions(s *ast.Expressions) {}
func (v *ExampleVisitor) VisitFieldDefinition(s *ast.FieldDefinition) {}
func (v *ExampleVisitor) VisitForInStatement(s *ast.ForInStatement) {}
func (v *ExampleVisitor) VisitForInto(s *ast.ForInto) {}
func (v *ExampleVisitor) VisitForLoopInitializer(s *ast.ForLoopInitializer) {}
func (v *ExampleVisitor) VisitForOfStatement(s *ast.ForOfStatement) {}
func (v *ExampleVisitor) VisitForStatement(s *ast.ForStatement) {}
func (v *ExampleVisitor) VisitFunctionDeclaration(s *ast.FunctionDeclaration) {}
func (v *ExampleVisitor) VisitFunctionLiteral(s *ast.FunctionLiteral) {}
func (v *ExampleVisitor) VisitIdentifier(s *ast.Identifier) {}
func (v *ExampleVisitor) VisitIfStatement(s *ast.IfStatement) {}
func (v *ExampleVisitor) VisitInvalidExpression(s *ast.InvalidExpression) {}
func (v *ExampleVisitor) VisitLabelledStatement(s *ast.LabelledStatement) {}
func (v *ExampleVisitor) VisitMemberExpression(s *ast.MemberExpression) {}
func (v *ExampleVisitor) VisitMemberProperty(s *ast.MemberProperty) {}
func (v *ExampleVisitor) VisitMetaProperty(s *ast.MetaProperty) {}
func (v *ExampleVisitor) VisitMethodDefinition(s *ast.MethodDefinition) {}
func (v *ExampleVisitor) VisitNewExpression(s *ast.NewExpression) {}
func (v *ExampleVisitor) VisitNullLiteral(s *ast.NullLiteral) {}
func (v *ExampleVisitor) VisitNumberLiteral(s *ast.NumberLiteral) {}
func (v *ExampleVisitor) VisitObjectLiteral(s *ast.ObjectLiteral) {}
func (v *ExampleVisitor) VisitObjectPattern(s *ast.ObjectPattern) {}
func (v *ExampleVisitor) VisitOptional(s *ast.Optional) {}
func (v *ExampleVisitor) VisitOptionalChain(s *ast.OptionalChain) {}
func (v *ExampleVisitor) VisitParameterList(s *ast.ParameterList) {}
func (v *ExampleVisitor) VisitPrivateDotExpression(s *ast.PrivateDotExpression) {}
func (v *ExampleVisitor) VisitPrivateIdentifier(s *ast.PrivateIdentifier) {}
func (v *ExampleVisitor) VisitProgram(s *ast.Program) {}
func (v *ExampleVisitor) VisitProperties(s *ast.Properties) {}
func (v *ExampleVisitor) VisitProperty(s *ast.Property) {}
func (v *ExampleVisitor) VisitPropertyKeyed(s *ast.PropertyKeyed) {}
func (v *ExampleVisitor) VisitPropertyShort(s *ast.PropertyShort) {}
func (v *ExampleVisitor) VisitRegExpLiteral(s *ast.RegExpLiteral) {}
func (v *ExampleVisitor) VisitReturnStatement(s *ast.ReturnStatement) {}
func (v *ExampleVisitor) VisitSequenceExpression(s *ast.SequenceExpression) {}
func (v *ExampleVisitor) VisitSpreadElement(s *ast.SpreadElement) {}
func (v *ExampleVisitor) VisitStatement(s *ast.Statement) {}
func (v *ExampleVisitor) VisitStatements(s *ast.Statements) {}
func (v *ExampleVisitor) VisitStringLiteral(s *ast.StringLiteral) {}
func (v *ExampleVisitor) VisitSuperExpression(s *ast.SuperExpression) {}
func (v *ExampleVisitor) VisitSwitchStatement(s *ast.SwitchStatement) {}
func (v *ExampleVisitor) VisitTemplateElement(s *ast.TemplateElement) {}
func (v *ExampleVisitor) VisitTemplateElements(s *ast.TemplateElements) {}
func (v *ExampleVisitor) VisitTemplateLiteral(s *ast.TemplateLiteral) {}
func (v *ExampleVisitor) VisitThisExpression(s *ast.ThisExpression) {}
func (v *ExampleVisitor) VisitThrowStatement(s *ast.ThrowStatement) {}
func (v *ExampleVisitor) VisitTryStatement(s *ast.TryStatement) {}
func (v *ExampleVisitor) VisitUnaryExpression(s *ast.UnaryExpression) {}
func (v *ExampleVisitor) VisitUpdateExpression(s *ast.UpdateExpression) {}
func (v *ExampleVisitor) VisitVariableDeclaration(s *ast.VariableDeclaration) {}
func (v *ExampleVisitor) VisitVariableDeclarator(s *ast.VariableDeclarator) {}
func (v *ExampleVisitor) VisitVariableDeclarators(s *ast.VariableDeclarators) {}
func (v *ExampleVisitor) VisitWhileStatement(s *ast.WhileStatement) {}
func (v *ExampleVisitor) VisitWithStatement(s *ast.WithStatement) {}
func (v *ExampleVisitor) VisitYieldExpression(s *ast.YieldExpression) {}
```

> 💡 Tip: Look at `ast/gen_visit.go` or use `NoopVisitor` as a reference for the full set.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gofast.disasm.dev/basics/visitor-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
