astutil

package
v0.0.0-...-a84439d Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package astutil provides AST utilities and shared types for Go source analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindAssignments

func FindAssignments(node ast.Node) []*ast.AssignStmt

FindAssignments returns all assignment statements in a node.

func FindCallExprs

func FindCallExprs(node ast.Node) []*ast.CallExpr

FindCallExprs returns all call expressions in a node.

func FindFuncDecls

func FindFuncDecls(file *ast.File) []*ast.FuncDecl

FindFuncDecls returns all function declarations in a file.

func FindMethodCalls

func FindMethodCalls(node ast.Node, methodName string) []*ast.CallExpr

FindMethodCalls finds all method calls matching a pattern in a node.

func FindVarDecls

func FindVarDecls(node ast.Node) []*ast.GenDecl

FindVarDecls returns all variable declarations in a node.

func GetCallArg

func GetCallArg(call *ast.CallExpr, index int) ast.Expr

GetCallArg gets a specific argument from a function call (0-indexed).

func GetCallName

func GetCallName(call *ast.CallExpr) string

GetCallName returns the name of a function call (e.g., "r.GET" or "http.HandleFunc").

func GetLineNumber

func GetLineNumber(fset *token.FileSet, node ast.Node) int

GetLineNumber returns the line number for an AST node.

func GetReceiverName

func GetReceiverName(fn *ast.FuncDecl) string

GetReceiverName returns the receiver variable name for a method.

func GetReceiverType

func GetReceiverType(fn *ast.FuncDecl) string

GetReceiverType returns the receiver type name for a method.

func GetStringSlice

func GetStringSlice(expr ast.Expr) []string

GetStringSlice extracts a []string from an array/slice literal.

func GetStringValue

func GetStringValue(expr ast.Expr) string

GetStringValue extracts the string value from an expression.

func IsMethodCall

func IsMethodCall(call *ast.CallExpr, receiverName, methodName string) bool

IsMethodCall checks if a call expression is a method call on a specific receiver.

Types

type ParsedSource

type ParsedSource struct {
	// FilePath is the path to the source file.
	FilePath string

	// FileSet is the token file set.
	FileSet *token.FileSet

	// AST is the parsed AST.
	AST *ast.File

	// Content is the raw source content.
	Content string

	// Imports maps import paths to their aliases.
	Imports map[string]string
}

ParsedSource represents a parsed Go source file.

func (*ParsedSource) GetImportAlias

func (s *ParsedSource) GetImportAlias(importPath string) string

GetImportAlias returns the alias for an import path, or empty string if not found.

func (*ParsedSource) HasImport

func (s *ParsedSource) HasImport(importPath string) bool

HasImport checks if the source has a specific import path.

func (*ParsedSource) HasImportPrefix

func (s *ParsedSource) HasImportPrefix(prefix string) bool

HasImportPrefix checks if the source has any import with the given prefix.

type SourceLoader

type SourceLoader struct {
	// contains filtered or unexported fields
}

SourceLoader handles loading and parsing Go source files.

func NewSourceLoader

func NewSourceLoader() *SourceLoader

NewSourceLoader creates a new SourceLoader.

func (*SourceLoader) ParseContent

func (l *SourceLoader) ParseContent(path, content string) (*ParsedSource, error)

ParseContent parses Go source code from a string.

func (*SourceLoader) ParseFile

func (l *SourceLoader) ParseFile(path string) (*ParsedSource, error)

ParseFile parses a Go source file.

func (*SourceLoader) TryParseFile

func (l *SourceLoader) TryParseFile(path string) (*ParsedSource, string)

TryParseFile attempts to parse a file, returning nil and error string on failure.

Jump to

Keyboard shortcuts

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