Documentation
¶
Index ¶
- Variables
- func CommentTarget(spec dst.Spec, outer dst.Node) dst.Node
- func Doc(n dst.Node, removeSlash bool) string
- func Find(identifier string, root dst.Node) (dst.Spec, dst.Node, bool)
- func FindFunc(identifier string, root dst.Node) (fn *dst.FuncDecl, found bool)
- func FindInterfaceMethod(identifier string, root dst.Node) (method *dst.Field, found bool)
- func FindType(identifier string, root dst.Node) (spec *dst.TypeSpec, decl *dst.GenDecl, found bool)
- func FindValue(identifier string, root dst.Node) (spec *dst.ValueSpec, decl *dst.GenDecl, found bool)
- func Format(node *dst.File) ([]byte, error)
- func Fprint(w io.Writer, node *dst.File) error
- func HasDoc(decs dst.Decorations) bool
- func Identifier(node dst.Node) (identifier string, exported bool)
- func IsExportedIdentifier(identifier string) bool
- func Minify[Node dst.Node](node Node, opts MinifyOptions) Node
- func MustParse[Code ~string | ~[]byte](code Code) dst.Node
- func Parse[Code ~string | ~[]byte](code Code) (*dst.File, error)
- func StripIdentifierPrefix(identifier string) string
- type MinifyOptions
Constants ¶
This section is empty.
Variables ¶
var ( // MinifyNone represents the default state where no minification options are // applied. MinifyNone MinifyOptions // MinifyUnexported specifies options to minify unexported function bodies and // comments as well as structure comments. MinifyUnexported = MinifyOptions{ FuncComment: true, FuncBody: true, StructComment: true, } // MinifyExported applies minification settings to exported Go code elements, // including comments and function bodies, while preserving the visibility of // these elements. MinifyExported = MinifyOptions{ FuncComment: true, FuncBody: true, StructComment: true, Exported: true, } // MinifyComments specifies options to remove comments from package // declarations, function declarations, and struct declarations for exported // identifiers. MinifyComments = MinifyOptions{ PackageComment: true, FuncComment: true, StructComment: true, Exported: true, } // MinifyAll represents the most aggressive reduction of a DST, stripping all // comments and function bodies, and applying these changes to both unexported // and exported entities. MinifyAll = MinifyOptions{ PackageComment: true, FuncComment: true, FuncBody: true, StructComment: true, Exported: true, } )
Functions ¶
func CommentTarget ¶ added in v0.0.4
CommentTarget determines the appropriate node for attaching a comment within a Go abstract syntax tree. It resolves between a specification and its outer declaration node to find where a comment should be associated, typically returning the node that represents the closest syntactic construct to which the comment applies. If no specific association is found, it defaults to using the provided outer node.
func Doc ¶
Doc extracts the leading comment from the specified node, concatenating all lines of the comment into a single string. If removeSlash is true, the "//" prefix is removed from each line of the comment before concatenation.
func Find ¶
Find locates a declaration within the abstract syntax tree of Go code given an identifier and the root node. It returns the associated specification, the parent node if applicable, and a boolean indicating whether the declaration was found. The identifier is expected to be prefixed with a domain specifying the type of declaration to search for, such as "func", "type", or "var". This function does not traverse nested scopes and is limited to top-level declarations or methods on top-level types.
func FindFunc ¶ added in v0.0.4
FindFunc locates a function declaration within the given root node of the abstract syntax tree that matches the specified identifier. It returns the function declaration if found and a boolean indicating whether the search was successful.
func FindInterfaceMethod ¶ added in v0.0.4
FindInterfaceMethod locates a method of a specified interface within the given abstract syntax tree node. It returns the method declaration as a *dst.Field and a boolean indicating whether the method was found. The identifier used to specify the method should be in the format "interfaceName.methodName". If the method is not found, the returned *dst.Field will be nil and the boolean will be false.
func FindType ¶ added in v0.0.4
FindType locates a type declaration within the abstract syntax tree of a Go source file, given its identifier and the root node of the tree. It returns the corresponding type specification, the enclosing general declaration if applicable, and a boolean indicating whether the type was found.
func FindValue ¶ added in v0.0.4
func FindValue(identifier string, root dst.Node) (spec *dst.ValueSpec, decl *dst.GenDecl, found bool)
FindValue locates a variable declaration within the abstract syntax tree rooted at the specified node, matching the provided identifier. It returns the corresponding value specification, the enclosing general declaration if present, and a boolean indicating whether the variable was found.
func Format ¶
Format formats a Go AST dst.File into a []byte. It uses the decorator package to print the AST with additional formatting such as indentation and comments.
func Fprint ¶
Fprint formats a *dst.File and writes the result to an io.Writer. It uses the decorator.Fprint function from the "github.com/dave/dst/decorator" package to format the AST nodes.
func HasDoc ¶
func HasDoc(decs dst.Decorations) bool
HasDoc reports whether the provided decorations contain any documentation comments.
func Identifier ¶
Identifier extracts the name and determines the export status of the given node. It returns an identifier string with a prefix indicating the kind of node, such as "func:", "type:", or "var:", along with a boolean indicating whether the identifier is exported.
func IsExportedIdentifier ¶ added in v0.0.4
IsExportedIdentifier determines whether the given identifier is exported in Go. An identifier is considered exported if it begins with an uppercase letter and is not the blank identifier "_". This function can be used to check if an identifier would be accessible from other packages based on Go's visibility rules.
func Minify ¶
func Minify[Node dst.Node](node Node, opts MinifyOptions) Node
Minify applies transformations to a given DST node based on the provided MinifyOptions, stripping parts like function bodies, comments, and optionally affecting exported identifiers only. It returns the transformed DST node.
func MustParse ¶ added in v0.0.4
MustParse parses the given code and returns the corresponding DST node. It panics if an error occurs during parsing. This convenience function is intended for use when the code is guaranteed to be valid and any parse error would be considered exceptional. The function accepts a type parameter that can be a string or a byte slice, representing the source code to parse. The returned DST node can then be used for further manipulation or analysis of the parsed code structure.
func Parse ¶ added in v0.0.4
Parse reads the given source code and constructs an abstract syntax tree for that code. It accepts a string or a byte slice as input and returns a pointer to a *dst.File representing the parsed code, along with any error encountered during parsing. The function also considers comments in the source code as part of the parsing process.
func StripIdentifierPrefix ¶ added in v0.0.4
StripIdentifierPrefix removes any prefix before a colon in the given identifier and returns the resulting string without the prefix. If no colon is present, it returns the original identifier unchanged.
Types ¶
type MinifyOptions ¶
type MinifyOptions struct { PackageComment bool FuncComment bool FuncBody bool StructComment bool Exported bool }
MinifyOptions represents a set of configurable behaviors to control the minification process of Go source code represented by the dst.Node structure. It allows specification of which parts of the code to minify, such as comments and function bodies, and whether to restrict minification to exported identifiers only. The zero value for MinifyOptions disables all minification features.
func (MinifyOptions) Minify ¶
func (opts MinifyOptions) Minify(node dst.Node) dst.Node
Minify applies the specified minification options to the given syntax tree node and returns a modified version of that node. The minification process may remove comments, function bodies, or alter the visibility of declarations based on the settings in the provided MinifyOptions. The function operates recursively on all applicable child nodes of the input node. It returns a new node with the modifications applied, while leaving the original node unchanged.