Skip to content

Commit

Permalink
Merge pull request #92 from k1LoW/errorstrings.exclude-test
Browse files Browse the repository at this point in the history
Add errorstrings.exclude-test
  • Loading branch information
k1LoW authored Dec 13, 2023
2 parents 90f6c97 + 4fecd3f commit 85d6e35
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ analyzers-settings:
analyzers-settings:
errorstrings:
include-generated: false # include generated codes (default: false)
exclude-test: true # exclude test files (default: false)
```

#### funcfmt
Expand Down
8 changes: 8 additions & 0 deletions analyzer/code_review_comments/errorstrings/errorstrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
var (
disable bool
includeGenerated bool
excludeTest bool
)

// Analyzer based on https://github.com/golang/go/wiki/CodeReviewComments#error-strings
Expand Down Expand Up @@ -56,6 +57,7 @@ func run(pass *analysis.Pass) (any, error) {
if c != nil {
disable = c.IsDisabled(name)
includeGenerated = c.AnalyzersSettings.Errorstrings.IncludeGenerated
excludeTest = c.AnalyzersSettings.Errorstrings.ExcludeTest
}
if disable {
return nil, nil
Expand All @@ -78,6 +80,11 @@ func run(pass *analysis.Pass) (any, error) {
return nil, err
}
i.Preorder(nodeFilter, func(n ast.Node) {
if excludeTest {
if strings.HasSuffix(pass.Fset.File(n.Pos()).Name(), "_test.go") {
return
}
}
switch e := n.(type) {
case *ast.CallExpr:
if len(e.Args) == 0 {
Expand Down Expand Up @@ -124,4 +131,5 @@ func isNG(in string) bool {
func init() {
Analyzer.Flags.BoolVar(&disable, "disable", false, "disable "+name+" analyzer")
Analyzer.Flags.BoolVar(&includeGenerated, "include-generated", false, "include generated codes")
Analyzer.Flags.BoolVar(&excludeTest, "exclude-test", false, "exclude test files")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
excludeTest = true
td := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, td, Analyzer, "a")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package a

import "testing"

func TestA(t *testing.T) {
t.Errorf("This is %s", "world")
}
10 changes: 5 additions & 5 deletions analyzer/code_review_comments/handlerrors/handlerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func run(pass *analysis.Pass) (any, error) {
}

i.Preorder(nodeFilter, func(n ast.Node) {
if excludeTest {
if strings.HasSuffix(pass.Fset.File(n.Pos()).Name(), "_test.go") {
return
}
}
switch nn := n.(type) {
case *ast.AssignStmt:
if len(nn.Rhs) == 0 {
Expand All @@ -107,11 +112,6 @@ func run(pass *analysis.Pass) (any, error) {
if id.Name != "_" {
continue
}
if excludeTest {
if strings.HasSuffix(pass.Fset.File(e.Pos()).Name(), "_test.go") {
return
}
}
br.report(e, i)
}
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Dontpanic struct {

type Errorstrings struct {
IncludeGenerated bool `yaml:"include-generated"`
ExcludeTest bool `yaml:"exclude-test"`
}

type Funcfmt struct {
Expand Down

0 comments on commit 85d6e35

Please sign in to comment.