Skip to content

Commit 772f9ed

Browse files
committed
chore: clean
1 parent eb22628 commit 772f9ed

File tree

3 files changed

+287
-241
lines changed

3 files changed

+287
-241
lines changed

pkg/golinters/gocritic/gocritic.go

+6-82
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"fmt"
66
"go/ast"
77
"go/types"
8-
"maps"
9-
"reflect"
108
"runtime"
119
"slices"
1210
"strings"
@@ -57,21 +55,18 @@ Dynamic rules are written declaratively with AST patterns, filters, report messa
5755
nil,
5856
).
5957
WithContextSetter(func(context *linter.Context) {
60-
wrapper.replacer = replacer
61-
62-
wrapper.init(context.Log, settings)
58+
wrapper.init(context.Log, settings, replacer)
6359
}).
6460
WithLoadMode(goanalysis.LoadModeTypesInfo)
6561
}
6662

6763
type goCriticWrapper struct {
6864
settingsWrapper *settingsWrapper
69-
replacer *strings.Replacer
7065
sizes types.Sizes
7166
once sync.Once
7267
}
7368

74-
func (w *goCriticWrapper) init(logger logutils.Log, settings *config.GoCriticSettings) {
69+
func (w *goCriticWrapper) init(logger logutils.Log, settings *config.GoCriticSettings, replacer *strings.Replacer) {
7570
if settings == nil {
7671
return
7772
}
@@ -83,12 +78,9 @@ func (w *goCriticWrapper) init(logger logutils.Log, settings *config.GoCriticSet
8378
}
8479
})
8580

86-
settingsWrapper := newSettingsWrapper(settings, logger)
87-
settingsWrapper.InferEnabledChecks()
81+
settingsWrapper := newSettingsWrapper(logger, settings, replacer)
8882

89-
// Validate must be after InferEnabledChecks, not before.
90-
// Because it uses gathered information about tags set and finally enabled checks.
91-
if err := settingsWrapper.Validate(); err != nil {
83+
if err := settingsWrapper.Load(); err != nil {
9284
logger.Fatalf("%s: invalid settings: %s", linterName, err)
9385
}
9486

@@ -137,7 +129,8 @@ func (w *goCriticWrapper) buildEnabledCheckers(linterCtx *gocriticlinter.Context
137129
continue
138130
}
139131

140-
if err := w.configureCheckerInfo(info, allLowerCasedParams); err != nil {
132+
err := w.settingsWrapper.setCheckerParams(info, allLowerCasedParams)
133+
if err != nil {
141134
return nil, err
142135
}
143136

@@ -152,59 +145,6 @@ func (w *goCriticWrapper) buildEnabledCheckers(linterCtx *gocriticlinter.Context
152145
return enabledCheckers, nil
153146
}
154147

155-
func (w *goCriticWrapper) configureCheckerInfo(
156-
info *gocriticlinter.CheckerInfo,
157-
allLowerCasedParams map[string]config.GoCriticCheckSettings,
158-
) error {
159-
params := allLowerCasedParams[strings.ToLower(info.Name)]
160-
if params == nil { // no config for this checker
161-
return nil
162-
}
163-
164-
// To lowercase info param keys here because golangci-lint's config parser lowercases all strings.
165-
infoParams := normalizeMap(info.Params)
166-
for k, p := range params {
167-
v, ok := infoParams[k]
168-
if ok {
169-
v.Value = w.normalizeCheckerParamsValue(p)
170-
continue
171-
}
172-
173-
// param `k` isn't supported
174-
if len(info.Params) == 0 {
175-
return fmt.Errorf("checker %s config param %s doesn't exist: checker doesn't have params",
176-
info.Name, k)
177-
}
178-
179-
supportedKeys := slices.Sorted(maps.Keys(info.Params))
180-
181-
return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
182-
info.Name, k, supportedKeys)
183-
}
184-
185-
return nil
186-
}
187-
188-
// normalizeCheckerParamsValue normalizes value types.
189-
// go-critic asserts that CheckerParam.Value has some specific types,
190-
// but the file parsers (TOML, YAML, JSON) don't create the same representation for raw type.
191-
// then we have to convert value types into the expected value types.
192-
// Maybe in the future, this kind of conversion will be done in go-critic itself.
193-
func (w *goCriticWrapper) normalizeCheckerParamsValue(p any) any {
194-
rv := reflect.ValueOf(p)
195-
switch rv.Type().Kind() {
196-
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
197-
return int(rv.Int())
198-
case reflect.Bool:
199-
return rv.Bool()
200-
case reflect.String:
201-
// Perform variable substitution.
202-
return w.replacer.Replace(rv.String())
203-
default:
204-
return p
205-
}
206-
}
207-
208148
func runOnFile(pass *analysis.Pass, f *ast.File, checks []*gocriticlinter.Checker) {
209149
for _, c := range checks {
210150
// All checkers are expected to use *lint.Context
@@ -230,19 +170,3 @@ func runOnFile(pass *analysis.Pass, f *ast.File, checks []*gocriticlinter.Checke
230170
}
231171
}
232172
}
233-
234-
func normalizeMap[ValueT any](in map[string]ValueT) map[string]ValueT {
235-
ret := make(map[string]ValueT, len(in))
236-
for k, v := range in {
237-
ret[strings.ToLower(k)] = v
238-
}
239-
return ret
240-
}
241-
242-
func isEnabledByDefaultGoCriticChecker(info *gocriticlinter.CheckerInfo) bool {
243-
// https://github.com/go-critic/go-critic/blob/5b67cfd487ae9fe058b4b19321901b3131810f65/cmd/gocritic/check.go#L342-L345
244-
return !info.HasTag(gocriticlinter.ExperimentalTag) &&
245-
!info.HasTag(gocriticlinter.OpinionatedTag) &&
246-
!info.HasTag(gocriticlinter.PerformanceTag) &&
247-
!info.HasTag(gocriticlinter.SecurityTag)
248-
}

0 commit comments

Comments
 (0)