Documentation
¶
Index ¶
- func BasicLiteralString(node ast.Node) (string, bool)
- func BuildCallSiteIndex(info *types.Info, files []*ast.File) map[types.Object][]*ast.CallExpr
- func EmbeddedFilesMatchingTemplateNameList(dir string, set *token.FileSet, comment ast.Node, ...) ([]string, error)
- func FindDefiningValue(info *types.Info, v *types.Var, files []*ast.File) (ast.Expr, bool)
- func FindDefiningValueInBlock(info *types.Info, v *types.Var, block ast.Node) (ast.Expr, bool)
- func FindEnclosingFuncLit(files []*ast.File, pos token.Pos) *ast.FuncLit
- func FindModificationReceiver(expr *ast.CallExpr, typesInfo *types.Info) types.Object
- func FuncLitVarObj(info *types.Info, files []*ast.File, fl *ast.FuncLit) types.Object
- func IsFuncParam(info *types.Info, files []*ast.File, expr ast.Expr) (paramIdx int, funcObj types.Object, ok bool)
- func IsTemplateMethod(typesInfo *types.Info, sel *ast.SelectorExpr) bool
- func IsTemplatePkgIdent(info *types.Info, ident *ast.Ident) bool
- func ParseTemplateNames(input string) []string
- func ReadComments(s *strings.Builder, groups ...*ast.CommentGroup) ast.Node
- func RelativeFilePaths(wd string, abs ...string) ([]string, error)
- func ResolveStringExpr(info *types.Info, files []*ast.File, expr ast.Expr) (string, bool)
- func ResolveStringSliceExpr(ctx *SliceEvalContext, expr ast.Expr) ([]string, bool)
- func StringLiteralExpression(wd string, set *token.FileSet, exp ast.Expr) (string, error)
- func StringLiteralExpressionList(wd string, set *token.FileSet, list []ast.Expr) ([]string, error)
- type EmbedFSResolver
- type Forrest
- type ParamBindings
- type SliceEvalContext
- type Template
- type TemplateFunctions
- type TemplateMetadata
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildCallSiteIndex ¶
BuildCallSiteIndex scans all files and builds a map from function types.Object to all call expressions that invoke it.
func EmbeddedFilesMatchingTemplateNameList ¶ added in v0.6.0
func FindDefiningValue ¶ added in v0.4.0
FindDefiningValue locates the RHS expression from the defining assignment of the given variable (either := or var declarations).
func FindDefiningValueInBlock ¶ added in v0.4.0
FindDefiningValueInBlock is like FindDefiningValue but searches within a single AST block (e.g. a function body) rather than across files.
func FindEnclosingFuncLit ¶ added in v0.5.0
FindEnclosingFuncLit returns the innermost FuncLit (closure) whose body contains pos. It only returns FuncLits that are NOT inside a FuncDecl's direct parameter list — i.e., it finds closures assigned to variables.
func FindModificationReceiver ¶
FindModificationReceiver unwraps template.Must and returns the types.Object of the variable receiver for a method call like ts.ParseFS(...) or template.Must(ts.ParseFS(...)). Returns nil if no variable receiver is found.
func FuncLitVarObj ¶ added in v0.5.0
FuncLitVarObj finds the variable that a FuncLit is assigned to. For example, given `render := func(...) { ... }`, it returns the types.Object for `render`. Returns nil if the FuncLit is not assigned to a named variable.
func IsFuncParam ¶
func IsFuncParam(info *types.Info, files []*ast.File, expr ast.Expr) (paramIdx int, funcObj types.Object, ok bool)
IsFuncParam reports whether the given expression is an identifier that refers to a function parameter. If so, it returns the parameter index (position in the function signature) and the types.Object for the enclosing function (or the variable a closure is assigned to).
func IsTemplateMethod ¶
func IsTemplateMethod(typesInfo *types.Info, sel *ast.SelectorExpr) bool
IsTemplateMethod reports whether sel refers to a method on *html/template.Template or *text/template.Template.
func IsTemplatePkgIdent ¶
IsTemplatePkgIdent reports whether ident refers to the "html/template" or "text/template" package via the type checker.
func ParseTemplateNames ¶ added in v0.6.0
func ReadComments ¶ added in v0.6.0
func ResolveStringExpr ¶
ResolveStringExpr attempts to statically resolve an AST expression to a string value. It handles string literals, named constants, and simple variable assignments (where the variable is defined once and never reassigned).
func ResolveStringSliceExpr ¶ added in v0.4.0
func ResolveStringSliceExpr(ctx *SliceEvalContext, expr ast.Expr) ([]string, bool)
ResolveStringSliceExpr evaluates an AST expression that produces a []string value. It handles composite literals, append, variables, filepath.Glob, and spread expressions.
func StringLiteralExpression ¶
Types ¶
type EmbedFSResolver ¶ added in v0.6.0
type EmbedFSResolver func(info *types.Info, files []*ast.File, fsIdent *ast.Ident) (paths []string, workingDir string, err error)
EmbedFSResolver is a callback that resolves an fs.FS identifier (typically a function parameter) to its embedded file paths by tracing through the call graph. It is called when embedFSFilePaths cannot find a package-level var declaration for the identifier. The resolver receives the identifier expression and the types.Info for the package containing the ParseFS call. It returns the list of relative embedded file paths and the working directory of the package that owns the //go:embed var, or an error.
type Forrest ¶
func NewForrest ¶
type ParamBindings ¶ added in v0.4.0
ParamBindings maps function parameter variables to their concrete string values from a call site, enabling cross-function string resolution.
func BuildParamBindings ¶ added in v0.4.0
func BuildParamBindings(info *types.Info, files []*ast.File, call *ast.CallExpr, fd *ast.FuncDecl) ParamBindings
BuildParamBindings constructs a ParamBindings map by matching call site arguments to function parameters. Only string-resolvable arguments are included. If an argument is itself a function parameter, the call graph is searched to resolve it from an outer call site.
type SliceEvalContext ¶ added in v0.4.0
type SliceEvalContext struct {
Info *types.Info
Files []*ast.File
Block ast.Node // scoped block (e.g. function body) for local var lookups
Bindings ParamBindings
WorkingDirectory string
EmbeddedPaths []string // relative paths of embedded files (for fs.Glob resolution)
EmbedResolver EmbedFSResolver // optional resolver for cross-package fs.FS tracing
// contains filtered or unexported fields
}
SliceEvalContext carries state for string-slice evaluation.
func (*SliceEvalContext) ResolveString ¶ added in v0.5.0
func (ctx *SliceEvalContext) ResolveString(expr ast.Expr) (string, bool)
ResolveString resolves a single AST expression to a string value.
func (*SliceEvalContext) WithBinding ¶ added in v0.5.0
func (ctx *SliceEvalContext) WithBinding(v *types.Var, value string) *SliceEvalContext
WithBinding returns a copy of the context with an additional variable binding. This is used for per-iteration evaluation, e.g. binding a range variable to a specific value for one iteration of a for-range loop.
type Template ¶
type Template interface {
New(name string) Template
Parse(text string) (Template, error)
Funcs(funcMap map[string]any) Template
Option(opt ...string) Template
Delims(left, right string) Template
Lookup(name string) Template
Name() string
AddParseTree(name string, tree *parse.Tree) (Template, error)
Tree() *parse.Tree
FindTree(name string) (*parse.Tree, bool)
TemplateNames() []string
}
Template abstracts over html/template.Template and text/template.Template so that the correct template package is used based on the user's import.
func EvaluateTemplateSelector ¶
func EvaluateTemplateSelector(ts Template, pkg *types.Package, typesInfo *types.Info, expression ast.Expr, workingDirectory, templatesVariable, rDelim, lDelim string, fileSet *token.FileSet, files []*ast.File, embeddedPaths []string, funcTypeMaps TemplateFunctions, fm map[string]any, meta *TemplateMetadata, sliceCtx *SliceEvalContext, embedFSResolver EmbedFSResolver) (Template, string, string, error)
func NewTemplate ¶
NewTemplate creates a Template backed by the appropriate template package either: "text/template" or "html/template".
type TemplateFunctions ¶
func DefaultFunctions ¶
func DefaultFunctions(pkg *types.Package) TemplateFunctions
func (TemplateFunctions) FindFunction ¶
func (functions TemplateFunctions) FindFunction(name string) (*types.Signature, bool)
type TemplateMetadata ¶
TemplateMetadata accumulates metadata during template evaluation.