package stubmethods

import "golang.org/x/tools/internal/lsp/analysis/stubmethods"

Index

Constants

const Doc = `stub methods analyzer

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

Variables

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, ifaceFile *ast.File, missingImport func(name, path string)) types.Qualifier

RelativeToFiles returns a types.Qualifier that formats package names according to the files where the concrete and interface types are defined.

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 ifaceFile in case an import is missing from concFile but is present in ifaceFile.

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.

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.
	Interface types.Object
	Concrete  *types.Named
	Pointer   bool
}

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

func GetStubInfo

func GetStubInfo(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.

Source Files

stubmethods.go

Version
v0.1.10
Published
Mar 15, 2022
Platform
js/wasm
Imports
13 packages
Last checked
6 hours ago

Tools for package owners.