astx

package
v3.6.0-beta.21 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractUsedHelpers

func ExtractUsedHelpers(pkg *packages.Package, root ast.Node) ([]ast.Decl, []*types.PkgName, error)

ExtractUsedHelpers performs a BFS over identifier references starting at root. Same-package function/const/type declarations referenced (transitively) are returned in discovery order. External package names are collected separately and deduplicated by imported *types.Package.

Package-level vars are rejected with a positioned error to keep WASM bodies pure (no hidden mutable shared state pulled into the generated main).

func ExtractUsedImports

func ExtractUsedImports(pkg *packages.Package, root ast.Node) ([]*ast.ImportSpec, error)

ExtractUsedImports returns the import specs in pkg that are referenced from the AST subtree rooted at root. Imports are deduplicated by path; aliases from the original spec are preserved.

func FormatNode

func FormatNode(node ast.Node, fset *token.FileSet) (string, error)

FormatNode renders an AST node back to Go source. For *ast.BlockStmt the outer braces are stripped so the caller can splice the inner statements directly into another body.

func StripPackageAndImports

func StripPackageAndImports(src string) (string, error)

StripPackageAndImports parses src as a Go file and returns the formatted source with the package clause and all import declarations removed. Doc comments on remaining declarations are preserved.

Types

type Entry

type Entry struct {
	Pkg  *packages.Package
	File *ast.File
}

Entry pairs a loaded package with a single file's syntax tree.

type Loader

type Loader struct {
	Fset *token.FileSet
	// contains filtered or unexported fields
}

Loader is a one-shot loader over a directory's Go packages, indexed by absolute file path and by package path.

func NewLoader

func NewLoader(dir string) (*Loader, error)

NewLoader loads "./..." rooted at dir using go/packages and indexes every compiled Go file by its absolute path.

func (*Loader) ByPath added in v3.6.0

func (l *Loader) ByPath(p string) *packages.Package

ByPath returns the loaded package for the given package path (PkgPath), or nil when the path is not in the loaded set. Without NeedDeps, packages reachable through pkg.Imports carry only an ID, so callers needing GoFiles or Module must look them up here.

func (*Loader) Get

func (l *Loader) Get(absPath string) (Entry, error)

Get returns the loaded Entry for an absolute file path.

type PageConfigResult

type PageConfigResult struct {
	Body        *ast.BlockStmt
	Compression string
	Compiler    string
	// Multiplexed reflects the RouteConfig.Multiplexed bool field. When true the
	// generated main() registers the ClientSideState body via GothicRegisterScope
	// so one instance serves every placement of the component (opt-in).
	Multiplexed bool
}

PageConfigResult holds extracted data from a RouteConfig composite literal (router.RouteConfig in a real project; routes.RouteConfig in the astx testdata).

func ExtractClientSideStateBody

func ExtractClientSideStateBody(entry Entry) (PageConfigResult, bool, error)

ExtractClientSideStateBody scans entry.File for a composite literal whose type (per TypesInfo) is a RouteConfig — router.RouteConfig in a real project (github.com/gothicframework/core/router) or routes.RouteConfig in the astx testdata fixtures — and which contains a ClientSideState key. It supports either an inline FuncLit value or a named function ident (resolved through TypesInfo.Uses → its FuncDecl).

It also extracts the WasmCompression field value (as a plain identifier name like "BROTLI" or "GZIP", or a string literal if written as such) when present.

Returns (result, true, nil) when found, (zero, false, nil) when no such literal exists, and a *PosError when the literal is malformed.

type PosError

type PosError struct {
	Pos token.Position
	Msg string
}

PosError is a positioned error from AST extraction.

func (*PosError) Error

func (e *PosError) Error() string

Jump to

Keyboard shortcuts

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