Ejemplo n.º 1
0
//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.
func Example() {
	//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)
	}
}
Ejemplo n.º 2
0
func TestUnexport(t *testing.T) {
	_, fset, info := parsePackage(pkg+"/testrename", t)
	defs := gounexport.GetDefinitions(info, fset)
	unusedDefs := gounexport.FindUnusedDefinitions(pkg, defs, nil)

	renamesCount := make(map[string]int)
	renameFunc := func(file string, offset int, source string, target string) error {
		log.Printf("renaming [%s] at %d from [%s] to [%s]", file, offset, source, target)
		renamesCount[source] = renamesCount[source] + 1
		return nil
	}

	for _, d := range unusedDefs {
		err := gounexport.Unexport(d, defs, renameFunc)
		if d.SimpleName == "UnusedStructConflict" && err == nil {
			t.Error("expected conflict error for UnusedStructConflict")
		}
		if d.SimpleName == "UnusedVarConflict" && err == nil {
			t.Error("expected conflict error for UnusedVarConflict")
		}
	}

	assertRename(renamesCount, "UnusedField", 1, t)
	assertRename(renamesCount, "UnusedMethod", 1, t)
	assertRename(renamesCount, "UsedInPackageMethod", 2, t)
}
Ejemplo n.º 3
0
func getDefinitionsToHideWithExclusions(pkg string, expectedLen int, excludes []*regexp.Regexp, t *testing.T) []*gounexport.Definition {
	_, fset, info := parsePackage(pkg, t)
	defs := gounexport.GetDefinitions(info, fset)
	unusedDefs := gounexport.FindUnusedDefinitions(pkg, defs, excludes)

	if expectedLen > 0 && len(unusedDefs) != expectedLen {
		t.Errorf("expected %d unused exported definitions, but found %d", expectedLen, len(unusedDefs))
	}
	return unusedDefs
}
Ejemplo n.º 4
0
func getUnusedDefinitions(pkg string, excludes []*regexp.Regexp) (
	[]*gounexport.Definition, map[string]*gounexport.Definition, error) {
	info := types.Info{
		Types: make(map[ast.Expr]types.TypeAndValue),
		Defs:  make(map[*ast.Ident]types.Object),
		Uses:  make(map[*ast.Ident]types.Object),
	}
	_, fset, err := gounexport.ParsePackage(pkg, &info)
	if err != nil {
		return nil, nil, err
	}
	defs := gounexport.GetDefinitions(&info, fset)
	return gounexport.FindUnusedDefinitions(pkg, defs, excludes), defs, nil
}
Ejemplo n.º 5
0
func TestGetDefinitionsToHideThis(t *testing.T) {
	pkg := "github.com/dooman87/gounexport"

	regex, _ := regexp.Compile("Test*")
	excludes := []*regexp.Regexp{regex}

	_, fset, info := parsePackage(pkg, t)
	defs := gounexport.GetDefinitions(info, fset)
	unusedDefs := gounexport.FindUnusedDefinitions(pkg, defs, excludes)

	log.Print("<<<<<<<<<<<<<<<<<<<<<<<<<<<")
	for _, d := range unusedDefs {
		util.Info("DEFINITION %s", d.Name)
		util.Info("\t%s:%d:%d", d.File, d.Line, d.Col)
	}
	log.Print("<<<<<<<<<<<<<<<<<<<<<<<<<<<")

	if len(unusedDefs) != 22 {
		t.Errorf("expected %d unused exported definitions, but found %d", 22, len(unusedDefs))
	}
}