Documentation ¶
Index ¶
- Constants
- Variables
- func DiagnosticForError(fset *token.FileSet, file *ast.File, start, end token.Pos, msg string, ...) (analysis.Diagnostic, bool)
- func MatchesMessage(msg string) bool
- func RelativeToFiles(concPkg *types.Package, concFile *ast.File, ifaceImports []*ast.ImportSpec, ...) types.Qualifier
- type StubInfo
Constants ¶
const Doc = `stub methods analyzer
This analyzer generates method stubs for concrete types
in order to implement a target interface`
Variables ¶
Functions ¶
func DiagnosticForError ¶ added in v0.13.0
func DiagnosticForError(fset *token.FileSet, file *ast.File, start, end token.Pos, msg string, info *types.Info) (analysis.Diagnostic, bool)
DiagnosticForError computes a diagnostic suggesting to implement an interface to fix the type checking error defined by (start, end, msg).
If no such fix is possible, the second result is false.
TODO(rfindley): simplify this signature once the stubmethods refactoring is no longer wedged into the analysis framework.
func MatchesMessage ¶ added in v0.13.0
MatchesMessage reports whether msg matches the error message sought after by the stubmethods fix.
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 ¶
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.