code

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package code provides utilities for working with Go source code and ASTs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCallerCallExpr

func GetCallerCallExpr(pkgs map[string]*packages.Package, callerFile string, callerLine int) (*ast.CallExpr, *ast.File, *packages.Package, error)

GetCallerCallExpr retrieves the *ast.CallExpr at a specific location in the caller's source code.

`pkgs` is a map of package paths to *packages.Package, representing the parsed ASTs. `callerFile` is the filename of the caller's source file. `callerLine` is the line number in the caller's source file where the call expression is located.

It returns the *ast.CallExpr, the *ast.File containing the expression, the *packages.Package to which the file belongs, and an error if any occurred during the process. Returns nil values if the package, file or expression is not found.

func GetPackageAST

func GetPackageAST(pkgDir string) (map[string]*packages.Package, error)

GetPackageAST retrieves the parsed AST for a given package directory. It returns a map from package paths to parsed packages. The function uses a global cache to avoid reparsing the same package multiple times. If the package is not already cached it attempts to parse the package, caches it and returns the result. It returns an error if the package cannot be parsed.

func InitPackageASTCache

func InitPackageASTCache(pkgDir string)

InitPackageASTCache initializes the package AST cache. It is usually called from a TestMain function. It parses and caches the AST for the package located at pkgDir. It panics if the package cannot be parsed.

func ParsePackageAST

func ParsePackageAST(ctx context.Context, pkgDir string) (map[string]*packages.Package, error)

ParsePackageAST parses and loads the AST for a given package directory.

`ctx` is the context for the operation, allowing cancellation. `pkgDir` is the directory of the package to parse.

It returns a map of package paths to *packages.Package, and an error if the parsing fails or if any of the loaded packages contains errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

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