js

package
v1.23.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inspect

func Inspect(node Node, f func(Node) bool)

Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).

In many cases it may be more convenient to use Preorder, which returns an iterator over the sequence of nodes, or PreorderStack, which (like Inspect) provides control over descent into subtrees, but additionally reports the stack of enclosing nodes.

func Preorder

func Preorder(root Node) iter.Seq[Node]

Preorder returns an iterator over all the nodes of the syntax tree beneath (and including) the specified root, in depth-first preorder.

For greater control over the traversal of each subtree, use Inspect or PreorderStack.

func PreorderStack

func PreorderStack(root Node, stack []Node, f func(n Node, stack []Node) bool)

PreorderStack traverses the tree rooted at root, calling f before visiting each node.

Each call to f provides the current node and traversal stack, consisting of the original value of stack appended with all nodes from root to n, excluding n itself. (This design allows calls to PreorderStack to be nested without double counting.)

If f returns false, the traversal skips over that subtree. Unlike Inspect, no second call to f is made after visiting node n. (In practice, the second call is nearly always used only to pop the stack, and it is surprisingly tricky to do this correctly.)

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

Types

type ArrayLit

type ArrayLit struct {
	Lbrack token.Pos // position of "["
	Values []Expr    // array elements; or nil
	Rbrack token.Pos // position of "]"
}

An ArrayLit node represents an array literal.

func (*ArrayLit) End

func (x *ArrayLit) End() token.Pos

func (*ArrayLit) Pos

func (x *ArrayLit) Pos() token.Pos

type BasicLit

type BasicLit struct {
	ValuePos token.Pos   // literal position
	ValueEnd token.Pos   // position immediately after the literal
	Kind     token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
	Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
}

func (*BasicLit) End

func (x *BasicLit) End() token.Pos

func (*BasicLit) Pos

func (x *BasicLit) Pos() token.Pos

type BinaryExpr

type BinaryExpr struct {
	X     Expr        // left operand
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	Y     Expr        // right operand
}

A BinaryExpr node represents a binary expression.

func (*BinaryExpr) End

func (x *BinaryExpr) End() token.Pos

func (*BinaryExpr) Pos

func (x *BinaryExpr) Pos() token.Pos

type BlockStmt

type BlockStmt struct {
	Lbrace token.Pos // position of "{"
	List   []Stmt
	Rbrace token.Pos // position of "}", if any (may be absent due to syntax error)
}

A BlockStmt node represents a braced statement list.

func (*BlockStmt) End

func (s *BlockStmt) End() token.Pos

func (*BlockStmt) Pos

func (s *BlockStmt) Pos() token.Pos

type BranchStmt

type BranchStmt struct {
	TokPos token.Pos   // position of Tok
	Tok    token.Token // keyword token (BREAK, CONTINUE)
	Label  *Ident      // label name; or nil
}

A BranchStmt node represents a break or continue statement.

func (*BranchStmt) End

func (s *BranchStmt) End() token.Pos

func (*BranchStmt) Pos

func (s *BranchStmt) Pos() token.Pos

type CallExpr

type CallExpr struct {
	Fun      Expr      // function expression
	Lparen   token.Pos // position of "("
	Args     []Expr    // function arguments; or nil
	Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
	Rparen   token.Pos // position of ")"
}

A CallExpr node represents an expression followed by an argument list.

func (*CallExpr) End

func (x *CallExpr) End() token.Pos

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Pos

type Comment

type Comment = ast.Comment

A Comment node represents a single //-style or /*-style comment.

The Text field contains the comment text without carriage returns (\r) that may have been present in the source. Because a comment's end position is computed using len(Text), the position reported by [Comment.End] does not match the true source end position for comments containing carriage returns.

type CommentGroup

type CommentGroup = ast.CommentGroup

A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.

type EmptyStmt

type EmptyStmt struct {
	Semicolon token.Pos // position of following ";"
	Implicit  bool      // if set, ";" was omitted in the source
}

An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately following (explicit or implicit) semicolon.

func (*EmptyStmt) End

func (s *EmptyStmt) End() token.Pos

func (*EmptyStmt) Pos

func (s *EmptyStmt) Pos() token.Pos

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

All expression nodes implement the Expr interface.

type ExprStmt

type ExprStmt struct {
	X Expr // expression
}

An ExprStmt node represents a (stand-alone) expression in a statement list.

func (*ExprStmt) End

func (s *ExprStmt) End() token.Pos

func (*ExprStmt) Pos

func (s *ExprStmt) Pos() token.Pos

type File

type File struct {
	Doc      *CommentGroup   // associated documentation; or nil
	Stmts    []Stmt          // top-level statements; or nil
	Comments []*CommentGroup // comments in the file, in lexical order
}

A File node represents a JavaScript source file.

type ForStmt

type ForStmt struct {
	For  token.Pos // position of "for" keyword
	Init Stmt      // initialization statement; or nil
	Cond Expr      // condition; or nil
	Post Stmt      // post iteration statement; or nil
	Body *BlockStmt
}

A ForStmt represents a for statement.

func (*ForStmt) End

func (s *ForStmt) End() token.Pos

func (*ForStmt) Pos

func (s *ForStmt) Pos() token.Pos

type FuncDecl

type FuncDecl struct {
	Doc     *CommentGroup // associated documentation; or nil
	Func    token.Pos     // position of "function" keyword
	Name    *Ident        // function/method name
	Opening token.Pos     // position of (
	Params  []*Ident      // parameters
	Closing token.Pos     // position of )
	Body    *BlockStmt    // function body
}

A FuncDecl node represents a function declaration.

function Name(Params) { Body }

func (*FuncDecl) End

func (f *FuncDecl) End() token.Pos

func (*FuncDecl) Pos

func (f *FuncDecl) Pos() token.Pos

type FuncLit

type FuncLit struct {
	Opening token.Pos  // position of (
	Params  []*Ident   // parameters
	Closing token.Pos  // position of )
	Body    *BlockStmt // function body
}

A FuncLit node represents a function literal.

func (*FuncLit) End

func (x *FuncLit) End() token.Pos

func (*FuncLit) Pos

func (x *FuncLit) Pos() token.Pos

type Ident

type Ident struct {
	NamePos token.Pos // identifier position
	Name    string    // identifier name
	Obj     *Object   // denoted object, or nil.
}

An Ident node represents an identifier.

func (*Ident) End

func (x *Ident) End() token.Pos

func (*Ident) Pos

func (x *Ident) Pos() token.Pos

type IfStmt

type IfStmt struct {
	If   token.Pos // position of "if" keyword
	Cond Expr      // condition
	Body *BlockStmt
	Else Stmt // else branch; or nil
}

An IfStmt node represents an if statement.

func (*IfStmt) End

func (s *IfStmt) End() token.Pos

func (*IfStmt) Pos

func (s *IfStmt) Pos() token.Pos

type ImportDecl

type ImportDecl struct {
	Import token.Pos     // position of "import" keyword
	Specs  []*ImportSpec // import specifications
	Path   *BasicLit     // import path
}

An ImportDecl represents an import declaration.

import { Name1, Name2 as AliasName2, type Name3 } from "path"

func (*ImportDecl) End

func (i *ImportDecl) End() token.Pos

func (*ImportDecl) Pos

func (i *ImportDecl) Pos() token.Pos

type ImportSpec

type ImportSpec struct {
	Type  token.Pos // position of "type"; or token.NoPos
	Name  *Ident    // symbol name or type name to import
	Alias *Ident    // alias name; or nil
}

An ImportSpec represents an import specification.

func (*ImportSpec) End

func (i *ImportSpec) End() token.Pos

func (*ImportSpec) Pos

func (i *ImportSpec) Pos() token.Pos

type IndexExpr

type IndexExpr struct {
	X      Expr      // expression
	Lbrack token.Pos // position of "["
	Index  Expr      // index expression
	Rbrack token.Pos // position of "]"
}

An IndexExpr node represents an expression followed by an index.

func (*IndexExpr) End

func (x *IndexExpr) End() token.Pos

func (*IndexExpr) Pos

func (x *IndexExpr) Pos() token.Pos

type LabeledStmt

type LabeledStmt struct {
	Label *Ident
	Colon token.Pos // position of ":"
	Stmt  Stmt
}

A LabeledStmt node represents a labeled statement.

func (*LabeledStmt) End

func (s *LabeledStmt) End() token.Pos

func (*LabeledStmt) Pos

func (s *LabeledStmt) Pos() token.Pos

type Node

type Node interface {
	Pos() token.Pos
	End() token.Pos
}

type Object

type Object struct {
	Name string // declared name
	Data any    // object-specific data; or nil
}

type ParenExpr

type ParenExpr struct {
	Lparen token.Pos // position of "("
	X      Expr      // parenthesized expression
	Rparen token.Pos // position of ")"
}

A ParenExpr node represents a parenthesized expression.

func (*ParenExpr) End

func (x *ParenExpr) End() token.Pos

func (*ParenExpr) Pos

func (x *ParenExpr) Pos() token.Pos

type ReturnStmt

type ReturnStmt struct {
	Return token.Pos // position of "return" keyword
	Result Expr      // result expression; or nil
}

A ReturnStmt node represents a return statement.

func (*ReturnStmt) End

func (s *ReturnStmt) End() token.Pos

func (*ReturnStmt) Pos

func (s *ReturnStmt) Pos() token.Pos

type SelectorExpr

type SelectorExpr struct {
	X   Expr   // expression; or nil (for an implicit package name)
	Sel *Ident // field selector
}

A SelectorExpr node represents an expression followed by a selector.

func (*SelectorExpr) End

func (x *SelectorExpr) End() token.Pos

func (*SelectorExpr) Pos

func (x *SelectorExpr) Pos() token.Pos

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

All statement nodes implement the Stmt interface.

type UnaryExpr

type UnaryExpr struct {
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	X     Expr        // operand
}

A UnaryExpr node represents a unary expression.

func (*UnaryExpr) End

func (x *UnaryExpr) End() token.Pos

func (*UnaryExpr) Pos

func (x *UnaryExpr) Pos() token.Pos

type ValueDecl

type ValueDecl struct {
	Doc    *CommentGroup // associated documentation; or nil
	TokPos token.Pos     // position of Tok
	Tok    token.Token   // VAR (let) or CONST (const)
	Name   *Ident        // name
	Value  Expr          // value; or nil
}

A ValueDecl node represents a variable or constant declaration.

let Name = Value const Name = Value

func (*ValueDecl) End

func (d *ValueDecl) End() token.Pos

func (*ValueDecl) Pos

func (d *ValueDecl) Pos() token.Pos

type Visitor

type Visitor interface {
	Visit(node Node) (w Visitor)
}

A Visitor's Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).

Directories

Path Synopsis
Package format implements standard formatting of JavaScript source.
Package format implements standard formatting of JavaScript source.
Package printer implements printing of AST nodes.
Package printer implements printing of AST nodes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL