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 is a variable of MinifyOptions type that represents the default, // no-op configuration where no minification actions are performed on the input // node. MinifyNone MinifyOptions MinifyUnexported = MinifyOptions{ FuncComment: true, FuncBody: true, StructComment: true, } MinifyExported = MinifyOptions{ FuncComment: true, FuncBody: true, StructComment: true, Exported: true, } MinifyComments = MinifyOptions{ PackageComment: true, FuncComment: true, StructComment: true, Exported: true, } 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 to attach a comment to, given a specification and an outer node. If the spec is nil or if the spec is a single TypeSpec or ValueSpec within a GenDecl, the outer node is returned. Otherwise, the spec is returned.
func Doc ¶
Doc returns the documentation string associated with the given dst.Node. If removeSlash is true, leading slashes and spaces are removed from the documentation lines.
func Find ¶
Find searches for a specified identifier within a given root node, returning the associated Spec, Node, and a boolean indicating if the identifier was found. It supports finding functions, types, and variables.
func FindFunc ¶ added in v0.0.4
FindFunc searches for a function with the specified identifier within the provided root node and returns the found *dst.FuncDecl and a boolean indicating whether the function was found or not.
func FindInterfaceMethod ¶ added in v0.0.4
FindInterfaceMethod searches for a method with the specified identifier in interface types within a given root node. It returns the found method as a *dst.Field and a boolean indicating whether the method was found or not.
func FindType ¶ added in v0.0.4
FindType searches for a type declaration with the given identifier in the provided root node. It returns the found TypeSpec, the containing GenDecl, 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 searches for a value with the given identifier in the provided root node, returning the matching ValueSpec, its parent GenDecl, and a boolean indicating whether the value 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 checks if the provided decorations contain any documentation comments.
func Identifier ¶
Identifier returns the identifier string and a boolean indicating whether the identifier is exported or not for the given node. The returned identifier is in the format "kind:name", where kind can be "func", "type", or "var".
func IsExportedIdentifier ¶ added in v0.0.4
IsExportedIdentifier checks if the given identifier string represents an exported identifier (i.e., starts with an uppercase letter) and returns true if it does, otherwise false. It also handles identifiers with a package prefix or type/method separator.
func Minify ¶
func Minify[Node dst.Node](node Node, opts MinifyOptions) Node
Minify applies the specified MinifyOptions to the given dst.Node, removing elements such as comments and function bodies to reduce its size. The resulting node is a clone of the original with the specified changes applied.
func MustParse ¶ added in v0.0.4
MustParse parses the provided code into a dst.Node, panicking if an error occurs during parsing.
func Parse ¶ added in v0.0.4
Parse takes a code input and returns a parsed *dst.File along with any error encountered. The input code can be either a string or a byte slice. The function uses decorator.ParseFile to parse the code with ParseComments and SkipObjectResolution flags.
func StripIdentifierPrefix ¶ added in v0.0.4
StripIdentifierPrefix removes the prefix (the part before the colon) from an identifier string, if present, and returns the remaining identifier.
Types ¶
type MinifyOptions ¶
type MinifyOptions struct { PackageComment bool FuncComment bool FuncBody bool StructComment bool Exported bool }
MinifyOptions is a configuration struct that sets options for minifying the different parts of a Go source code file, such as package comments, function comments and bodies, and struct comments. It can also be configured to only minify exported or unexported elements.
func (MinifyOptions) Minify ¶
func (opts MinifyOptions) Minify(node dst.Node) dst.Node
Minify applies the specified MinifyOptions to the given dst.Node, removing comments, function bodies, and/or struct comments based on the options set. If Exported is true, only exported nodes are affected; otherwise, all nodes are affected. The modified node is returned.