gounexport

package module
v0.0.0-...-ff11b4d Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2015 License: MIT Imports: 10 Imported by: 0

README

Gounexport

Gounexport is a tool for finding exported symbols that is not used outside of the package and unexporting them by renaming to lowercase.

By default, it's working in safe mode and only printing out result without renaming. Use -rename option to do actual renaming.

Usage: gounexport [OPTIONS] package
  -exclude string
        File with exlude patterns for objects that shouldn't be unexported. Each pattern should be started at new line. Default pattern is Test* to exclude tests methods.
  -out string
        Output file. If not set then stdout will be used
  -rename
        If set, then all defenitions that will be determined as unused will be renamed in files
  -verbose
        Turning on verbose mode

History

The app was originally developed as part of fifth golang-challenge. Surprisingly, I found that the solution is doing the job quite sufficient. However, the code was not looking good and required a lot of refactoring, cause it was my first go app and I was running out of time. Finally, I did it and proud of it :)

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

func GetDefinitions(info *types.Info, fset *token.FileSet) map[string]*Definition

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

func ParsePackage(pkgName string, info *types.Info) (*types.Package, *token.FileSet, error)

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

type Usage

type Usage struct {
	//Pos is a position of usage: file, line, col
	Pos token.Position
}

Usage is a struct that define one usage of a definition

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.

Jump to

Keyboard shortcuts

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