@@ -17,8 +17,13 @@ type ContextAsArgumentRule struct {
17
17
}
18
18
19
19
// Apply applies the rule to given file.
20
- func (r * ContextAsArgumentRule ) Apply (file * lint.File , args lint.Arguments ) []lint.Failure {
21
- r .configureOnce .Do (func () { r .configure (args ) })
20
+ func (r * ContextAsArgumentRule ) Apply (file * lint.File , arguments lint.Arguments ) []lint.Failure {
21
+ var configureErr error
22
+ r .configureOnce .Do (func () { configureErr = r .configure (arguments ) })
23
+
24
+ if configureErr != nil {
25
+ return newInternalFailureError (configureErr )
26
+ }
22
27
23
28
var failures []lint.Failure
24
29
for _ , decl := range file .AST .Decls {
@@ -59,27 +64,32 @@ func (*ContextAsArgumentRule) Name() string {
59
64
return "context-as-argument"
60
65
}
61
66
62
- func (r * ContextAsArgumentRule ) configure (arguments lint.Arguments ) {
63
- r .allowTypes = r .getAllowTypesFromArguments (arguments )
67
+ func (r * ContextAsArgumentRule ) configure (arguments lint.Arguments ) error {
68
+ types , err := r .getAllowTypesFromArguments (arguments )
69
+ if err != nil {
70
+ return err
71
+ }
72
+ r .allowTypes = types
73
+ return nil
64
74
}
65
75
66
- func (r * ContextAsArgumentRule ) getAllowTypesFromArguments (args lint.Arguments ) map [string ]struct {} {
76
+ func (r * ContextAsArgumentRule ) getAllowTypesFromArguments (args lint.Arguments ) ( map [string ]struct {}, error ) {
67
77
allowTypesBefore := []string {}
68
78
if len (args ) >= 1 {
69
79
argKV , ok := args [0 ].(map [string ]any )
70
80
if ! ok {
71
- panic ( fmt .Sprintf ( "Invalid argument to the context-as-argument rule. Expecting a k,v map, got %T" , args [0 ]) )
81
+ return nil , fmt .Errorf ( "invalid argument to the context-as-argument rule. Expecting a k,v map, got %T" , args [0 ])
72
82
}
73
83
for k , v := range argKV {
74
84
switch k {
75
85
case "allowTypesBefore" :
76
86
typesBefore , ok := v .(string )
77
87
if ! ok {
78
- panic ( fmt .Sprintf ( "Invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T" , v ) )
88
+ return nil , fmt .Errorf ( "invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T" , v )
79
89
}
80
90
allowTypesBefore = append (allowTypesBefore , strings .Split (typesBefore , "," )... )
81
91
default :
82
- panic ( fmt .Sprintf ( "Invalid argument to the context-as-argument rule. Unrecognized key %s" , k ) )
92
+ return nil , fmt .Errorf ( "invalid argument to the context-as-argument rule. Unrecognized key %s" , k )
83
93
}
84
94
}
85
95
}
@@ -90,5 +100,5 @@ func (r *ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments)
90
100
}
91
101
92
102
result ["context.Context" ] = struct {}{} // context.Context is always allowed before another context.Context
93
- return result
103
+ return result , nil
94
104
}
0 commit comments