@@ -8,8 +8,12 @@ import (
88 "path/filepath"
99 "runtime"
1010 "runtime/debug"
11+ "sort"
12+ "strings"
1113 "sync"
1214
15+ "github.com/golangci/golangci-lint/pkg/config"
16+
1317 "github.com/go-lintpack/lintpack"
1418 "golang.org/x/tools/go/loader"
1519
@@ -27,22 +31,79 @@ func (Gocritic) Desc() string {
2731 return "The most opinionated Go source code linter"
2832}
2933
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+ }
3376
77+ func (lint Gocritic ) buildEnabledCheckers (lintCtx * linter.Context , lintpackCtx * lintpack.Context ) ([]* lintpack.Checker , error ) {
3478 s := lintCtx .Settings ().Gocritic
79+ allParams := s .GetLowercasedParams ()
3580
3681 var enabledCheckers []* lintpack.Checker
3782 for _ , info := range lintpack .GetCheckersInfo () {
3883 if ! s .IsCheckEnabled (info .Name ) {
3984 continue
4085 }
4186
87+ if err := lint .configureCheckerInfo (info , allParams ); err != nil {
88+ return nil , err
89+ }
90+
4291 c := lintpack .NewChecker (lintpackCtx , info )
4392 enabledCheckers = append (enabledCheckers , c )
4493 }
4594
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+
46107 issuesCh := make (chan result.Issue , 1024 )
47108 var panicErr error
48109 go func () {
0 commit comments