github.com/nozzle/golangci-lint@v1.49.0-nz3/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/golangci/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  	CanAutoFix      bool
    46  	IsSlow          bool
    47  	DoesChangeTypes bool
    48  
    49  	Since       string
    50  	Deprecation *Deprecation
    51  }
    52  
    53  func (lc *Config) ConsiderSlow() *Config {
    54  	lc.IsSlow = true
    55  	return lc
    56  }
    57  
    58  func (lc *Config) IsSlowLinter() bool {
    59  	return lc.IsSlow
    60  }
    61  
    62  func (lc *Config) WithLoadFiles() *Config {
    63  	lc.LoadMode |= packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles
    64  	return lc
    65  }
    66  
    67  func (lc *Config) WithLoadForGoAnalysis() *Config {
    68  	lc = lc.WithLoadFiles()
    69  	lc.LoadMode |= packages.NeedImports | packages.NeedDeps | packages.NeedExportFile | packages.NeedTypesSizes
    70  	lc.IsSlow = true
    71  	return lc
    72  }
    73  
    74  func (lc *Config) WithPresets(presets ...string) *Config {
    75  	lc.InPresets = presets
    76  	return lc
    77  }
    78  
    79  func (lc *Config) WithURL(url string) *Config {
    80  	lc.OriginalURL = url
    81  	return lc
    82  }
    83  
    84  func (lc *Config) WithAlternativeNames(names ...string) *Config {
    85  	lc.AlternativeNames = names
    86  	return lc
    87  }
    88  
    89  func (lc *Config) WithAutoFix() *Config {
    90  	lc.CanAutoFix = true
    91  	return lc
    92  }
    93  
    94  func (lc *Config) WithChangeTypes() *Config {
    95  	lc.DoesChangeTypes = true
    96  	return lc
    97  }
    98  
    99  func (lc *Config) WithSince(version string) *Config {
   100  	lc.Since = version
   101  	return lc
   102  }
   103  
   104  func (lc *Config) Deprecated(message, version, replacement string) *Config {
   105  	lc.Deprecation = &Deprecation{
   106  		Since:       version,
   107  		Message:     message,
   108  		Replacement: replacement,
   109  	}
   110  	return lc
   111  }
   112  
   113  func (lc *Config) IsDeprecated() bool {
   114  	return lc.Deprecation != nil
   115  }
   116  
   117  func (lc *Config) AllNames() []string {
   118  	return append([]string{lc.Name()}, lc.AlternativeNames...)
   119  }
   120  
   121  func (lc *Config) Name() string {
   122  	return lc.Linter.Name()
   123  }
   124  
   125  func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
   126  	if cfg != nil && config.IsGreaterThanOrEqualGo118(cfg.Run.Go) {
   127  		lc.Linter = &Noop{
   128  			name: lc.Linter.Name(),
   129  			desc: lc.Linter.Desc(),
   130  			run: func(pass *analysis.Pass) (interface{}, error) {
   131  				return nil, nil
   132  			},
   133  		}
   134  
   135  		lc.LoadMode = 0
   136  		return lc.WithLoadFiles()
   137  	}
   138  
   139  	return lc
   140  }
   141  
   142  func NewConfig(linter Linter) *Config {
   143  	lc := &Config{
   144  		Linter: linter,
   145  	}
   146  	return lc.WithLoadFiles()
   147  }