1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-30 08:57:07 +02:00
revive/rule/context-as-argument.go

111 lines
2.8 KiB
Go
Raw Normal View History

2018-01-25 21:34:38 +02:00
package rule
import (
"fmt"
2018-01-25 21:34:38 +02:00
"go/ast"
"strings"
2022-04-10 09:06:59 +02:00
"sync"
2018-01-25 21:34:38 +02:00
"github.com/mgechev/revive/lint"
)
2018-05-27 06:28:31 +02:00
// ContextAsArgumentRule lints given else constructs.
type ContextAsArgumentRule struct {
allowTypesLUT map[string]struct{}
2022-04-10 09:06:59 +02:00
sync.Mutex
}
2018-01-25 21:34:38 +02:00
// Apply applies the rule to given file.
func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
2022-04-10 09:06:59 +02:00
r.Lock()
if r.allowTypesLUT == nil {
r.allowTypesLUT = getAllowTypesFromArguments(args)
}
2022-04-10 09:06:59 +02:00
r.Unlock()
var failures []lint.Failure
2022-04-10 09:06:59 +02:00
r.Lock()
2018-01-25 21:34:38 +02:00
walker := lintContextArguments{
allowTypesLUT: r.allowTypesLUT,
2018-01-25 21:34:38 +02:00
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
2022-04-10 09:06:59 +02:00
r.Unlock()
2018-01-25 21:34:38 +02:00
ast.Walk(walker, file.AST)
2018-01-25 21:34:38 +02:00
return failures
}
// Name returns the rule name.
2022-04-10 11:55:13 +02:00
func (*ContextAsArgumentRule) Name() string {
2018-05-27 06:28:31 +02:00
return "context-as-argument"
2018-01-25 21:34:38 +02:00
}
type lintContextArguments struct {
allowTypesLUT map[string]struct{}
onFailure func(lint.Failure)
2018-01-25 21:34:38 +02:00
}
func (w lintContextArguments) Visit(n ast.Node) ast.Visitor {
fn, ok := n.(*ast.FuncDecl)
if !ok || len(fn.Type.Params.List) <= 1 {
return w
}
fnArgs := fn.Type.Params.List
2018-01-25 21:34:38 +02:00
// A context.Context should be the first parameter of a function.
// Flag any that show up after the first.
isCtxStillAllowed := true
for _, arg := range fnArgs {
2021-05-26 00:09:05 +02:00
argIsCtx := isPkgDot(arg.Type, "context", "Context")
if argIsCtx && !isCtxStillAllowed {
2018-01-25 21:34:38 +02:00
w.onFailure(lint.Failure{
2021-05-26 00:09:05 +02:00
Node: arg,
2018-01-25 21:34:38 +02:00
Category: "arg-order",
Failure: "context.Context should be the first parameter of a function",
Confidence: 0.9,
})
break // only flag one
}
typeName := gofmt(arg.Type)
// a parameter of type context.Context is still allowed if the current arg type is in the LUT
_, isCtxStillAllowed = w.allowTypesLUT[typeName]
2018-01-25 21:34:38 +02:00
}
return nil // avoid visiting the function body
}
func getAllowTypesFromArguments(args lint.Arguments) map[string]struct{} {
allowTypesBefore := []string{}
if len(args) >= 1 {
argKV, ok := args[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0]))
}
for k, v := range argKV {
switch k {
case "allowTypesBefore":
typesBefore, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v))
}
allowTypesBefore = append(allowTypesBefore, strings.Split(typesBefore, ",")...)
default:
panic(fmt.Sprintf("Invalid argument to the context-as-argument rule. Unrecognized key %s", k))
}
}
}
result := make(map[string]struct{}, len(allowTypesBefore))
for _, v := range allowTypesBefore {
result[v] = struct{}{}
}
result["context.Context"] = struct{}{} // context.Context is always allowed before another context.Context
return result
2018-01-25 21:34:38 +02:00
}