dstutil

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package dstutil provides utilities for DST (Decorated Syntax Tree) manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InsertStatements

func InsertStatements(body *dst.BlockStmt, stmtStr string) bool

InsertStatements inserts statements at the beginning of a function body.

func MatchesExact

func MatchesExact(a, b dst.Stmt) bool

MatchesExact compares two statements for exact equality. Unlike MatchesSkeleton, this also compares literal values.

func MatchesSkeleton

func MatchesSkeleton(a, b dst.Stmt) bool

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

func ParseStatements(stmtStr string) ([]dst.Stmt, error)

ParseStatements parses a statement string into DST statements. Supports multiple statements separated by newlines.

func RemoveStatements

func RemoveStatements(body *dst.BlockStmt, index, count int) bool

RemoveStatements removes `count` statements starting at the given index.

func UpdateStatements

func UpdateStatements(body *dst.BlockStmt, index, count int, stmtStr string) bool

UpdateStatements updates statements starting at the given index. It replaces `count` statements with the parsed statements from stmtStr.

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

func (c *Comparator) Compare(a, b dst.Node, path string, exact bool) bool

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.

Jump to

Keyboard shortcuts

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