nodes

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	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,
	}
)

MinifyNone is a predefined variable of type MinifyOptions that represents the option to not minify any part of the code. It is one of the four options available in the package nodes/internal/nodes/minify.go.

Functions

func Doc

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

Doc package provides functions for working with Go code documentation. The `HasDoc` function checks whether a node has any associated comments. The `Doc` function retrieves the comment text associated with a node. The `Identifier` function returns the identifier and export status of a node. The `Find` function finds the first node with a matching identifier in the AST rooted at the given node. The `FindT` function is a type-safe version of `Find`.

func Find

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

Find searches for a Go AST dst.Node with the given identifier in the root node and its children. If found, it returns the first matching node and true; otherwise, it returns nil and false.

func FindT

func FindT[Node dst.Node](identifier string, root dst.Node) (Node, bool)

FindT[Node dst.Node] searches for the first node in the AST rooted at root with an identifier that matches the given identifier, and returns it as a Node of type Node. If no such node is found, it returns nil and false.

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 [func] returns a boolean indicating whether a given Go AST node has any associated documentation comments. It takes the Decorations of the node as input.

func Identifier

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

Identifier function in the nodes package returns the identifier and export status of a Go AST node. It takes a dst.Node as input and returns the identifier string and a boolean indicating whether the identifier is exported or not.

func Minify

func Minify[Node dst.Node](node Node, opts MinifyOptions) Node

Minify is a function that takes a Node and a MinifyOptions as input and returns a Node. It removes unnecessary code from the given Node based on the options provided in MinifyOptions. MinifyOptions is a struct type that specifies which parts of the code to minify. There are four pre-defined MinifyOptions: MinifyNone, MinifyUnexported, MinifyExported, MinifyComments, and MinifyAll. The options include package comments, function comments, function bodies, and struct comments. The function also checks whether identifiers are exported or not before applying the patch.

Types

type MinifyOptions

type MinifyOptions struct {
	PackageComment bool
	FuncComment    bool
	FuncBody       bool
	StructComment  bool
	Exported       bool
}

MinifyOptions represents the options for minifying Go AST nodes. It includes fields for controlling the minification of package comments, function comments, function bodies, and struct comments, as well as whether to minify only exported identifiers. MinifyOptions also has a Minify method that takes a Go AST node and returns the minified version according to the options specified. The Minify function is a convenience wrapper around MinifyOptions.Minify that allows specifying the type of the input and output nodes.

func (MinifyOptions) Minify

func (opts MinifyOptions) Minify(node dst.Node) dst.Node

MinifyOptions.Minify [Node dst.Node](node Node, opts MinifyOptions) Node

Minify applies the minification options specified in the MinifyOptions struct to the provided AST node of type "Node" and returns the resulting node. The minification options include removing function bodies, comments, or struct comments based on the values of corresponding fields in the MinifyOptions struct. If a field is set to true, then that part of the code will be minified. The "Exported" field determines whether to apply the minification options to exported or unexported identifiers.

Jump to

Keyboard shortcuts

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