astmod

package
v0.0.0-...-452f322 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 9 Imported by: 0

README

astmod

Build Status GoDoc

Go AST manipulation library to rewrite import statement in the AST (in-place). Also allows listing the imports matching the given prefix.

Documentation

Overview

Astmod is a Go AST manipulation library to rewrite import statement in the AST (in-place). It also allows listing the imports matching the given prefix.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListImportPaths

func ListImportPaths(fname string, src interface{}, prefix string) ([]string, error)

ListImportPaths returns a slice of strings for the unique imports paths matching the given prefix.

func Rewrite

func Rewrite(fname string, src interface{}, fromPrefix, toPrefix string) (buf *bytes.Buffer, err error)

Rewrite modifies the AST to rewrite import statements. src should be compatible with go/parser/#ParseFile: (The type of the argument for the src parameter must be string, []byte, or io.Reader.)

return of nil, nil (no result, no error) means no changes are needed

func RewriteImports

func RewriteImports(f *ast.File, fromPrefix, toPrefix string) (changed bool, err error)

RewriteImports rewrites imports in the passed AST (in-place). It returns bool changed set to true if any changes were made and non-nil err on error

Types

type ImportSpec

type ImportSpec struct {
	Name, Path string
}

ImportSpec is a slimmed-down version of https://golang.org/pkg/go/ast/#ImportSpec.

func ListImports

func ListImports(fname string, src interface{}, prefix string) ([]*ImportSpec, error)

ListImports returns a set of ImportSpec objects with imports matching the given prefix.

func (*ImportSpec) String

func (i *ImportSpec) String() string

String prints the import with the name field prepended (if not empty).

Jump to

Keyboard shortcuts

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