forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp_linters.go
156 lines (124 loc) · 3.53 KB
/
help_linters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package commands
import (
"encoding/json"
"fmt"
"maps"
"slices"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goformatters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type linterHelp struct {
Name string `json:"name"`
Desc string `json:"description"`
Groups []string `json:"groups"`
Fast bool `json:"fast"`
AutoFix bool `json:"autoFix"`
Deprecated bool `json:"deprecated"`
Since string `json:"since"`
OriginalURL string `json:"originalURL,omitempty"`
}
func newLinterHelp(lc *linter.Config) linterHelp {
groups := []string{config.GroupAll}
if !lc.IsSlowLinter() {
groups = append(groups, config.GroupFast)
}
return linterHelp{
Name: lc.Name(),
Desc: formatDescription(lc.Linter.Desc()),
Groups: slices.Concat(groups, slices.Collect(maps.Keys(lc.Groups))),
Fast: !lc.IsSlowLinter(),
AutoFix: lc.CanAutoFix,
Deprecated: lc.IsDeprecated(),
Since: lc.Since,
OriginalURL: lc.OriginalURL,
}
}
func (c *helpCommand) lintersPreRunE(_ *cobra.Command, _ []string) error {
// The command doesn't depend on the real configuration.
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), config.NewDefault(), lintersdb.NewLinterBuilder())
if err != nil {
return err
}
c.dbManager = dbManager
return nil
}
func (c *helpCommand) lintersExecute(_ *cobra.Command, _ []string) error {
if c.opts.JSON {
return c.lintersPrintJSON()
}
c.lintersPrint()
return nil
}
func (c *helpCommand) lintersPrintJSON() error {
var linters []linterHelp
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
if lc.Internal {
continue
}
if goformatters.IsFormatter(lc.Name()) {
continue
}
linters = append(linters, newLinterHelp(lc))
}
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(linters)
}
func (c *helpCommand) lintersPrint() {
var enabledLCs, disabledLCs []*linter.Config
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
if lc.Internal {
continue
}
if goformatters.IsFormatter(lc.Name()) {
continue
}
if lc.FromGroup(config.GroupStandard) {
enabledLCs = append(enabledLCs, lc)
} else {
disabledLCs = append(disabledLCs, lc)
}
}
color.Green("Enabled by default linters:\n")
printLinters(enabledLCs)
color.Red("\nDisabled by default linters:\n")
printLinters(disabledLCs)
}
func printLinters(lcs []*linter.Config) {
slices.SortFunc(lcs, func(a, b *linter.Config) int {
if a.IsDeprecated() && b.IsDeprecated() {
return strings.Compare(a.Name(), b.Name())
}
if a.IsDeprecated() {
return 1
}
if b.IsDeprecated() {
return -1
}
return strings.Compare(a.Name(), b.Name())
})
for _, lc := range lcs {
desc := formatDescription(lc.Linter.Desc())
deprecatedMark := ""
if lc.IsDeprecated() {
deprecatedMark = " [" + color.RedString("deprecated") + "]"
}
var capabilities []string
if !lc.IsSlowLinter() {
capabilities = append(capabilities, color.BlueString("fast"))
}
if lc.CanAutoFix {
capabilities = append(capabilities, color.GreenString("auto-fix"))
}
var capability string
if capabilities != nil {
capability = " [" + strings.Join(capabilities, ", ") + "]"
}
_, _ = fmt.Fprintf(logutils.StdOut, "%s%s: %s%s\n",
color.YellowString(lc.Name()), deprecatedMark, desc, capability)
}
}