|
1 | 1 | package golinters
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
| 5 | + "unicode" |
| 6 | + |
4 | 7 | "golang.org/x/tools/go/analysis"
|
| 8 | + scconfig "honnef.co/go/tools/config" |
5 | 9 |
|
6 | 10 | "github.com/golangci/golangci-lint/pkg/config"
|
7 | 11 | "github.com/golangci/golangci-lint/pkg/logutils"
|
8 | 12 | )
|
9 | 13 |
|
10 | 14 | var debugf = logutils.Debug("megacheck")
|
11 | 15 |
|
12 |
| -func setupStaticCheckAnalyzers(m map[string]*analysis.Analyzer, settings *config.StaticCheckSettings) []*analysis.Analyzer { |
13 |
| - var ret []*analysis.Analyzer |
14 |
| - for _, v := range m { |
15 |
| - setAnalyzerGoVersion(v, settings) |
16 |
| - ret = append(ret, v) |
| 16 | +func getGoVersion(settings *config.StaticCheckSettings) string { |
| 17 | + var goVersion string |
| 18 | + if settings != nil { |
| 19 | + goVersion = settings.GoVersion |
| 20 | + } |
| 21 | + |
| 22 | + if goVersion != "" { |
| 23 | + return goVersion |
17 | 24 | }
|
18 |
| - return ret |
19 |
| -} |
20 | 25 |
|
21 |
| -func setAnalyzerGoVersion(a *analysis.Analyzer, settings *config.StaticCheckSettings) { |
22 | 26 | // TODO: uses "1.13" for backward compatibility, but in the future (v2) must be set by using build.Default.ReleaseTags like staticcheck.
|
23 |
| - goVersion := "1.13" |
24 |
| - if settings != nil && settings.GoVersion != "" { |
25 |
| - goVersion = settings.GoVersion |
| 27 | + return "1.13" |
| 28 | +} |
| 29 | + |
| 30 | +func setupStaticCheckAnalyzers(src map[string]*analysis.Analyzer, goVersion string, checks []string) []*analysis.Analyzer { |
| 31 | + var names []string |
| 32 | + for name := range src { |
| 33 | + names = append(names, name) |
| 34 | + } |
| 35 | + |
| 36 | + filter := filterAnalyzerNames(names, checks) |
| 37 | + |
| 38 | + var ret []*analysis.Analyzer |
| 39 | + for name, a := range src { |
| 40 | + if filter[name] { |
| 41 | + setAnalyzerGoVersion(a, goVersion) |
| 42 | + ret = append(ret, a) |
| 43 | + } |
26 | 44 | }
|
27 | 45 |
|
| 46 | + return ret |
| 47 | +} |
| 48 | + |
| 49 | +func setAnalyzerGoVersion(a *analysis.Analyzer, goVersion string) { |
28 | 50 | if v := a.Flags.Lookup("go"); v != nil {
|
29 | 51 | if err := v.Value.Set(goVersion); err != nil {
|
30 | 52 | debugf("Failed to set go version: %s", err)
|
31 | 53 | }
|
32 | 54 | }
|
33 | 55 | }
|
| 56 | + |
| 57 | +func staticCheckConfig(settings *config.StaticCheckSettings) *scconfig.Config { |
| 58 | + var cfg *scconfig.Config |
| 59 | + |
| 60 | + if settings == nil || !settings.HasConfiguration() { |
| 61 | + return &scconfig.Config{ |
| 62 | + Checks: []string{"*"}, // override for compatibility reason. Must drop in the next major version. |
| 63 | + Initialisms: scconfig.DefaultConfig.Initialisms, |
| 64 | + DotImportWhitelist: scconfig.DefaultConfig.DotImportWhitelist, |
| 65 | + HTTPStatusCodeWhitelist: scconfig.DefaultConfig.HTTPStatusCodeWhitelist, |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + cfg = &scconfig.Config{ |
| 70 | + Checks: settings.Checks, |
| 71 | + Initialisms: settings.Initialisms, |
| 72 | + DotImportWhitelist: settings.DotImportWhitelist, |
| 73 | + HTTPStatusCodeWhitelist: settings.HTTPStatusCodeWhitelist, |
| 74 | + } |
| 75 | + |
| 76 | + if len(cfg.Checks) == 0 { |
| 77 | + cfg.Checks = append(cfg.Checks, "*") // override for compatibility reason. Must drop in the next major version. |
| 78 | + } |
| 79 | + |
| 80 | + if len(cfg.Initialisms) == 0 { |
| 81 | + cfg.Initialisms = append(cfg.Initialisms, scconfig.DefaultConfig.Initialisms...) |
| 82 | + } |
| 83 | + |
| 84 | + if len(cfg.DotImportWhitelist) == 0 { |
| 85 | + cfg.DotImportWhitelist = append(cfg.DotImportWhitelist, scconfig.DefaultConfig.DotImportWhitelist...) |
| 86 | + } |
| 87 | + |
| 88 | + if len(cfg.HTTPStatusCodeWhitelist) == 0 { |
| 89 | + cfg.HTTPStatusCodeWhitelist = append(cfg.HTTPStatusCodeWhitelist, scconfig.DefaultConfig.HTTPStatusCodeWhitelist...) |
| 90 | + } |
| 91 | + |
| 92 | + cfg.Checks = normalizeList(cfg.Checks) |
| 93 | + cfg.Initialisms = normalizeList(cfg.Initialisms) |
| 94 | + cfg.DotImportWhitelist = normalizeList(cfg.DotImportWhitelist) |
| 95 | + cfg.HTTPStatusCodeWhitelist = normalizeList(cfg.HTTPStatusCodeWhitelist) |
| 96 | + |
| 97 | + return cfg |
| 98 | +} |
| 99 | + |
| 100 | +// https://github.com/dominikh/go-tools/blob/9bf17c0388a65710524ba04c2d821469e639fdc2/lintcmd/lint.go#L437-L477 |
| 101 | +// nolint // Keep the original source code. |
| 102 | +func filterAnalyzerNames(analyzers []string, checks []string) map[string]bool { |
| 103 | + allowedChecks := map[string]bool{} |
| 104 | + |
| 105 | + for _, check := range checks { |
| 106 | + b := true |
| 107 | + if len(check) > 1 && check[0] == '-' { |
| 108 | + b = false |
| 109 | + check = check[1:] |
| 110 | + } |
| 111 | + |
| 112 | + if check == "*" || check == "all" { |
| 113 | + // Match all |
| 114 | + for _, c := range analyzers { |
| 115 | + allowedChecks[c] = b |
| 116 | + } |
| 117 | + } else if strings.HasSuffix(check, "*") { |
| 118 | + // Glob |
| 119 | + prefix := check[:len(check)-1] |
| 120 | + isCat := strings.IndexFunc(prefix, func(r rune) bool { return unicode.IsNumber(r) }) == -1 |
| 121 | + |
| 122 | + for _, a := range analyzers { |
| 123 | + idx := strings.IndexFunc(a, func(r rune) bool { return unicode.IsNumber(r) }) |
| 124 | + if isCat { |
| 125 | + // Glob is S*, which should match S1000 but not SA1000 |
| 126 | + cat := a[:idx] |
| 127 | + if prefix == cat { |
| 128 | + allowedChecks[a] = b |
| 129 | + } |
| 130 | + } else { |
| 131 | + // Glob is S1* |
| 132 | + if strings.HasPrefix(a, prefix) { |
| 133 | + allowedChecks[a] = b |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } else { |
| 138 | + // Literal check name |
| 139 | + allowedChecks[check] = b |
| 140 | + } |
| 141 | + } |
| 142 | + return allowedChecks |
| 143 | +} |
| 144 | + |
| 145 | +// https://github.com/dominikh/go-tools/blob/9bf17c0388a65710524ba04c2d821469e639fdc2/config/config.go#L95-L116 |
| 146 | +func normalizeList(list []string) []string { |
| 147 | + if len(list) > 1 { |
| 148 | + nlist := make([]string, 0, len(list)) |
| 149 | + nlist = append(nlist, list[0]) |
| 150 | + for i, el := range list[1:] { |
| 151 | + if el != list[i] { |
| 152 | + nlist = append(nlist, el) |
| 153 | + } |
| 154 | + } |
| 155 | + list = nlist |
| 156 | + } |
| 157 | + |
| 158 | + for _, el := range list { |
| 159 | + if el == "inherit" { |
| 160 | + // This should never happen, because the default config |
| 161 | + // should not use "inherit" |
| 162 | + panic(`unresolved "inherit"`) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return list |
| 167 | +} |
0 commit comments