Exemple #1
0
// NewAutoImporter returns an AutoImporter visitor that generates patches to add missing
// import statements to the standard library.
//     auto := NewAutoImporter(patchable.File)
//     scopes.WalkFile(patchable.File, auto)
//     patchable.FprintPatched(os.Stdout, patchable.All(), auto.Patches)
func NewAutoImporter(file *ast.File) *AutoImporter {
	auto := &AutoImporter{patch.Patches{}, make(map[*ast.Ident]bool), make(map[string]bool), file.Name.End()}
	for _, imp := range file.Imports {
		auto.m[imports.GetNameOrGuess(imp)] = true
	}
	return auto
}
Exemple #2
0
func (v *Unused) ExitScope(scope *ast.Scope, node ast.Node, last bool) scopes.Visitor {
	for _, obj := range scope.Objects {
		if !v.Used[obj] {
			v.Visitor.UnusedObj(obj, node)
		}
	}
	if file, ok := node.(*ast.File); ok {
		for _, imp := range file.Imports {
			name := imports.GetNameOrGuess(imp)
			if !v.UsedImports[name] && !anonymousImport(imp.Name) && imp.Path.Value != `"C"` {
				v.Visitor.UnusedImport(imp)
			}
		}
	}
	return v
}