Skip to content

Commit 100ed3c

Browse files
committed
chore: split wrapper and settingsWrapper
1 parent 6f6f105 commit 100ed3c

File tree

2 files changed

+329
-316
lines changed

2 files changed

+329
-316
lines changed

pkg/golinters/gocritic/gocritic.go

Lines changed: 0 additions & 316 deletions
Original file line numberDiff line numberDiff line change
@@ -227,312 +227,6 @@ func runOnFile(pass *analysis.Pass, f *ast.File, checks []*gocriticlinter.Checke
227227
}
228228
}
229229

230-
type goCriticChecks[T any] map[string]T
231-
232-
func (m goCriticChecks[T]) has(name string) bool {
233-
_, ok := m[name]
234-
return ok
235-
}
236-
237-
type settingsWrapper struct {
238-
*config.GoCriticSettings
239-
240-
logger logutils.Log
241-
242-
allCheckers []*gocriticlinter.CheckerInfo
243-
244-
allChecks goCriticChecks[struct{}]
245-
allChecksByTag goCriticChecks[[]string]
246-
allTagsSorted []string
247-
inferredEnabledChecks goCriticChecks[struct{}]
248-
249-
// *LowerCased fields are used for GoCriticSettings.SettingsPerCheck validation only.
250-
251-
allChecksLowerCased goCriticChecks[struct{}]
252-
inferredEnabledChecksLowerCased goCriticChecks[struct{}]
253-
}
254-
255-
func newSettingsWrapper(settings *config.GoCriticSettings, logger logutils.Log) *settingsWrapper {
256-
allCheckers := gocriticlinter.GetCheckersInfo()
257-
258-
allChecks := make(goCriticChecks[struct{}], len(allCheckers))
259-
allChecksLowerCased := make(goCriticChecks[struct{}], len(allCheckers))
260-
allChecksByTag := make(goCriticChecks[[]string])
261-
for _, checker := range allCheckers {
262-
allChecks[checker.Name] = struct{}{}
263-
allChecksLowerCased[strings.ToLower(checker.Name)] = struct{}{}
264-
265-
for _, tag := range checker.Tags {
266-
allChecksByTag[tag] = append(allChecksByTag[tag], checker.Name)
267-
}
268-
}
269-
270-
allTagsSorted := slices.Sorted(maps.Keys(allChecksByTag))
271-
272-
return &settingsWrapper{
273-
GoCriticSettings: settings,
274-
logger: logger,
275-
allCheckers: allCheckers,
276-
allChecks: allChecks,
277-
allChecksLowerCased: allChecksLowerCased,
278-
allChecksByTag: allChecksByTag,
279-
allTagsSorted: allTagsSorted,
280-
inferredEnabledChecks: make(goCriticChecks[struct{}]),
281-
inferredEnabledChecksLowerCased: make(goCriticChecks[struct{}]),
282-
}
283-
}
284-
285-
func (s *settingsWrapper) IsCheckEnabled(name string) bool {
286-
return s.inferredEnabledChecks.has(name)
287-
}
288-
289-
func (s *settingsWrapper) GetLowerCasedParams() map[string]config.GoCriticCheckSettings {
290-
return normalizeMap(s.SettingsPerCheck)
291-
}
292-
293-
// InferEnabledChecks tries to be consistent with (lintersdb.Manager).build.
294-
func (s *settingsWrapper) InferEnabledChecks() {
295-
s.debugChecksInitialState()
296-
297-
enabledByDefaultChecks, disabledByDefaultChecks := s.buildEnabledAndDisabledByDefaultChecks()
298-
299-
debugChecksListf(enabledByDefaultChecks, "Enabled by default")
300-
debugChecksListf(disabledByDefaultChecks, "Disabled by default")
301-
302-
enabledChecks := make(goCriticChecks[struct{}])
303-
304-
if s.EnableAll {
305-
enabledChecks = make(goCriticChecks[struct{}], len(s.allCheckers))
306-
for _, info := range s.allCheckers {
307-
enabledChecks[info.Name] = struct{}{}
308-
}
309-
} else if !s.DisableAll {
310-
// enable-all/disable-all revokes the default settings.
311-
enabledChecks = make(goCriticChecks[struct{}], len(enabledByDefaultChecks))
312-
for _, check := range enabledByDefaultChecks {
313-
enabledChecks[check] = struct{}{}
314-
}
315-
}
316-
317-
if len(s.EnabledTags) != 0 {
318-
enabledFromTags := s.expandTagsToChecks(s.EnabledTags)
319-
320-
debugChecksListf(enabledFromTags, "Enabled by config tags %s", s.EnabledTags)
321-
322-
for _, check := range enabledFromTags {
323-
enabledChecks[check] = struct{}{}
324-
}
325-
}
326-
327-
if len(s.EnabledChecks) != 0 {
328-
debugChecksListf(s.EnabledChecks, "Enabled by config")
329-
330-
for _, check := range s.EnabledChecks {
331-
if enabledChecks.has(check) {
332-
s.logger.Warnf("%s: no need to enable check %q: it's already enabled", linterName, check)
333-
continue
334-
}
335-
enabledChecks[check] = struct{}{}
336-
}
337-
}
338-
339-
if len(s.DisabledTags) != 0 {
340-
disabledFromTags := s.expandTagsToChecks(s.DisabledTags)
341-
342-
debugChecksListf(disabledFromTags, "Disabled by config tags %s", s.DisabledTags)
343-
344-
for _, check := range disabledFromTags {
345-
delete(enabledChecks, check)
346-
}
347-
}
348-
349-
if len(s.DisabledChecks) != 0 {
350-
debugChecksListf(s.DisabledChecks, "Disabled by config")
351-
352-
for _, check := range s.DisabledChecks {
353-
if !enabledChecks.has(check) {
354-
s.logger.Warnf("%s: no need to disable check %q: it's already disabled", linterName, check)
355-
continue
356-
}
357-
delete(enabledChecks, check)
358-
}
359-
}
360-
361-
s.inferredEnabledChecks = enabledChecks
362-
s.inferredEnabledChecksLowerCased = normalizeMap(s.inferredEnabledChecks)
363-
364-
s.debugChecksFinalState()
365-
}
366-
367-
func (s *settingsWrapper) buildEnabledAndDisabledByDefaultChecks() (enabled, disabled []string) {
368-
for _, info := range s.allCheckers {
369-
if enabledByDef := isEnabledByDefaultGoCriticChecker(info); enabledByDef {
370-
enabled = append(enabled, info.Name)
371-
} else {
372-
disabled = append(disabled, info.Name)
373-
}
374-
}
375-
return enabled, disabled
376-
}
377-
378-
func (s *settingsWrapper) expandTagsToChecks(tags []string) []string {
379-
var checks []string
380-
for _, tag := range tags {
381-
checks = append(checks, s.allChecksByTag[tag]...)
382-
}
383-
return checks
384-
}
385-
386-
func (s *settingsWrapper) debugChecksInitialState() {
387-
if !isDebug {
388-
return
389-
}
390-
391-
debugf("All gocritic existing tags and checks:")
392-
for _, tag := range s.allTagsSorted {
393-
debugChecksListf(s.allChecksByTag[tag], " tag %q", tag)
394-
}
395-
}
396-
397-
func (s *settingsWrapper) debugChecksFinalState() {
398-
if !isDebug {
399-
return
400-
}
401-
402-
var enabledChecks []string
403-
var disabledChecks []string
404-
405-
for _, checker := range s.allCheckers {
406-
check := checker.Name
407-
if s.inferredEnabledChecks.has(check) {
408-
enabledChecks = append(enabledChecks, check)
409-
} else {
410-
disabledChecks = append(disabledChecks, check)
411-
}
412-
}
413-
414-
debugChecksListf(enabledChecks, "Final used")
415-
416-
if len(disabledChecks) == 0 {
417-
debugf("All checks are enabled")
418-
} else {
419-
debugChecksListf(disabledChecks, "Final not used")
420-
}
421-
}
422-
423-
// Validate tries to be consistent with (lintersdb.Validator).validateEnabledDisabledLintersConfig.
424-
func (s *settingsWrapper) Validate() error {
425-
for _, v := range []func() error{
426-
s.validateOptionsCombinations,
427-
s.validateCheckerTags,
428-
s.validateCheckerNames,
429-
s.validateDisabledAndEnabledAtOneMoment,
430-
s.validateAtLeastOneCheckerEnabled,
431-
} {
432-
if err := v(); err != nil {
433-
return err
434-
}
435-
}
436-
return nil
437-
}
438-
439-
func (s *settingsWrapper) validateOptionsCombinations() error {
440-
if s.EnableAll {
441-
if s.DisableAll {
442-
return errors.New("enable-all and disable-all options must not be combined")
443-
}
444-
445-
if len(s.EnabledTags) != 0 {
446-
return errors.New("enable-all and enabled-tags options must not be combined")
447-
}
448-
449-
if len(s.EnabledChecks) != 0 {
450-
return errors.New("enable-all and enabled-checks options must not be combined")
451-
}
452-
}
453-
454-
if s.DisableAll {
455-
if len(s.DisabledTags) != 0 {
456-
return errors.New("disable-all and disabled-tags options must not be combined")
457-
}
458-
459-
if len(s.DisabledChecks) != 0 {
460-
return errors.New("disable-all and disabled-checks options must not be combined")
461-
}
462-
463-
if len(s.EnabledTags) == 0 && len(s.EnabledChecks) == 0 {
464-
return errors.New("all checks were disabled, but no one check was enabled: at least one must be enabled")
465-
}
466-
}
467-
468-
return nil
469-
}
470-
471-
func (s *settingsWrapper) validateCheckerTags() error {
472-
for _, tag := range s.EnabledTags {
473-
if !s.allChecksByTag.has(tag) {
474-
return fmt.Errorf("enabled tag %q doesn't exist, see %s's documentation", tag, linterName)
475-
}
476-
}
477-
478-
for _, tag := range s.DisabledTags {
479-
if !s.allChecksByTag.has(tag) {
480-
return fmt.Errorf("disabled tag %q doesn't exist, see %s's documentation", tag, linterName)
481-
}
482-
}
483-
484-
return nil
485-
}
486-
487-
func (s *settingsWrapper) validateCheckerNames() error {
488-
for _, check := range s.EnabledChecks {
489-
if !s.allChecks.has(check) {
490-
return fmt.Errorf("enabled check %q doesn't exist, see %s's documentation", check, linterName)
491-
}
492-
}
493-
494-
for _, check := range s.DisabledChecks {
495-
if !s.allChecks.has(check) {
496-
return fmt.Errorf("disabled check %q doesn't exist, see %s documentation", check, linterName)
497-
}
498-
}
499-
500-
for check := range s.SettingsPerCheck {
501-
lcName := strings.ToLower(check)
502-
if !s.allChecksLowerCased.has(lcName) {
503-
return fmt.Errorf("invalid check settings: check %q doesn't exist, see %s documentation", check, linterName)
504-
}
505-
if !s.inferredEnabledChecksLowerCased.has(lcName) {
506-
s.logger.Warnf("%s: settings were provided for disabled check %q", check, linterName)
507-
}
508-
}
509-
510-
return nil
511-
}
512-
513-
func (s *settingsWrapper) validateDisabledAndEnabledAtOneMoment() error {
514-
for _, tag := range s.DisabledTags {
515-
if slices.Contains(s.EnabledTags, tag) {
516-
return fmt.Errorf("tag %q disabled and enabled at one moment", tag)
517-
}
518-
}
519-
520-
for _, check := range s.DisabledChecks {
521-
if slices.Contains(s.EnabledChecks, check) {
522-
return fmt.Errorf("check %q disabled and enabled at one moment", check)
523-
}
524-
}
525-
526-
return nil
527-
}
528-
529-
func (s *settingsWrapper) validateAtLeastOneCheckerEnabled() error {
530-
if len(s.inferredEnabledChecks) == 0 {
531-
return errors.New("eventually all checks were disabled: at least one must be enabled")
532-
}
533-
return nil
534-
}
535-
536230
func normalizeMap[ValueT any](in map[string]ValueT) map[string]ValueT {
537231
ret := make(map[string]ValueT, len(in))
538232
for k, v := range in {
@@ -548,13 +242,3 @@ func isEnabledByDefaultGoCriticChecker(info *gocriticlinter.CheckerInfo) bool {
548242
!info.HasTag(gocriticlinter.PerformanceTag) &&
549243
!info.HasTag(gocriticlinter.SecurityTag)
550244
}
551-
552-
func debugChecksListf(checks []string, format string, args ...any) {
553-
if !isDebug {
554-
return
555-
}
556-
557-
v := slices.Sorted(slices.Values(checks))
558-
559-
debugf("%s checks (%d): %s", fmt.Sprintf(format, args...), len(checks), strings.Join(v, ", "))
560-
}

0 commit comments

Comments
 (0)