Documentation ¶
Overview ¶
Package gounexport provides functionality to unexport unused public symbols.
In detail, what you can do using this package:
* parse package and get package's definition
* get information about all definitions in packages
* get unused packages
* unexport unused definitions
For result Definition struct is used. It's also includes Definition.Usage array with all usages (internal and external) across the package.
Example ¶
ExampleGetUnusedDefitions shows how to use gounexport package to find all definition that not used in a package. As the result, all unused definitions will be printed in console.
package main import ( "go/ast" "go/types" "regexp" "github.com/dooman87/gounexport" "github.com/dooman87/gounexport/util" ) func main() { //package to check pkg := "github.com/dooman87/gounexport" //Regular expression to exclude //tests methods from the result. regex, _ := regexp.Compile("Test*") excludes := []*regexp.Regexp{regex} //Internal info structure that required for //ParsePackage call info := types.Info{ Types: make(map[ast.Expr]types.TypeAndValue), Defs: make(map[*ast.Ident]types.Object), Uses: make(map[*ast.Ident]types.Object), } //Parsing package to fill info struct and //get file set. _, fset, err := gounexport.ParsePackage(pkg, &info) if err != nil { util.Err("error while parsing package %v", err) } //Analyze info and extract all definitions with usages. defs := gounexport.GetDefinitions(&info, fset) //Find all definitions that not used unusedDefs := gounexport.FindUnusedDefinitions(pkg, defs, excludes) //Print all unused definition to stdout. for _, d := range unusedDefs { util.Info("DEFINITION %s", d.Name) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDefinitions ¶
GetDefinitions collects information about all (exported and unexported) definitions and adapt them to Definition structure. Returns map where key is full name (package + name) of symbol.
func ParsePackage ¶
ParsePackage parses package and filling info structure. It's filling info about all internal packages even if they are not imported in the root package.
func Unexport ¶
func Unexport(def *Definition, allDefs map[string]*Definition, renameFunc func(string, int, string, string) error) error
Unexport hides definition by changing first letter to lower case. It won't rename if there is already existing unexported symbol with the same name. renameFunc is a func that accepts four arguments: full path to file, offset in a file to replace, original string, string to replace. It will be called when renaming is possible.
Types ¶
type Definition ¶
type Definition struct { //Full file path for current defintion File string //Full name of the definition Name string //Simple name of the definition. We are using it //for renaming purposes. SimpleName string //List of interfaces that implemented current definition //It will be interfaces definitions for a type definition and // methods definition for a function Interfaces []*Definition //type of definition TypeOf reflect.Type //Number of line in the file where definition is declared Line int //Column in the file where definition is declared Col int //Offset in source file Offset int //True, if definition is exported Exported bool //Package of the definition Pkg *types.Package //List of usages of the definition Usages []*Usage }
Definition of symbol in package
func FindUnusedDefinitions ¶
func FindUnusedDefinitions(pkg string, defs map[string]*Definition, excludes []*regexp.Regexp) []*Definition
FindUnusedDefinitions returns list of definitions that could be moved to private e.g. renamed. Criteria for renaming: - Definition should be exported - Definition should be in target package - Definition is not implementing external interfaces - Definition is not used in external packages
Directories ¶
Path | Synopsis |
---|---|
Package cmd provides command line interface to gounexport tool.
|
Package cmd provides command line interface to gounexport tool. |
Package fs provides utility functions to work with sources files relate to standard golang workspace.
|
Package fs provides utility functions to work with sources files relate to standard golang workspace. |
Package importer provides implementation of go/type/importer.
|
Package importer provides implementation of go/type/importer. |
Package util provides simple wrapper on top of log package.
|
Package util provides simple wrapper on top of log package. |