nodes

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 is a preconfigured set of MinifyOptions that minifies
	// function comments, function bodies, and struct comments for unexported
	// elements only.
	MinifyUnexported = MinifyOptions{
		FuncComment:   true,
		FuncBody:      true,
		StructComment: true,
	}

	// MinifyExported is a variable of MinifyOptions type that configures
	// minification to remove function comments, function bodies, and struct
	// comments for exported elements in the Go source code.
	MinifyExported = MinifyOptions{
		FuncComment:   true,
		FuncBody:      true,
		StructComment: true,
		Exported:      true,
	}

	// MinifyComments is a predefined configuration of MinifyOptions that minifies
	// package, function, and struct comments for both exported and unexported
	// elements.
	MinifyComments = MinifyOptions{
		PackageComment: true,
		FuncComment:    true,
		StructComment:  true,
		Exported:       true,
	}

	// MinifyAll is a variable of MinifyOptions type representing the most
	// aggressive minification configuration, which removes package comments,
	// function comments and bodies, and struct comments for both exported and
	// unexported elements in the input node.
	MinifyAll = MinifyOptions{
		PackageComment: true,
		FuncComment:    true,
		FuncBody:       true,
		StructComment:  true,
		Exported:       true,
	}
)

Functions

func CommentTarget added in v0.0.4

func CommentTarget(spec dst.Spec, outer dst.Node) dst.Node

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

func Doc(n dst.Node, removeSlash bool) string

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

func Find(identifier string, root dst.Node) (dst.Spec, dst.Node, bool)

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

func FindFunc(identifier string, root dst.Node) (fn *dst.FuncDecl, found bool)

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

func FindInterfaceMethod(identifier string, root dst.Node) (method *dst.Field, found bool)

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

func FindType(identifier string, root dst.Node) (spec *dst.TypeSpec, decl *dst.GenDecl, found bool)

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

func Format(node *dst.File) ([]byte, error)

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

func Fprint(w io.Writer, node *dst.File) error

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

func Identifier(node dst.Node) (identifier string, exported bool)

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

func IsExportedIdentifier(identifier string) bool

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

func MustParse[Code ~string | ~[]byte](code Code) dst.Node

MustParse parses the provided code into a dst.Node, panicking if an error occurs during parsing.

func Parse added in v0.0.4

func Parse[Code ~string | ~[]byte](code Code) (*dst.File, error)

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

func StripIdentifierPrefix(identifier string) string

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.

Jump to

Keyboard shortcuts

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