github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/golint.go (about)

     1  // Copyright 2018 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package check
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"golang.org/x/lint"
    12  )
    13  
    14  // minConfidence is the confidence threshold for problems reported from Golint.
    15  // Golint's default is 0.8.
    16  const minConfidence = 0.79
    17  
    18  // shouldIgnore is called to filter irrelevant issues reported by Golint.
    19  func shouldIgnore(p lint.Problem) bool {
    20  	if p.Confidence < minConfidence {
    21  		return true
    22  	}
    23  
    24  	// Ignore unexported-type-in-api.
    25  	if p.Category == "unexported-type-in-api" {
    26  		return true
    27  	}
    28  
    29  	// Tast test functions can be exported without comment.
    30  	if isEntryFile(p.Position.Filename) &&
    31  		p.Category == "comments" &&
    32  		strings.Contains(p.Text, "should have comment or be unexported") {
    33  		return true
    34  	}
    35  
    36  	return false
    37  }
    38  
    39  // Golint runs Golint to find issues.
    40  func Golint(path string, code []byte, debug bool) []*Issue {
    41  	ps, err := (&lint.Linter{}).Lint(path, code)
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	var issues []*Issue
    47  	for _, p := range ps {
    48  		if shouldIgnore(p) {
    49  			continue
    50  		}
    51  
    52  		var msg string
    53  		if debug {
    54  			msg = fmt.Sprintf("[%s; %.2f] %s", p.Category, p.Confidence, p.Text)
    55  		} else {
    56  			msg = p.Text
    57  		}
    58  		issues = append(issues, &Issue{
    59  			Pos:  p.Position,
    60  			Msg:  msg,
    61  			Link: p.Link,
    62  		})
    63  	}
    64  	return issues
    65  }