github.com/trigonella/golangci-lint@v1.10.1/pkg/golinters/golint.go (about)

     1  package golinters
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"go/ast"
     7  	"go/token"
     8  
     9  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    10  	"github.com/golangci/golangci-lint/pkg/result"
    11  	lintAPI "github.com/golangci/lint-1"
    12  )
    13  
    14  type Golint struct{}
    15  
    16  func (Golint) Name() string {
    17  	return "golint"
    18  }
    19  
    20  func (Golint) Desc() string {
    21  	return "Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes"
    22  }
    23  
    24  func (g Golint) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
    25  	var issues []result.Issue
    26  	var lintErr error
    27  	for _, pkg := range lintCtx.PkgProgram.Packages() {
    28  		files, fset, err := getASTFilesForPkg(lintCtx, &pkg)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  
    33  		i, err := g.lintPkg(lintCtx.Settings().Golint.MinConfidence, files, fset)
    34  		if err != nil {
    35  			lintErr = err
    36  			continue
    37  		}
    38  		issues = append(issues, i...)
    39  	}
    40  	if lintErr != nil {
    41  		lintCtx.Log.Warnf("Golint: %s", lintErr)
    42  	}
    43  
    44  	return issues, nil
    45  }
    46  
    47  func (g Golint) lintPkg(minConfidence float64, files []*ast.File, fset *token.FileSet) ([]result.Issue, error) {
    48  	l := new(lintAPI.Linter)
    49  	ps, err := l.LintASTFiles(files, fset)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("can't lint %d files: %s", len(files), err)
    52  	}
    53  
    54  	if len(ps) == 0 {
    55  		return nil, nil
    56  	}
    57  
    58  	issues := make([]result.Issue, 0, len(ps)) // This is worst case
    59  	for _, p := range ps {
    60  		if p.Confidence >= minConfidence {
    61  			issues = append(issues, result.Issue{
    62  				Pos:        p.Position,
    63  				Text:       markIdentifiers(p.Text),
    64  				FromLinter: g.Name(),
    65  			})
    66  			// TODO: use p.Link and p.Category
    67  		}
    68  	}
    69  
    70  	return issues, nil
    71  }