2018-01-25 11:34:38 -08:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
2021-12-31 17:11:18 -08:00
|
|
|
"fmt"
|
2018-01-25 11:34:38 -08:00
|
|
|
"go/ast"
|
2021-12-31 17:11:18 -08:00
|
|
|
"strings"
|
2018-01-25 11:34:38 -08:00
|
|
|
|
2025-05-26 13:18:38 +02:00
|
|
|
"github.com/mgechev/revive/internal/astutils"
|
2018-01-25 11:34:38 -08:00
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// ContextAsArgumentRule suggests that `context.Context` should be the first argument of a function.
|
2021-12-31 17:11:18 -08:00
|
|
|
type ContextAsArgumentRule struct {
|
2024-12-04 20:54:09 +01:00
|
|
|
allowTypes map[string]struct{}
|
2021-12-31 17:11:18 -08:00
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2024-12-13 21:38:46 +01:00
|
|
|
func (r *ContextAsArgumentRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2021-12-31 17:11:18 -08:00
|
|
|
var failures []lint.Failure
|
2024-12-04 20:54:09 +01:00
|
|
|
for _, decl := range file.AST.Decls {
|
|
|
|
|
fn, ok := decl.(*ast.FuncDecl)
|
|
|
|
|
if !ok || len(fn.Type.Params.List) <= 1 {
|
|
|
|
|
continue // not a function or a function with less than 2 parameters
|
|
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
2024-12-04 20:54:09 +01:00
|
|
|
fnArgs := fn.Type.Params.List
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2025-05-26 13:18:38 +02:00
|
|
|
argIsCtx := astutils.IsPkgDotName(arg.Type, "context", "Context")
|
2024-12-04 20:54:09 +01:00
|
|
|
if argIsCtx && !isCtxStillAllowed {
|
|
|
|
|
failures = append(failures, lint.Failure{
|
|
|
|
|
Node: arg,
|
2025-01-18 13:16:19 +02:00
|
|
|
Category: lint.FailureCategoryArgOrder,
|
2024-12-04 20:54:09 +01:00
|
|
|
Failure: "context.Context should be the first parameter of a function",
|
|
|
|
|
Confidence: 0.9,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
break // only flag one
|
|
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
2025-05-26 13:18:38 +02:00
|
|
|
typeName := astutils.GoFmt(arg.Type)
|
2024-12-04 20:54:09 +01:00
|
|
|
// a parameter of type context.Context is still allowed if the current arg type is in the allow types LookUpTable
|
|
|
|
|
_, isCtxStillAllowed = r.allowTypes[typeName]
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
2024-12-04 20:54:09 +01:00
|
|
|
return failures
|
2024-11-15 13:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 11:34:38 -08:00
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*ContextAsArgumentRule) Name() string {
|
2018-05-26 21:28:31 -07:00
|
|
|
return "context-as-argument"
|
2018-01-25 11:34:38 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 21:38:46 +01:00
|
|
|
// Configure validates the rule configuration, and configures the rule accordingly.
|
|
|
|
|
//
|
|
|
|
|
// Configuration implements the [lint.ConfigurableRule] interface.
|
|
|
|
|
func (r *ContextAsArgumentRule) Configure(arguments lint.Arguments) error {
|
2024-12-11 19:35:58 +01:00
|
|
|
types, err := r.getAllowTypesFromArguments(arguments)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
r.allowTypes = types
|
|
|
|
|
return nil
|
2021-12-31 17:11:18 -08:00
|
|
|
}
|
|
|
|
|
|
2024-12-12 08:42:41 +01:00
|
|
|
func (*ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments) (map[string]struct{}, error) {
|
2021-12-31 17:11:18 -08:00
|
|
|
allowTypesBefore := []string{}
|
|
|
|
|
if len(args) >= 1 {
|
2023-09-24 08:44:02 +02:00
|
|
|
argKV, ok := args[0].(map[string]any)
|
2021-12-31 17:11:18 -08:00
|
|
|
if !ok {
|
2024-12-11 19:35:58 +01:00
|
|
|
return nil, fmt.Errorf("invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0])
|
2021-12-31 17:11:18 -08:00
|
|
|
}
|
|
|
|
|
for k, v := range argKV {
|
2025-03-28 01:34:20 -07:00
|
|
|
if !isRuleOption(k, "allowTypesBefore") {
|
2024-12-11 19:35:58 +01:00
|
|
|
return nil, fmt.Errorf("invalid argument to the context-as-argument rule. Unrecognized key %s", k)
|
2021-12-31 17:11:18 -08:00
|
|
|
}
|
2025-03-28 01:34:20 -07:00
|
|
|
typesBefore, ok := v.(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v)
|
|
|
|
|
}
|
|
|
|
|
allowTypesBefore = append(allowTypesBefore, strings.Split(typesBefore, ",")...)
|
2021-12-31 17:11:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2024-12-11 19:35:58 +01:00
|
|
|
return result, nil
|
2018-01-25 11:34:38 -08:00
|
|
|
}
|