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

     1  package golinters
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/tetafro/godot"
     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 godotName = "godot"
    15  
    16  func NewGodot() *goanalysis.Linter {
    17  	var mu sync.Mutex
    18  	var resIssues []goanalysis.Issue
    19  
    20  	analyzer := &analysis.Analyzer{
    21  		Name: godotName,
    22  		Doc:  goanalysis.TheOnlyanalyzerDoc,
    23  	}
    24  	return goanalysis.NewLinter(
    25  		godotName,
    26  		"Check if comments end in a period",
    27  		[]*analysis.Analyzer{analyzer},
    28  		nil,
    29  	).WithContextSetter(func(lintCtx *linter.Context) {
    30  		cfg := lintCtx.Cfg.LintersSettings.Godot
    31  		settings := godot.Settings{
    32  			Scope:   godot.Scope(cfg.Scope),
    33  			Exclude: cfg.Exclude,
    34  			Period:  true,
    35  			Capital: cfg.Capital,
    36  		}
    37  
    38  		// Convert deprecated setting
    39  		if cfg.CheckAll { // nolint: staticcheck
    40  			settings.Scope = godot.TopLevelScope
    41  		}
    42  
    43  		if settings.Scope == "" {
    44  			settings.Scope = godot.DeclScope
    45  		}
    46  
    47  		analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
    48  			var issues []godot.Issue
    49  			for _, file := range pass.Files {
    50  				iss, err := godot.Run(file, pass.Fset, settings)
    51  				if err != nil {
    52  					return nil, err
    53  				}
    54  				issues = append(issues, iss...)
    55  			}
    56  
    57  			if len(issues) == 0 {
    58  				return nil, nil
    59  			}
    60  
    61  			res := make([]goanalysis.Issue, len(issues))
    62  			for k, i := range issues {
    63  				issue := result.Issue{
    64  					Pos:        i.Pos,
    65  					Text:       i.Message,
    66  					FromLinter: godotName,
    67  					Replacement: &result.Replacement{
    68  						NewLines: []string{i.Replacement},
    69  					},
    70  				}
    71  
    72  				res[k] = goanalysis.NewIssue(&issue, pass)
    73  			}
    74  
    75  			mu.Lock()
    76  			resIssues = append(resIssues, res...)
    77  			mu.Unlock()
    78  
    79  			return nil, nil
    80  		}
    81  	}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    82  		return resIssues
    83  	}).WithLoadMode(goanalysis.LoadModeSyntax)
    84  }