Documentation
¶
Overview ¶
Package dstutil provides utilities for DST (Decorated Syntax Tree) manipulation.
Index ¶
- func InsertStatements(body *dst.BlockStmt, stmtStr string) bool
- func MatchesExact(a, b dst.Stmt) bool
- func MatchesSkeleton(a, b dst.Stmt) bool
- func ParseStatements(stmtStr string) ([]dst.Stmt, error)
- func RemoveStatements(body *dst.BlockStmt, index, count int) bool
- func UpdateStatements(body *dst.BlockStmt, index, count int, stmtStr string) bool
- type Comparator
- type NodeComparer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsertStatements ¶
InsertStatements inserts statements at the beginning of a function body.
func MatchesExact ¶
MatchesExact compares two statements for exact equality. Unlike MatchesSkeleton, this also compares literal values.
func MatchesSkeleton ¶
MatchesSkeleton compares two statements by their AST structure. It returns true if both statements have the same "skeleton" - same node types and static identifiers, but potentially different dynamic values (variables, literals).
func ParseStatements ¶
ParseStatements parses a statement string into DST statements. Supports multiple statements separated by newlines.
func RemoveStatements ¶
RemoveStatements removes `count` statements starting at the given index.
Types ¶
type Comparator ¶ added in v0.6.1
type Comparator struct {
// contains filtered or unexported fields
}
Comparator manages NodeComparer implementations and performs comparisons. It acts as a registry for node-specific comparers and handles dispatch.
func NewComparator ¶ added in v0.6.1
func NewComparator() *Comparator
NewComparator creates a new Comparator with the default set of comparers.
func (*Comparator) Compare ¶ added in v0.6.1
Compare compares two DST nodes using the registered comparers.
func (*Comparator) Register ¶ added in v0.6.1
func (c *Comparator) Register(nodeType reflect.Type, comparer NodeComparer)
Register adds a NodeComparer for a specific node type.
type NodeComparer ¶ added in v0.6.1
type NodeComparer interface {
// Compare compares two nodes of the same type.
// The nodes are guaranteed to be of the type this comparer handles.
// Use c.Compare for recursive child comparisons.
Compare(a, b dst.Node, path string, exact bool, c *Comparator) bool
}
NodeComparer defines the interface for comparing specific DST node types. Implementations handle the comparison logic for a single node type, delegating child node comparisons back to the Comparator.