stubmethods

package
v0.12.0-pre.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const Doc = `stub methods analyzer

This analyzer generates method stubs for concrete types
in order to implement a target interface`

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:             "stubmethods",
	Doc:              Doc,
	Requires:         []*analysis.Analyzer{inspect.Analyzer},
	Run:              run,
	RunDespiteErrors: true,
}

Functions

func RelativeToFiles

func RelativeToFiles(concPkg *types.Package, concFile *ast.File, ifaceImports []*ast.ImportSpec, missingImport func(name, path string)) types.Qualifier

RelativeToFiles returns a types.Qualifier that formats package names according to the import environments of the files that define the concrete type and the interface type. (Only the imports of the latter file are provided.)

This is similar to types.RelativeTo except if a file imports the package with a different name, then it will use it. And if the file does import the package but it is ignored, then it will return the original name. It also prefers package names in importEnv in case an import is missing from concFile but is present among importEnv.

Additionally, if missingImport is not nil, the function will be called whenever the concFile is presented with a package that is not imported. This is useful so that as types.TypeString is formatting a function signature, it is identifying packages that will need to be imported when stubbing an interface.

TODO(rfindley): investigate if this can be merged with source.Qualifier.

Types

type StubInfo

type StubInfo struct {
	// Interface is the interface that the client wants to implement.
	// When the interface is defined, the underlying object will be a TypeName.
	// Note that we keep track of types.Object instead of types.Type in order
	// to keep a reference to the declaring object's package and the ast file
	// in the case where the concrete type file requires a new import that happens to be renamed
	// in the interface file.
	// TODO(marwan-at-work): implement interface literals.
	Fset      *token.FileSet // the FileSet used to type-check the types below
	Interface *types.TypeName
	Concrete  *types.Named
	Pointer   bool
}

StubInfo represents a concrete type that wants to stub out an interface type

func GetStubInfo

func GetStubInfo(fset *token.FileSet, ti *types.Info, path []ast.Node, pos token.Pos) *StubInfo

GetStubInfo determines whether the "missing method error" can be used to deduced what the concrete and interface types are.

TODO(adonovan): this function (and its following 5 helpers) tries to deduce a pair of (concrete, interface) types that are related by an assignment, either explictly or through a return statement or function call. This is essentially what the refactor/satisfy does, more generally. Refactor to share logic, after auditing 'satisfy' for safety on ill-typed code.

Jump to

Keyboard shortcuts

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