Skip to content

Commit e8dd5e5

Browse files
committed
feat: add enabled formatters to enabled linters
1 parent 521fdc3 commit e8dd5e5

File tree

2 files changed

+105
-57
lines changed

2 files changed

+105
-57
lines changed

pkg/lint/lintersdb/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (m *Manager) build(enabledByDefaultLinters []*linter.Config) map[string]*li
177177
}
178178
}
179179

180-
for _, name := range m.cfg.Linters.Enable {
180+
for _, name := range slices.Concat(m.cfg.Linters.Enable, m.cfg.Formatters.Enable) {
181181
for _, lc := range m.GetLinterConfigs(name) {
182182
// it's important to use lc.Name() nor name because name can be alias
183183
resultLintersSet[lc.Name()] = lc

pkg/lint/lintersdb/manager_test.go

+104-56
Original file line numberDiff line numberDiff line change
@@ -65,96 +65,144 @@ func TestManager_GetOptimizedLinters(t *testing.T) {
6565
}
6666

6767
func TestManager_build(t *testing.T) {
68-
type cs struct {
69-
cfg config.Linters
70-
name string // test case name
71-
def []string // enabled by default linters
72-
exp []string // alphabetically ordered enabled linter names
73-
}
74-
7568
allMegacheckLinterNames := []string{"gosimple", "staticcheck", "unused"}
7669

77-
cases := []cs{
70+
testCases := []struct {
71+
desc string
72+
cfg *config.Config
73+
defaultSet []string // enabled by default linters
74+
expected []string // alphabetically ordered enabled linter names
75+
}{
76+
{
77+
desc: "disable all linters from megacheck",
78+
cfg: &config.Config{
79+
Linters: config.Linters{
80+
Disable: []string{"megacheck"},
81+
},
82+
},
83+
defaultSet: allMegacheckLinterNames,
84+
expected: []string{"typecheck"}, // all disabled
85+
},
7886
{
79-
cfg: config.Linters{
80-
Disable: []string{"megacheck"},
87+
desc: "disable only staticcheck",
88+
cfg: &config.Config{
89+
Linters: config.Linters{
90+
Disable: []string{"staticcheck"},
91+
},
8192
},
82-
name: "disable all linters from megacheck",
83-
def: allMegacheckLinterNames,
84-
exp: []string{"typecheck"}, // all disabled
93+
defaultSet: allMegacheckLinterNames,
94+
expected: []string{"gosimple", "typecheck", "unused"},
95+
},
96+
{
97+
desc: "don't merge into megacheck",
98+
defaultSet: allMegacheckLinterNames,
99+
expected: []string{"gosimple", "staticcheck", "typecheck", "unused"},
85100
},
86101
{
87-
cfg: config.Linters{
88-
Disable: []string{"staticcheck"},
102+
desc: "expand megacheck",
103+
cfg: &config.Config{
104+
Linters: config.Linters{
105+
Enable: []string{"megacheck"},
106+
},
89107
},
90-
name: "disable only staticcheck",
91-
def: allMegacheckLinterNames,
92-
exp: []string{"gosimple", "typecheck", "unused"},
108+
defaultSet: nil,
109+
expected: []string{"gosimple", "staticcheck", "typecheck", "unused"},
110+
},
111+
{
112+
desc: "don't disable anything",
113+
defaultSet: []string{"gofmt", "govet", "typecheck"},
114+
expected: []string{"gofmt", "govet", "typecheck"},
93115
},
94116
{
95-
name: "don't merge into megacheck",
96-
def: allMegacheckLinterNames,
97-
exp: []string{"gosimple", "staticcheck", "typecheck", "unused"},
117+
desc: "enable gosec by gas alias",
118+
cfg: &config.Config{
119+
Linters: config.Linters{
120+
Enable: []string{"gas"},
121+
},
122+
},
123+
expected: []string{"gosec", "typecheck"},
98124
},
99125
{
100-
name: "expand megacheck",
101-
cfg: config.Linters{
102-
Enable: []string{"megacheck"},
126+
desc: "enable gosec by primary name",
127+
cfg: &config.Config{
128+
Linters: config.Linters{
129+
Enable: []string{"gosec"},
130+
},
103131
},
104-
def: nil,
105-
exp: []string{"gosimple", "staticcheck", "typecheck", "unused"},
132+
expected: []string{"gosec", "typecheck"},
106133
},
107134
{
108-
name: "don't disable anything",
109-
def: []string{"gofmt", "govet", "typecheck"},
110-
exp: []string{"gofmt", "govet", "typecheck"},
135+
desc: "enable gosec by both names",
136+
cfg: &config.Config{
137+
Linters: config.Linters{
138+
Enable: []string{"gosec", "gas"},
139+
},
140+
},
141+
expected: []string{"gosec", "typecheck"},
111142
},
112143
{
113-
name: "enable gosec by gas alias",
114-
cfg: config.Linters{
115-
Enable: []string{"gas"},
144+
desc: "disable gosec by gas alias",
145+
cfg: &config.Config{
146+
Linters: config.Linters{
147+
Disable: []string{"gas"},
148+
},
116149
},
117-
exp: []string{"gosec", "typecheck"},
150+
defaultSet: []string{"gosec"},
151+
expected: []string{"typecheck"},
118152
},
119153
{
120-
name: "enable gosec by primary name",
121-
cfg: config.Linters{
122-
Enable: []string{"gosec"},
154+
desc: "disable gosec by primary name",
155+
cfg: &config.Config{
156+
Linters: config.Linters{
157+
Disable: []string{"gosec"},
158+
},
123159
},
124-
exp: []string{"gosec", "typecheck"},
160+
defaultSet: []string{"gosec"},
161+
expected: []string{"typecheck"},
125162
},
126163
{
127-
name: "enable gosec by both names",
128-
cfg: config.Linters{
129-
Enable: []string{"gosec", "gas"},
164+
desc: "linters and formatters",
165+
cfg: &config.Config{
166+
Linters: config.Linters{
167+
Enable: []string{"gosec"},
168+
},
169+
Formatters: config.Formatters{
170+
Enable: []string{"gofmt"},
171+
},
130172
},
131-
exp: []string{"gosec", "typecheck"},
173+
expected: []string{"gosec", "gofmt", "typecheck"},
132174
},
133175
{
134-
name: "disable gosec by gas alias",
135-
cfg: config.Linters{
136-
Disable: []string{"gas"},
176+
desc: "linters and formatters but linters configuration disables the formatter",
177+
cfg: &config.Config{
178+
Linters: config.Linters{
179+
Enable: []string{"gosec"},
180+
Disable: []string{"gofmt"},
181+
},
182+
Formatters: config.Formatters{
183+
Enable: []string{"gofmt"},
184+
},
137185
},
138-
def: []string{"gosec"},
139-
exp: []string{"typecheck"},
186+
expected: []string{"gosec", "typecheck"},
140187
},
141188
{
142-
name: "disable gosec by primary name",
143-
cfg: config.Linters{
144-
Disable: []string{"gosec"},
189+
desc: "only formatters",
190+
cfg: &config.Config{
191+
Formatters: config.Formatters{
192+
Enable: []string{"gofmt"},
193+
},
145194
},
146-
def: []string{"gosec"},
147-
exp: []string{"typecheck"},
195+
expected: []string{"gofmt", "typecheck"},
148196
},
149197
}
150198

151-
for _, c := range cases {
152-
t.Run(c.name, func(t *testing.T) {
153-
m, err := NewManager(logutils.NewStderrLog("skip"), &config.Config{Linters: c.cfg}, NewLinterBuilder())
199+
for _, test := range testCases {
200+
t.Run(test.desc, func(t *testing.T) {
201+
m, err := NewManager(logutils.NewStderrLog("skip"), test.cfg, NewLinterBuilder())
154202
require.NoError(t, err)
155203

156204
var defaultLinters []*linter.Config
157-
for _, ln := range c.def {
205+
for _, ln := range test.defaultSet {
158206
lcs := m.GetLinterConfigs(ln)
159207
assert.NotNil(t, lcs, ln)
160208
defaultLinters = append(defaultLinters, lcs...)
@@ -167,7 +215,7 @@ func TestManager_build(t *testing.T) {
167215
enabledLinters = append(enabledLinters, ln)
168216
}
169217

170-
assert.ElementsMatch(t, c.exp, enabledLinters)
218+
assert.ElementsMatch(t, test.expected, enabledLinters)
171219
})
172220
}
173221
}

0 commit comments

Comments
 (0)