github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/lint/lintersdb/manager.go (about) 1 package lintersdb 2 3 import ( 4 "fmt" 5 "github.com/elek/golangci-lint/pkg/golinters/deferloop" 6 "github.com/elek/golangci-lint/pkg/golinters/errs" 7 "github.com/elek/golangci-lint/pkg/golinters/monkit" 8 "path/filepath" 9 "plugin" 10 11 "github.com/spf13/viper" 12 "golang.org/x/tools/go/analysis" 13 14 "github.com/elek/golangci-lint/pkg/config" 15 "github.com/elek/golangci-lint/pkg/golinters" 16 "github.com/elek/golangci-lint/pkg/golinters/goanalysis" 17 "github.com/elek/golangci-lint/pkg/lint/linter" 18 "github.com/elek/golangci-lint/pkg/logutils" 19 "github.com/elek/golangci-lint/pkg/report" 20 ) 21 22 type Manager struct { 23 nameToLCs map[string][]*linter.Config 24 cfg *config.Config 25 log logutils.Log 26 } 27 28 func NewManager(cfg *config.Config, log logutils.Log) *Manager { 29 m := &Manager{cfg: cfg, log: log} 30 nameToLCs := make(map[string][]*linter.Config) 31 for _, lc := range m.GetAllSupportedLinterConfigs() { 32 for _, name := range lc.AllNames() { 33 nameToLCs[name] = append(nameToLCs[name], lc) 34 } 35 } 36 37 m.nameToLCs = nameToLCs 38 return m 39 } 40 41 func (m *Manager) WithCustomLinters() *Manager { 42 if m.log == nil { 43 m.log = report.NewLogWrapper(logutils.NewStderrLog(""), &report.Data{}) 44 } 45 if m.cfg != nil { 46 for name, settings := range m.cfg.LintersSettings.Custom { 47 lc, err := m.loadCustomLinterConfig(name, settings) 48 49 if err != nil { 50 m.log.Errorf("Unable to load custom analyzer %s:%s, %v", 51 name, 52 settings.Path, 53 err) 54 } else { 55 m.nameToLCs[name] = append(m.nameToLCs[name], lc) 56 } 57 } 58 } 59 return m 60 } 61 62 func (Manager) AllPresets() []string { 63 return []string{ 64 linter.PresetBugs, 65 linter.PresetComment, 66 linter.PresetComplexity, 67 linter.PresetError, 68 linter.PresetFormatting, 69 linter.PresetImport, 70 linter.PresetMetaLinter, 71 linter.PresetModule, 72 linter.PresetPerformance, 73 linter.PresetSQL, 74 linter.PresetStyle, 75 linter.PresetTest, 76 linter.PresetUnused, 77 } 78 } 79 80 func (m Manager) allPresetsSet() map[string]bool { 81 ret := map[string]bool{} 82 for _, p := range m.AllPresets() { 83 ret[p] = true 84 } 85 return ret 86 } 87 88 func (m Manager) GetLinterConfigs(name string) []*linter.Config { 89 return m.nameToLCs[name] 90 } 91 92 func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) bool) []*linter.Config { 93 var ret []*linter.Config 94 for _, lc := range lcs { 95 lc := lc 96 lc.EnabledByDefault = isEnabled(lc) 97 ret = append(ret, lc) 98 } 99 100 return ret 101 } 102 103 //nolint:funlen 104 func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { 105 var govetCfg *config.GovetSettings 106 var testpackageCfg *config.TestpackageSettings 107 var exhaustiveCfg *config.ExhaustiveSettings 108 var exhaustiveStructCfg *config.ExhaustiveStructSettings 109 var errorlintCfg *config.ErrorLintSettings 110 var thelperCfg *config.ThelperSettings 111 var predeclaredCfg *config.PredeclaredSettings 112 var ifshortCfg *config.IfshortSettings 113 var reviveCfg *config.ReviveSettings 114 var cyclopCfg *config.Cyclop 115 var importAsCfg *config.ImportAsSettings 116 var goModDirectivesCfg *config.GoModDirectivesSettings 117 var tagliatelleCfg *config.TagliatelleSettings 118 var gosecCfg *config.GoSecSettings 119 var gosimpleCfg *config.StaticCheckSettings 120 var staticcheckCfg *config.StaticCheckSettings 121 var stylecheckCfg *config.StaticCheckSettings 122 var unusedCfg *config.StaticCheckSettings 123 var wrapcheckCfg *config.WrapcheckSettings 124 125 if m.cfg != nil { 126 govetCfg = &m.cfg.LintersSettings.Govet 127 testpackageCfg = &m.cfg.LintersSettings.Testpackage 128 exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive 129 exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct 130 errorlintCfg = &m.cfg.LintersSettings.ErrorLint 131 thelperCfg = &m.cfg.LintersSettings.Thelper 132 predeclaredCfg = &m.cfg.LintersSettings.Predeclared 133 ifshortCfg = &m.cfg.LintersSettings.Ifshort 134 reviveCfg = &m.cfg.LintersSettings.Revive 135 cyclopCfg = &m.cfg.LintersSettings.Cyclop 136 importAsCfg = &m.cfg.LintersSettings.ImportAs 137 goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives 138 tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle 139 gosecCfg = &m.cfg.LintersSettings.Gosec 140 gosimpleCfg = &m.cfg.LintersSettings.Gosimple 141 staticcheckCfg = &m.cfg.LintersSettings.Staticcheck 142 stylecheckCfg = &m.cfg.LintersSettings.Stylecheck 143 unusedCfg = &m.cfg.LintersSettings.Unused 144 wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck 145 } 146 147 const megacheckName = "megacheck" 148 149 lcs := []*linter.Config{ 150 linter.NewConfig(golinters.NewGovet(govetCfg)). 151 WithSince("v1.0.0"). 152 WithLoadForGoAnalysis(). 153 WithPresets(linter.PresetBugs, linter.PresetMetaLinter). 154 WithAlternativeNames("vet", "vetshadow"). 155 WithURL("https://golang.org/cmd/vet/"), 156 linter.NewConfig(golinters.NewBodyclose()). 157 WithSince("v1.18.0"). 158 WithLoadForGoAnalysis(). 159 WithPresets(linter.PresetPerformance, linter.PresetBugs). 160 WithURL("https://github.com/timakin/bodyclose"), 161 linter.NewConfig(golinters.NewNoctx()). 162 WithSince("v1.28.0"). 163 WithLoadForGoAnalysis(). 164 WithPresets(linter.PresetPerformance, linter.PresetBugs). 165 WithURL("https://github.com/sonatard/noctx"), 166 linter.NewConfig(golinters.NewErrcheck()). 167 WithSince("v1.0.0"). 168 WithLoadForGoAnalysis(). 169 WithPresets(linter.PresetBugs, linter.PresetError). 170 WithURL("https://github.com/kisielk/errcheck"), 171 linter.NewConfig(golinters.NewGolint()). 172 WithSince("v1.0.0"). 173 WithLoadForGoAnalysis(). 174 WithPresets(linter.PresetStyle). 175 WithURL("https://github.com/golang/lint"). 176 Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"), 177 linter.NewConfig(golinters.NewRowsErrCheck()). 178 WithSince("v1.23.0"). 179 WithLoadForGoAnalysis(). 180 WithPresets(linter.PresetBugs, linter.PresetSQL). 181 WithURL("https://github.com/jingyugao/rowserrcheck"), 182 183 linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)). 184 WithSince("v1.0.0"). 185 WithLoadForGoAnalysis(). 186 WithPresets(linter.PresetBugs, linter.PresetMetaLinter). 187 WithAlternativeNames(megacheckName). 188 WithURL("https://staticcheck.io/"), 189 linter.NewConfig(golinters.NewUnused(unusedCfg)). 190 WithSince("v1.20.0"). 191 WithLoadForGoAnalysis(). 192 WithPresets(linter.PresetUnused). 193 WithAlternativeNames(megacheckName). 194 ConsiderSlow(). 195 WithChangeTypes(). 196 WithURL("https://github.com/dominikh/go-tools/tree/master/unused"), 197 linter.NewConfig(golinters.NewGosimple(gosimpleCfg)). 198 WithSince("v1.20.0"). 199 WithLoadForGoAnalysis(). 200 WithPresets(linter.PresetStyle). 201 WithAlternativeNames(megacheckName). 202 WithURL("https://github.com/dominikh/go-tools/tree/master/simple"), 203 204 linter.NewConfig(golinters.NewStylecheck(stylecheckCfg)). 205 WithSince("v1.20.0"). 206 WithLoadForGoAnalysis(). 207 WithPresets(linter.PresetStyle). 208 WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"), 209 linter.NewConfig(golinters.NewGosec(gosecCfg)). 210 WithSince("v1.0.0"). 211 WithLoadForGoAnalysis(). 212 WithPresets(linter.PresetBugs). 213 WithURL("https://github.com/securego/gosec"). 214 WithAlternativeNames("gas"), 215 linter.NewConfig(golinters.NewStructcheck()). 216 WithSince("v1.0.0"). 217 WithLoadForGoAnalysis(). 218 WithPresets(linter.PresetUnused). 219 WithURL("https://github.com/opennota/check"), 220 linter.NewConfig(golinters.NewVarcheck()). 221 WithSince("v1.0.0"). 222 WithLoadForGoAnalysis(). 223 WithPresets(linter.PresetUnused). 224 WithURL("https://github.com/opennota/check"), 225 linter.NewConfig(golinters.NewInterfacer()). 226 WithSince("v1.0.0"). 227 WithLoadForGoAnalysis(). 228 WithPresets(linter.PresetStyle). 229 WithURL("https://github.com/mvdan/interfacer"). 230 Deprecated("The repository of the linter has been archived by the owner.", "v1.38.0", ""), 231 linter.NewConfig(golinters.NewUnconvert()). 232 WithSince("v1.0.0"). 233 WithLoadForGoAnalysis(). 234 WithPresets(linter.PresetStyle). 235 WithURL("https://github.com/mdempsky/unconvert"), 236 linter.NewConfig(golinters.NewIneffassign()). 237 WithSince("v1.0.0"). 238 WithPresets(linter.PresetUnused). 239 WithURL("https://github.com/gordonklaus/ineffassign"), 240 linter.NewConfig(golinters.NewDupl()). 241 WithSince("v1.0.0"). 242 WithPresets(linter.PresetStyle). 243 WithURL("https://github.com/mibk/dupl"), 244 linter.NewConfig(golinters.NewGoconst()). 245 WithSince("v1.0.0"). 246 WithPresets(linter.PresetStyle). 247 WithURL("https://github.com/jgautheron/goconst"), 248 linter.NewConfig(golinters.NewDeadcode()). 249 WithSince("v1.0.0"). 250 WithLoadForGoAnalysis(). 251 WithPresets(linter.PresetUnused). 252 WithURL("https://github.com/remyoudompheng/go-misc/tree/master/deadcode"), 253 linter.NewConfig(golinters.NewGocyclo()). 254 WithSince("v1.0.0"). 255 WithPresets(linter.PresetComplexity). 256 WithURL("https://github.com/fzipp/gocyclo"), 257 linter.NewConfig(golinters.NewCyclop(cyclopCfg)). 258 WithSince("v1.37.0"). 259 WithLoadForGoAnalysis(). 260 WithPresets(linter.PresetComplexity). 261 WithURL("https://github.com/bkielbasa/cyclop"), 262 linter.NewConfig(golinters.NewGocognit()). 263 WithSince("v1.20.0"). 264 WithPresets(linter.PresetComplexity). 265 WithURL("https://github.com/uudashr/gocognit"), 266 linter.NewConfig(golinters.NewTypecheck()). 267 WithSince("v1.3.0"). 268 WithLoadForGoAnalysis(). 269 WithPresets(linter.PresetBugs). 270 WithURL(""), 271 linter.NewConfig(golinters.NewAsciicheck()). 272 WithSince("v1.26.0"). 273 WithPresets(linter.PresetBugs, linter.PresetStyle). 274 WithURL("https://github.com/tdakkota/asciicheck"), 275 276 linter.NewConfig(golinters.NewGofmt()). 277 WithSince("v1.0.0"). 278 WithPresets(linter.PresetFormatting). 279 WithAutoFix(). 280 WithURL("https://golang.org/cmd/gofmt/"), 281 linter.NewConfig(golinters.NewGofumpt()). 282 WithSince("v1.28.0"). 283 WithPresets(linter.PresetFormatting). 284 WithAutoFix(). 285 WithURL("https://github.com/mvdan/gofumpt"), 286 linter.NewConfig(golinters.NewGoimports()). 287 WithSince("v1.20.0"). 288 WithPresets(linter.PresetFormatting, linter.PresetImport). 289 WithAutoFix(). 290 WithURL("https://godoc.org/golang.org/x/tools/cmd/goimports"), 291 linter.NewConfig(golinters.NewGoHeader()). 292 WithSince("v1.28.0"). 293 WithPresets(linter.PresetStyle). 294 WithURL("https://github.com/denis-tingajkin/go-header"), 295 linter.NewConfig(golinters.NewGci()). 296 WithSince("v1.30.0"). 297 WithPresets(linter.PresetFormatting, linter.PresetImport). 298 WithAutoFix(). 299 WithURL("https://github.com/daixiang0/gci"), 300 linter.NewConfig(golinters.NewMaligned()). 301 WithSince("v1.0.0"). 302 WithLoadForGoAnalysis(). 303 WithPresets(linter.PresetPerformance). 304 WithURL("https://github.com/mdempsky/maligned"). 305 Deprecated("The repository of the linter has been archived by the owner.", "v1.38.0", "govet 'fieldalignment'"), 306 linter.NewConfig(golinters.NewDepguard()). 307 WithSince("v1.4.0"). 308 WithLoadForGoAnalysis(). 309 WithPresets(linter.PresetStyle, linter.PresetImport, linter.PresetModule). 310 WithURL("https://github.com/OpenPeeDeeP/depguard"), 311 linter.NewConfig(golinters.NewMisspell()). 312 WithSince("v1.8.0"). 313 WithPresets(linter.PresetStyle, linter.PresetComment). 314 WithAutoFix(). 315 WithURL("https://github.com/client9/misspell"), 316 linter.NewConfig(golinters.NewLLL()). 317 WithSince("v1.8.0"). 318 WithPresets(linter.PresetStyle), 319 linter.NewConfig(golinters.NewUnparam()). 320 WithSince("v1.9.0"). 321 WithPresets(linter.PresetUnused). 322 WithLoadForGoAnalysis(). 323 WithURL("https://github.com/mvdan/unparam"), 324 linter.NewConfig(golinters.NewDogsled()). 325 WithSince("v1.19.0"). 326 WithPresets(linter.PresetStyle). 327 WithURL("https://github.com/alexkohler/dogsled"), 328 linter.NewConfig(golinters.NewNakedret()). 329 WithSince("v1.19.0"). 330 WithPresets(linter.PresetStyle). 331 WithURL("https://github.com/alexkohler/nakedret"), 332 linter.NewConfig(golinters.NewPrealloc()). 333 WithSince("v1.19.0"). 334 WithPresets(linter.PresetPerformance). 335 WithURL("https://github.com/alexkohler/prealloc"), 336 linter.NewConfig(golinters.NewScopelint()). 337 WithSince("v1.12.0"). 338 WithPresets(linter.PresetBugs). 339 WithURL("https://github.com/kyoh86/scopelint"). 340 Deprecated("The repository of the linter has been deprecated by the owner.", "v1.39.0", "exportloopref"), 341 linter.NewConfig(golinters.NewGocritic()). 342 WithSince("v1.12.0"). 343 WithPresets(linter.PresetStyle, linter.PresetMetaLinter). 344 WithLoadForGoAnalysis(). 345 WithURL("https://github.com/go-critic/go-critic"), 346 linter.NewConfig(golinters.NewGochecknoinits()). 347 WithSince("v1.12.0"). 348 WithPresets(linter.PresetStyle). 349 WithURL("https://github.com/leighmcculloch/gochecknoinits"), 350 linter.NewConfig(golinters.NewGochecknoglobals()). 351 WithSince("v1.12.0"). 352 WithPresets(linter.PresetStyle). 353 WithURL("https://github.com/leighmcculloch/gochecknoglobals"), 354 linter.NewConfig(golinters.NewGodox()). 355 WithSince("v1.19.0"). 356 WithPresets(linter.PresetStyle, linter.PresetComment). 357 WithURL("https://github.com/matoous/godox"), 358 linter.NewConfig(golinters.NewFunlen()). 359 WithSince("v1.18.0"). 360 WithPresets(linter.PresetComplexity). 361 WithURL("https://github.com/ultraware/funlen"), 362 linter.NewConfig(golinters.NewWhitespace()). 363 WithSince("v1.19.0"). 364 WithPresets(linter.PresetStyle). 365 WithAutoFix(). 366 WithURL("https://github.com/ultraware/whitespace"), 367 linter.NewConfig(golinters.NewWSL()). 368 WithSince("v1.20.0"). 369 WithPresets(linter.PresetStyle). 370 WithURL("https://github.com/bombsimon/wsl"), 371 linter.NewConfig(golinters.NewGoPrintfFuncName()). 372 WithSince("v1.23.0"). 373 WithPresets(linter.PresetStyle). 374 WithURL("https://github.com/jirfag/go-printf-func-name"), 375 linter.NewConfig(golinters.NewGoMND(m.cfg)). 376 WithSince("v1.22.0"). 377 WithPresets(linter.PresetStyle). 378 WithURL("https://github.com/tommy-muehle/go-mnd"), 379 linter.NewConfig(golinters.NewGoerr113()). 380 WithSince("v1.26.0"). 381 WithPresets(linter.PresetStyle, linter.PresetError). 382 WithLoadForGoAnalysis(). 383 WithURL("https://github.com/Djarvur/go-err113"), 384 linter.NewConfig(golinters.NewGomodguard()). 385 WithSince("v1.25.0"). 386 WithPresets(linter.PresetStyle, linter.PresetImport, linter.PresetModule). 387 WithURL("https://github.com/ryancurrah/gomodguard"), 388 linter.NewConfig(golinters.NewGodot()). 389 WithSince("v1.25.0"). 390 WithPresets(linter.PresetStyle, linter.PresetComment). 391 WithAutoFix(). 392 WithURL("https://github.com/tetafro/godot"), 393 linter.NewConfig(golinters.NewTestpackage(testpackageCfg)). 394 WithSince("v1.25.0"). 395 WithPresets(linter.PresetStyle, linter.PresetTest). 396 WithURL("https://github.com/maratori/testpackage"), 397 linter.NewConfig(golinters.NewNestif()). 398 WithSince("v1.25.0"). 399 WithPresets(linter.PresetComplexity). 400 WithURL("https://github.com/nakabonne/nestif"), 401 linter.NewConfig(golinters.NewExportLoopRef()). 402 WithSince("v1.28.0"). 403 WithPresets(linter.PresetBugs). 404 WithLoadForGoAnalysis(). 405 WithURL("https://github.com/kyoh86/exportloopref"), 406 linter.NewConfig(golinters.NewExhaustive(exhaustiveCfg)). 407 WithSince(" v1.28.0"). 408 WithPresets(linter.PresetBugs). 409 WithLoadForGoAnalysis(). 410 WithURL("https://github.com/nishanths/exhaustive"), 411 linter.NewConfig(golinters.NewSQLCloseCheck()). 412 WithSince("v1.28.0"). 413 WithPresets(linter.PresetBugs, linter.PresetSQL). 414 WithLoadForGoAnalysis(). 415 WithURL("https://github.com/ryanrolds/sqlclosecheck"), 416 linter.NewConfig(golinters.NewNLReturn()). 417 WithSince("v1.30.0"). 418 WithPresets(linter.PresetStyle). 419 WithURL("https://github.com/ssgreg/nlreturn"), 420 linter.NewConfig(golinters.NewWrapcheck(wrapcheckCfg)). 421 WithSince("v1.32.0"). 422 WithPresets(linter.PresetStyle, linter.PresetError). 423 WithLoadForGoAnalysis(). 424 WithURL("https://github.com/tomarrell/wrapcheck"), 425 linter.NewConfig(golinters.NewThelper(thelperCfg)). 426 WithSince("v1.34.0"). 427 WithPresets(linter.PresetStyle). 428 WithLoadForGoAnalysis(). 429 WithURL("https://github.com/kulti/thelper"), 430 linter.NewConfig(golinters.NewTparallel()). 431 WithSince("v1.32.0"). 432 WithPresets(linter.PresetStyle, linter.PresetTest). 433 WithLoadForGoAnalysis(). 434 WithURL("https://github.com/moricho/tparallel"), 435 linter.NewConfig(golinters.NewExhaustiveStruct(exhaustiveStructCfg)). 436 WithSince("v1.32.0"). 437 WithPresets(linter.PresetStyle, linter.PresetTest). 438 WithLoadForGoAnalysis(). 439 WithURL("https://github.com/mbilski/exhaustivestruct"), 440 linter.NewConfig(golinters.NewErrorLint(errorlintCfg)). 441 WithSince("v1.32.0"). 442 WithPresets(linter.PresetBugs, linter.PresetError). 443 WithLoadForGoAnalysis(). 444 WithURL("https://github.com/polyfloyd/go-errorlint"), 445 linter.NewConfig(golinters.NewParallelTest()). 446 WithSince("v1.33.0"). 447 WithPresets(linter.PresetStyle, linter.PresetTest). 448 WithURL("https://github.com/kunwardeep/paralleltest"), 449 linter.NewConfig(golinters.NewMakezero()). 450 WithSince("v1.34.0"). 451 WithPresets(linter.PresetStyle, linter.PresetBugs). 452 WithLoadForGoAnalysis(). 453 WithURL("https://github.com/ashanbrown/makezero"), 454 linter.NewConfig(golinters.NewForbidigo()). 455 WithSince("v1.34.0"). 456 WithPresets(linter.PresetStyle). 457 WithURL("https://github.com/ashanbrown/forbidigo"), 458 linter.NewConfig(golinters.NewIfshort(ifshortCfg)). 459 WithSince("v1.36.0"). 460 WithPresets(linter.PresetStyle). 461 WithURL("https://github.com/esimonov/ifshort"), 462 linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)). 463 WithSince("v1.35.0"). 464 WithPresets(linter.PresetStyle). 465 WithURL("https://github.com/nishanths/predeclared"), 466 linter.NewConfig(golinters.NewRevive(reviveCfg)). 467 WithSince("v1.37.0"). 468 WithPresets(linter.PresetStyle, linter.PresetMetaLinter). 469 ConsiderSlow(). 470 WithURL("https://github.com/mgechev/revive"), 471 linter.NewConfig(golinters.NewDurationCheck()). 472 WithSince("v1.37.0"). 473 WithPresets(linter.PresetBugs). 474 WithLoadForGoAnalysis(). 475 WithURL("https://github.com/charithe/durationcheck"), 476 linter.NewConfig(golinters.NewWastedAssign()). 477 WithSince("v1.38.0"). 478 WithPresets(linter.PresetStyle). 479 WithLoadForGoAnalysis(). 480 WithURL("https://github.com/sanposhiho/wastedassign"), 481 linter.NewConfig(golinters.NewImportAs(importAsCfg)). 482 WithSince("v1.38.0"). 483 WithPresets(linter.PresetStyle). 484 WithLoadForGoAnalysis(). 485 WithURL("https://github.com/julz/importas"), 486 linter.NewConfig(golinters.NewNilErr()). 487 WithSince("v1.38.0"). 488 WithLoadForGoAnalysis(). 489 WithPresets(linter.PresetBugs). 490 WithURL("https://github.com/gostaticanalysis/nilerr"), 491 linter.NewConfig(golinters.NewForceTypeAssert()). 492 WithSince("v1.38.0"). 493 WithPresets(linter.PresetStyle). 494 WithURL("https://github.com/gostaticanalysis/forcetypeassert"), 495 linter.NewConfig(golinters.NewGoModDirectives(goModDirectivesCfg)). 496 WithSince("v1.39.0"). 497 WithPresets(linter.PresetStyle, linter.PresetModule). 498 WithURL("https://github.com/ldez/gomoddirectives"), 499 linter.NewConfig(golinters.NewPromlinter()). 500 WithSince("v1.40.0"). 501 WithPresets(linter.PresetStyle). 502 WithURL("https://github.com/yeya24/promlinter"), 503 linter.NewConfig(golinters.NewTagliatelle(tagliatelleCfg)). 504 WithSince("v1.40.0"). 505 WithPresets(linter.PresetStyle). 506 WithURL("https://github.com/ldez/tagliatelle"), 507 linter.NewConfig(golinters.NewErrName()). 508 WithPresets(linter.PresetStyle). 509 WithLoadForGoAnalysis(). 510 WithURL("https://github.com/Antonboom/errname"). 511 WithSince("v1.42.0"), 512 linter.NewConfig(monkit.NewMonkitCheck()). 513 WithLoadForGoAnalysis(), 514 linter.NewConfig(deferloop.NewDeferLoopCheck()). 515 WithLoadForGoAnalysis(), 516 linter.NewConfig(errs.NewErrsCheck()). 517 WithLoadForGoAnalysis(), 518 519 // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives 520 linter.NewConfig(golinters.NewNoLintLint()). 521 WithSince("v1.26.0"). 522 WithPresets(linter.PresetStyle). 523 WithURL("https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/nolintlint/README.md"), 524 } 525 526 enabledByDefault := map[string]bool{ 527 golinters.NewGovet(nil).Name(): true, 528 golinters.NewErrcheck().Name(): true, 529 golinters.NewStaticcheck(staticcheckCfg).Name(): true, 530 golinters.NewUnused(unusedCfg).Name(): true, 531 golinters.NewGosimple(gosimpleCfg).Name(): true, 532 golinters.NewStructcheck().Name(): true, 533 golinters.NewVarcheck().Name(): true, 534 golinters.NewIneffassign().Name(): true, 535 golinters.NewDeadcode().Name(): true, 536 golinters.NewTypecheck().Name(): true, 537 } 538 return enableLinterConfigs(lcs, func(lc *linter.Config) bool { 539 return enabledByDefault[lc.Name()] 540 }) 541 } 542 543 func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config { 544 var ret []*linter.Config 545 for _, lc := range m.GetAllSupportedLinterConfigs() { 546 if lc.EnabledByDefault { 547 ret = append(ret, lc) 548 } 549 } 550 551 return ret 552 } 553 554 func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config { 555 ret := map[string]*linter.Config{} 556 for _, lc := range lcs { 557 lc := lc // local copy 558 ret[lc.Name()] = lc 559 } 560 561 return ret 562 } 563 564 func (m Manager) GetAllLinterConfigsForPreset(p string) []*linter.Config { 565 var ret []*linter.Config 566 for _, lc := range m.GetAllSupportedLinterConfigs() { 567 for _, ip := range lc.InPresets { 568 if p == ip { 569 ret = append(ret, lc) 570 break 571 } 572 } 573 } 574 575 return ret 576 } 577 578 func (m Manager) loadCustomLinterConfig(name string, settings config.CustomLinterSettings) (*linter.Config, error) { 579 analyzer, err := m.getAnalyzerPlugin(settings.Path) 580 if err != nil { 581 return nil, err 582 } 583 m.log.Infof("Loaded %s: %s", settings.Path, name) 584 customLinter := goanalysis.NewLinter( 585 name, 586 settings.Description, 587 analyzer.GetAnalyzers(), 588 nil).WithLoadMode(goanalysis.LoadModeTypesInfo) 589 linterConfig := linter.NewConfig(customLinter) 590 linterConfig.EnabledByDefault = true 591 linterConfig.IsSlow = false 592 linterConfig.WithURL(settings.OriginalURL) 593 return linterConfig, nil 594 } 595 596 type AnalyzerPlugin interface { 597 GetAnalyzers() []*analysis.Analyzer 598 } 599 600 func (m Manager) getAnalyzerPlugin(path string) (AnalyzerPlugin, error) { 601 if !filepath.IsAbs(path) { 602 // resolve non-absolute paths relative to config file's directory 603 configFilePath := viper.ConfigFileUsed() 604 absConfigFilePath, err := filepath.Abs(configFilePath) 605 if err != nil { 606 return nil, fmt.Errorf("could not get absolute representation of config file path %q: %v", configFilePath, err) 607 } 608 path = filepath.Join(filepath.Dir(absConfigFilePath), path) 609 } 610 611 plug, err := plugin.Open(path) 612 if err != nil { 613 return nil, err 614 } 615 616 symbol, err := plug.Lookup("AnalyzerPlugin") 617 if err != nil { 618 return nil, err 619 } 620 621 analyzerPlugin, ok := symbol.(AnalyzerPlugin) 622 if !ok { 623 return nil, fmt.Errorf("plugin %s does not abide by 'AnalyzerPlugin' interface", path) 624 } 625 626 return analyzerPlugin, nil 627 }