lintutil

package
v0.0.0-...-ab0e469 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package lintutil contains utilities for writing linters.

For general documentation on linting, see dev/linters/README.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallsSuper

func CallsSuper(funcDecl *ast.FuncDecl, typesInfo *types.Info) bool

CallsSuper returns whether the given function body (which must be a receiver) calls its "super" -- that is, <receiver-var>.<superclass-name>.<receiver-name>().

This doesn't validate what the superclass name is; in the general case it's not clear what the correct rule would be as it's not necessarily the name of any field of the receiver-type (it may be a field of an intermediate embedded type). We just validate that you call some method of a field of your receiver, which has the same name; in practice you probably only have one option.

func FilterFuncs

func FilterFuncs(files []*ast.File, predicate func(*ast.FuncDecl) bool) []*ast.FuncDecl

FilterFuncs returns the toplevel functions in the given files that match the predicate.

func IsResolverFunc

func IsResolverFunc(funcDecl *ast.FuncDecl, typesInfo *types.Info) bool

Says whether the given function is a graphql resolver. A surprising number of linters want to special case graphql resolvers, which have a format dictated by gqlgen and thus may not follow our linting rules. This helps with that.

The conditions we use are: 1) having receiver whose name ends with "Resolver" 2) is exported 3a) either has a `context.Context` as the first argument (for resolvers) 3b) or returns an object whose name ends with Resolver (for federation)

func NameOf

func NameOf(obj types.Object) string

NameOf takes a node and returns the name of the symbol to which it refers, if any, in the form "package/path.UnqualifiedName". For built-in calls (such as `println()`) it uses a package name of "builtin" (so `builtin.println`).

This will return a name for functions (including builtin), types, package-vars, consts, and not necessarily other nodes like struct fields. If it can't determine the name, it returns "".

Note that methods have names like "(package/path.Interface).Method" or "(*package/path.Struct).Method".

func ObjectFor

func ObjectFor(node ast.Node, typesInfo *types.Info) types.Object

ObjectFor takes an AST node, and returns the corresponding types.Object, if there is one.

func ReceiversByType

func ReceiversByType(files []*ast.File, typesInfo *types.Info) map[types.Type][]*ast.FuncDecl

ReceiversByType returns all the receivers in the file, in a map by type.

Note that it may be more efficient to export this as an analyzer-result.

func TypeIs

func TypeIs(typ types.Type, pkgPath string, name string) bool

TypeIs takes a type object, and returns true if it is the given named type.

Returns false if pkgPath.name is not a type, or if it is not this type. Note that this includes cases where this type wraps pkgPath.name, or where they share an underlying type: this will only return true if the types are the same. Predeclared types will match the empty path.

func UnwrapMaybePointer

func UnwrapMaybePointer(typ types.Type) types.Type

UnwrapMaybePointer returns T if passed any of T, *T, **T, etc.

Types

This section is empty.

Jump to

Keyboard shortcuts

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