package gocode

import "golazy.dev/lazycode/gocode"

Package gocode provides syntax-aware Go edits as lazycode operations.

For example, an installer can plan a blank import without changing the file on disk:

workspace, err := lazycode.Load(".", "init/app.go")
if err != nil {
	return err
}

_, err = workspace.Plan(
	gocode.Rewrite("init/app.go",
		func(_ *token.FileSet, file *ast.File) (bool, error) {
			return gocode.EnsureBlankImport(
				file,
				"example.com/addons/seo",
			)
		},
	),
)
if err != nil {
	return err
}
if result.Changed() {
	fmt.Println("init/app.go would change")
}

Rewrite parses the file with comments, applies the AST change, and formats changed output. EnsureFile plans a formatted generated Go sidecar.

Functions

func EnsureBlankImport

EnsureBlankImport adds importPath with the blank identifier.

func EnsureBlankImport(file *ast.File, importPath string) (bool, error)

func EnsureFile

EnsureFile formats source and creates or replaces name in memory. It is useful for generated Go sidecars and rejects syntactically invalid source.

func EnsureFile(name string, source []byte) lazycode.Operation

func EnsureImport

EnsureImport adds an ordinary import if it is absent and reports whether the syntax tree changed. Use EnsureNamedImport when validation errors must be distinguished from an unchanged file.

func EnsureImport(file *ast.File, importPath string) bool

func EnsureNamedImport

EnsureNamedImport adds importPath with name. An empty name creates an ordinary import; "_" and "." create blank and dot imports respectively.

func EnsureNamedImport(file *ast.File, name, importPath string) (bool, error)

func HasImport

HasImport reports whether file imports importPath with any import name.

func HasImport(file *ast.File, importPath string) bool

func ImportName

ImportName reports the explicit name for importPath. An empty name means a normal import whose package name is inferred by Go. The returned error rejects malformed or duplicate imports instead of choosing one silently.

func ImportName(file *ast.File, importPath string) (string, bool, error)

func Parse

Parse parses source as a Go file, preserving comments and skipping deprecated object resolution.

func Parse(name string, source []byte) (*token.FileSet, *ast.File, error)

func RemoveImport

RemoveImport removes every declaration of importPath and reports whether the syntax tree changed.

func RemoveImport(file *ast.File, importPath string) bool

func RewriteSource

RewriteSource applies rewrite, formats the syntax tree, and reports whether the formatted result differs from source.

func RewriteSource(name string, source []byte, rewrite RewriteFunc) ([]byte, bool, error)

func UsesSelector

UsesSelector reports whether file contains a selector rooted at packageName, such as seo.Configure.

func UsesSelector(file *ast.File, packageName string) bool

Types

type RewriteFunc

RewriteFunc mutates a parsed Go file and reports whether it changed.

type RewriteFunc func(*token.FileSet, *ast.File) (bool, error)