Documentation
¶
Overview ¶
Package astutil provides shared AST/type helper functions used by linters.
Index ¶
- func AsStringsMethodCall(pass *analysis.Pass, expr ast.Expr, methodName string) (*ast.CallExpr, bool)
- func BuildContainsFix(expr *ast.BinaryExpr, pkgText, sText, subText string, negated bool, ...) []analysis.SuggestedFix
- func CallQualifierText(fset *token.FileSet, call *ast.CallExpr) string
- func CalledOSFunc(pass *analysis.Pass, call *ast.CallExpr, allowedNames ...string) (*types.Func, bool)
- func ConstIntValue(pass *analysis.Pass, expr ast.Expr) (int64, bool)
- func ContextContextType(pass *analysis.Pass) types.Type
- func ContextParamName(pass *analysis.Pass, fn *ast.FuncType) (string, bool)
- func EnclosingFuncType(node ast.Node) *ast.FuncType
- func FlipComparisonOp(op token.Token) token.Token
- func ImportedAs(file *ast.File, info *types.Info, importPath string) (string, bool)
- func Inspector(pass *analysis.Pass) (*inspector.Inspector, error)
- func IsFmtErrorf(pass *analysis.Pass, call *ast.CallExpr) bool
- func IsGoOrDeferClosure(funcLitCur inspector.Cursor) bool
- func IsLocalObject(obj types.Object) bool
- func IsPkgSelector(pass *analysis.Pass, sel *ast.SelectorExpr, pkgPath string) bool
- func IsStringLiteral(expr ast.Expr) bool
- func NodeText(fset *token.FileSet, node ast.Node) string
- func QualifierShadowed(pkg *types.Package, pos token.Pos, name, importPath string) bool
- func RhsExprForIndex(rhs []ast.Expr, idx int) (ast.Expr, bool)
- func Root(pass *analysis.Pass) (inspector.Cursor, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsStringsMethodCall ¶ added in v0.82.10
func AsStringsMethodCall(pass *analysis.Pass, expr ast.Expr, methodName string) (*ast.CallExpr, bool)
AsStringsMethodCall returns the *ast.CallExpr if expr is a call to the named method on the "strings" package (e.g. "Index" or "Count").
func BuildContainsFix ¶ added in v0.82.10
func BuildContainsFix(expr *ast.BinaryExpr, pkgText, sText, subText string, negated bool, fixMessage string) []analysis.SuggestedFix
BuildContainsFix builds the suggested fix rewriting a comparison to strings.Contains. fixMessage is used as the SuggestedFix.Message field so callers can identify the rewritten function (e.g. "Index" vs "Count").
func CallQualifierText ¶ added in v0.82.10
CallQualifierText returns the source text of the package qualifier in a selector call such as pkg.Method(...). For example, for strings.Index(...) it returns "strings" (or the local alias when the import is aliased). Returns "" if call.Fun is not a *ast.SelectorExpr.
func CalledOSFunc ¶ added in v0.82.3
func CalledOSFunc(pass *analysis.Pass, call *ast.CallExpr, allowedNames ...string) (*types.Func, bool)
CalledOSFunc reports whether call resolves to a function in package os. If allowedNames are provided, the function name must match one of them.
func ConstIntValue ¶ added in v0.82.10
ConstIntValue returns the integer constant value of expr, if it is a constant integer.
func ContextContextType ¶ added in v0.82.3
ContextContextType returns the types.Type for context.Context, or nil if the context package is not imported.
func ContextParamName ¶ added in v0.82.3
ContextParamName returns the name of the first context.Context parameter in fn, and true, or "", false if none exists.
func EnclosingFuncType ¶ added in v0.82.3
EnclosingFuncType extracts a function type from a FuncDecl or FuncLit node.
func FlipComparisonOp ¶ added in v0.82.3
FlipComparisonOp returns the comparison operator with left and right operands swapped.
func ImportedAs ¶ added in v0.82.10
ImportedAs returns the local binding name for importPath in file along with whether the import exists. When the import has an explicit alias (Name != nil), the alias is returned. Otherwise info.Implicits is consulted to obtain the *types.PkgName that the type-checker created for the import; its Name() method returns the package's declared name, which may differ from the last path segment for versioned modules (e.g. "github.com/foo/v2" declares package "foo"). info may be nil as a fallback, in which case the last path segment is used. The special aliases "." and "_" are returned as-is for callers to handle. Import path literals are decoded with strconv.Unquote so both double-quoted and raw (backtick) spellings are matched correctly.
func Inspector ¶ added in v0.79.4
Inspector extracts the *inspector.Inspector from pass.ResultOf. It returns an error if the result has an unexpected type.
func IsFmtErrorf ¶
IsFmtErrorf reports whether call is a call to fmt.Errorf (including aliases).
func IsGoOrDeferClosure ¶ added in v0.82.3
IsGoOrDeferClosure reports whether the FuncLit at funcLitCur is the direct callee of a go or defer statement, handling parenthesized forms like defer (func(){})().
func IsLocalObject ¶
IsLocalObject reports whether obj is a local (non-package-scope) object.
func IsPkgSelector ¶ added in v0.80.5
IsPkgSelector reports whether sel is a selector on an imported package with the given import path.
func IsStringLiteral ¶
IsStringLiteral reports whether expr is a string literal.
func QualifierShadowed ¶ added in v0.82.10
QualifierShadowed reports whether name cannot safely be used as a qualifier for importPath at pos. It returns true when:
- a local variable or parameter named name is in scope at pos, or
- name is bound to a *types.PkgName for a different import path.
Either case means that emitting "name.Foo" at pos would not resolve to the intended package. Call this before emitting a fix that uses name as a package qualifier to ensure the qualifier resolves to the expected import and not to a local variable, a parameter, or an unrelated package import.
func RhsExprForIndex ¶
RhsExprForIndex returns the RHS expression mapped to idx when available. When rhs has a single expression, only idx==0 is considered mapped.
Types ¶
This section is empty.