@@ -8,8 +8,12 @@ import (
8
8
"path/filepath"
9
9
"runtime"
10
10
"runtime/debug"
11
+ "sort"
12
+ "strings"
11
13
"sync"
12
14
15
+ "github.com/golangci/golangci-lint/pkg/config"
16
+
13
17
"github.com/go-lintpack/lintpack"
14
18
"golang.org/x/tools/go/loader"
15
19
@@ -27,22 +31,79 @@ func (Gocritic) Desc() string {
27
31
return "The most opinionated Go source code linter"
28
32
}
29
33
30
- func (lint Gocritic ) Run (ctx context.Context , lintCtx * linter.Context ) ([]result.Issue , error ) {
31
- sizes := types .SizesFor ("gc" , runtime .GOARCH )
32
- lintpackCtx := lintpack .NewContext (lintCtx .Program .Fset , sizes )
34
+ func (Gocritic ) normalizeCheckerInfoParams (info * lintpack.CheckerInfo ) lintpack.CheckerParams {
35
+ // lowercase info param keys here because golangci-lint's config parser lowercases all strings
36
+ ret := lintpack.CheckerParams {}
37
+ for k , v := range info .Params {
38
+ ret [strings .ToLower (k )] = v
39
+ }
40
+
41
+ return ret
42
+ }
43
+
44
+ func (lint Gocritic ) configureCheckerInfo (info * lintpack.CheckerInfo , allParams map [string ]config.GocriticCheckSettings ) error {
45
+ params := allParams [strings .ToLower (info .Name )]
46
+ if params == nil { // no config for this checker
47
+ return nil
48
+ }
49
+
50
+ infoParams := lint .normalizeCheckerInfoParams (info )
51
+ for k , p := range params {
52
+ v , ok := infoParams [k ]
53
+ if ok {
54
+ v .Value = p
55
+ continue
56
+ }
57
+
58
+ // param `k` isn't supported
59
+ if len (info .Params ) == 0 {
60
+ return fmt .Errorf ("checker %s config param %s doesn't exist: checker doesn't have params" ,
61
+ info .Name , k )
62
+ }
63
+
64
+ var supportedKeys []string
65
+ for sk := range info .Params {
66
+ supportedKeys = append (supportedKeys , sk )
67
+ }
68
+ sort .Strings (supportedKeys )
69
+
70
+ return fmt .Errorf ("checker %s config param %s doesn't exist, all existing: %s" ,
71
+ info .Name , k , supportedKeys )
72
+ }
73
+
74
+ return nil
75
+ }
33
76
77
+ func (lint Gocritic ) buildEnabledCheckers (lintCtx * linter.Context , lintpackCtx * lintpack.Context ) ([]* lintpack.Checker , error ) {
34
78
s := lintCtx .Settings ().Gocritic
79
+ allParams := s .GetLowercasedParams ()
35
80
36
81
var enabledCheckers []* lintpack.Checker
37
82
for _ , info := range lintpack .GetCheckersInfo () {
38
83
if ! s .IsCheckEnabled (info .Name ) {
39
84
continue
40
85
}
41
86
87
+ if err := lint .configureCheckerInfo (info , allParams ); err != nil {
88
+ return nil , err
89
+ }
90
+
42
91
c := lintpack .NewChecker (lintpackCtx , info )
43
92
enabledCheckers = append (enabledCheckers , c )
44
93
}
45
94
95
+ return enabledCheckers , nil
96
+ }
97
+
98
+ func (lint Gocritic ) Run (ctx context.Context , lintCtx * linter.Context ) ([]result.Issue , error ) {
99
+ sizes := types .SizesFor ("gc" , runtime .GOARCH )
100
+ lintpackCtx := lintpack .NewContext (lintCtx .Program .Fset , sizes )
101
+
102
+ enabledCheckers , err := lint .buildEnabledCheckers (lintCtx , lintpackCtx )
103
+ if err != nil {
104
+ return nil , err
105
+ }
106
+
46
107
issuesCh := make (chan result.Issue , 1024 )
47
108
var panicErr error
48
109
go func () {
0 commit comments