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

     1  package golinters
     2  
     3  import (
     4  	"go/token"
     5  	"strings"
     6  	"sync"
     7  
     8  	"github.com/matoous/godox"
     9  	"golang.org/x/tools/go/analysis"
    10  
    11  	"github.com/elek/golangci-lint/pkg/golinters/goanalysis"
    12  	"github.com/elek/golangci-lint/pkg/lint/linter"
    13  	"github.com/elek/golangci-lint/pkg/result"
    14  )
    15  
    16  const godoxName = "godox"
    17  
    18  func NewGodox() *goanalysis.Linter {
    19  	var mu sync.Mutex
    20  	var resIssues []goanalysis.Issue
    21  
    22  	analyzer := &analysis.Analyzer{
    23  		Name: godoxName,
    24  		Doc:  goanalysis.TheOnlyanalyzerDoc,
    25  	}
    26  	return goanalysis.NewLinter(
    27  		godoxName,
    28  		"Tool for detection of FIXME, TODO and other comment keywords",
    29  		[]*analysis.Analyzer{analyzer},
    30  		nil,
    31  	).WithContextSetter(func(lintCtx *linter.Context) {
    32  		analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
    33  			var issues []godox.Message
    34  			for _, file := range pass.Files {
    35  				issues = append(issues, godox.Run(file, pass.Fset, lintCtx.Settings().Godox.Keywords...)...)
    36  			}
    37  
    38  			if len(issues) == 0 {
    39  				return nil, nil
    40  			}
    41  
    42  			res := make([]goanalysis.Issue, len(issues))
    43  			for k, i := range issues {
    44  				res[k] = goanalysis.NewIssue(&result.Issue{
    45  					Pos: token.Position{
    46  						Filename: i.Pos.Filename,
    47  						Line:     i.Pos.Line,
    48  					},
    49  					Text:       strings.TrimRight(i.Message, "\n"),
    50  					FromLinter: godoxName,
    51  				}, pass)
    52  			}
    53  
    54  			mu.Lock()
    55  			resIssues = append(resIssues, res...)
    56  			mu.Unlock()
    57  
    58  			return nil, nil
    59  		}
    60  	}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    61  		return resIssues
    62  	}).WithLoadMode(goanalysis.LoadModeSyntax)
    63  }