github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/lint/linter/config.go (about) 1 package linter 2 3 import ( 4 "golang.org/x/tools/go/analysis" 5 "golang.org/x/tools/go/packages" 6 7 "github.com/chenfeining/golangci-lint/pkg/config" 8 ) 9 10 const ( 11 PresetBugs = "bugs" // Related to bugs detection. 12 PresetComment = "comment" // Related to comments analysis. 13 PresetComplexity = "complexity" // Related to code complexity analysis. 14 PresetError = "error" // Related to error handling analysis. 15 PresetFormatting = "format" // Related to code formatting. 16 PresetImport = "import" // Related to imports analysis. 17 PresetMetaLinter = "metalinter" // Related to linter that contains multiple rules or multiple linters. 18 PresetModule = "module" // Related to Go modules analysis. 19 PresetPerformance = "performance" // Related to performance. 20 PresetSQL = "sql" // Related to SQL. 21 PresetStyle = "style" // Related to coding style. 22 PresetTest = "test" // Related to the analysis of the code of the tests. 23 PresetUnused = "unused" // Related to the detection of unused code. 24 ) 25 26 // LastLinter nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives. 27 const LastLinter = "nolintlint" 28 29 type Deprecation struct { 30 Since string 31 Message string 32 Replacement string 33 } 34 35 type Config struct { 36 Linter Linter 37 EnabledByDefault bool 38 39 LoadMode packages.LoadMode 40 41 InPresets []string 42 AlternativeNames []string 43 44 OriginalURL string // URL of original (not forked) repo, needed for autogenerated README 45 Internal bool // Internal linters cannot be disabled (ex: typecheck). 46 CanAutoFix bool 47 IsSlow bool 48 DoesChangeTypes bool 49 50 Since string 51 Deprecation *Deprecation 52 } 53 54 func (lc *Config) WithEnabledByDefault() *Config { 55 lc.EnabledByDefault = true 56 return lc 57 } 58 59 func (lc *Config) WithInternal() *Config { 60 lc.Internal = true 61 return lc 62 } 63 64 func (lc *Config) ConsiderSlow() *Config { 65 lc.IsSlow = true 66 return lc 67 } 68 69 func (lc *Config) IsSlowLinter() bool { 70 return lc.IsSlow 71 } 72 73 func (lc *Config) WithLoadFiles() *Config { 74 lc.LoadMode |= packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles 75 return lc 76 } 77 78 func (lc *Config) WithLoadForGoAnalysis() *Config { 79 lc = lc.WithLoadFiles() 80 lc.LoadMode |= packages.NeedImports | packages.NeedDeps | packages.NeedExportFile | packages.NeedTypesSizes 81 lc.IsSlow = true 82 return lc 83 } 84 85 func (lc *Config) WithPresets(presets ...string) *Config { 86 lc.InPresets = presets 87 return lc 88 } 89 90 func (lc *Config) WithURL(url string) *Config { 91 lc.OriginalURL = url 92 return lc 93 } 94 95 func (lc *Config) WithAlternativeNames(names ...string) *Config { 96 lc.AlternativeNames = names 97 return lc 98 } 99 100 func (lc *Config) WithAutoFix() *Config { 101 lc.CanAutoFix = true 102 return lc 103 } 104 105 func (lc *Config) WithChangeTypes() *Config { 106 lc.DoesChangeTypes = true 107 return lc 108 } 109 110 func (lc *Config) WithSince(version string) *Config { 111 lc.Since = version 112 return lc 113 } 114 115 func (lc *Config) Deprecated(message, version, replacement string) *Config { 116 lc.Deprecation = &Deprecation{ 117 Since: version, 118 Message: message, 119 Replacement: replacement, 120 } 121 return lc 122 } 123 124 func (lc *Config) IsDeprecated() bool { 125 return lc.Deprecation != nil 126 } 127 128 func (lc *Config) AllNames() []string { 129 return append([]string{lc.Name()}, lc.AlternativeNames...) 130 } 131 132 func (lc *Config) Name() string { 133 return lc.Linter.Name() 134 } 135 136 func (lc *Config) WithNoopFallback(cfg *config.Config) *Config { 137 if cfg != nil && config.IsGreaterThanOrEqualGo118(cfg.Run.Go) { 138 lc.Linter = &Noop{ 139 name: lc.Linter.Name(), 140 desc: lc.Linter.Desc(), 141 run: func(pass *analysis.Pass) (any, error) { 142 return nil, nil 143 }, 144 } 145 146 lc.LoadMode = 0 147 return lc.WithLoadFiles() 148 } 149 150 return lc 151 } 152 153 func NewConfig(linter Linter) *Config { 154 lc := &Config{ 155 Linter: linter, 156 } 157 return lc.WithLoadFiles() 158 }