github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/golinters/wsl.go (about)

     1  package golinters
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/bombsimon/wsl/v3"
     7  	"golang.org/x/tools/go/analysis"
     8  
     9  	"github.com/elek/golangci-lint/pkg/golinters/goanalysis"
    10  	"github.com/elek/golangci-lint/pkg/lint/linter"
    11  	"github.com/elek/golangci-lint/pkg/result"
    12  )
    13  
    14  const (
    15  	name = "wsl"
    16  )
    17  
    18  // NewWSL returns a new WSL linter.
    19  func NewWSL() *goanalysis.Linter {
    20  	var (
    21  		issues   []goanalysis.Issue
    22  		mu       = sync.Mutex{}
    23  		analyzer = &analysis.Analyzer{
    24  			Name: goanalysis.TheOnlyAnalyzerName,
    25  			Doc:  goanalysis.TheOnlyanalyzerDoc,
    26  		}
    27  	)
    28  
    29  	return goanalysis.NewLinter(
    30  		name,
    31  		"Whitespace Linter - Forces you to use empty lines!",
    32  		[]*analysis.Analyzer{analyzer},
    33  		nil,
    34  	).WithContextSetter(func(lintCtx *linter.Context) {
    35  		analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
    36  			var (
    37  				files        = []string{}
    38  				linterCfg    = lintCtx.Cfg.LintersSettings.WSL
    39  				processorCfg = wsl.Configuration{
    40  					StrictAppend:                     linterCfg.StrictAppend,
    41  					AllowAssignAndCallCuddle:         linterCfg.AllowAssignAndCallCuddle,
    42  					AllowAssignAndAnythingCuddle:     linterCfg.AllowAssignAndAnythingCuddle,
    43  					AllowMultiLineAssignCuddle:       linterCfg.AllowMultiLineAssignCuddle,
    44  					AllowCuddleDeclaration:           linterCfg.AllowCuddleDeclaration,
    45  					AllowTrailingComment:             linterCfg.AllowTrailingComment,
    46  					AllowSeparatedLeadingComment:     linterCfg.AllowSeparatedLeadingComment,
    47  					ForceCuddleErrCheckAndAssign:     linterCfg.ForceCuddleErrCheckAndAssign,
    48  					ForceCaseTrailingWhitespaceLimit: linterCfg.ForceCaseTrailingWhitespaceLimit,
    49  					ForceExclusiveShortDeclarations:  linterCfg.ForceExclusiveShortDeclarations,
    50  					AllowCuddleWithCalls:             []string{"Lock", "RLock"},
    51  					AllowCuddleWithRHS:               []string{"Unlock", "RUnlock"},
    52  					ErrorVariableNames:               []string{"err"},
    53  				}
    54  			)
    55  
    56  			for _, file := range pass.Files {
    57  				files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
    58  			}
    59  
    60  			wslErrors, _ := wsl.NewProcessorWithConfig(processorCfg).
    61  				ProcessFiles(files)
    62  
    63  			if len(wslErrors) == 0 {
    64  				return nil, nil
    65  			}
    66  
    67  			mu.Lock()
    68  			defer mu.Unlock()
    69  
    70  			for _, err := range wslErrors {
    71  				issues = append(issues, goanalysis.NewIssue(&result.Issue{
    72  					FromLinter: name,
    73  					Pos:        err.Position,
    74  					Text:       err.Reason,
    75  				}, pass))
    76  			}
    77  
    78  			return nil, nil
    79  		}
    80  	}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    81  		return issues
    82  	}).WithLoadMode(goanalysis.LoadModeSyntax)
    83  }