-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from k1LoW/errorstrings
Add errorstrings analyzer
- Loading branch information
Showing
8 changed files
with
228 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
analyzer/code_review_comments/errorstrings/errorstrings.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package errorstrings | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/gostaticanalysis/comment/passes/commentmap" | ||
"github.com/k1LoW/gostyle/config" | ||
"github.com/k1LoW/gostyle/reporter" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
const ( | ||
name = "errorstrings" | ||
doc = "Analyzer based on https://github.com/golang/go/wiki/CodeReviewComments#error-strings" | ||
msg = "Error strings should not be capitalized (unless beginning with proper nouns or acronyms) or end with punctuation, since they are usually printed following other context. (ref: https://github.com/golang/go/wiki/CodeReviewComments#error-strings)" | ||
) | ||
|
||
var ( | ||
disable bool | ||
includeGenerated bool | ||
) | ||
|
||
// Analyzer based on https://github.com/golang/go/wiki/CodeReviewComments#error-strings | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: name, | ||
Doc: doc, | ||
Run: run, | ||
Requires: []*analysis.Analyzer{ | ||
inspect.Analyzer, | ||
commentmap.Analyzer, | ||
}, | ||
} | ||
|
||
// AnalyzerWithConfig based on https://github.com/golang/go/wiki/CodeReviewComments#error-strings | ||
var AnalyzerWithConfig = &analysis.Analyzer{ | ||
Name: name, | ||
Doc: doc, | ||
Run: run, | ||
Requires: []*analysis.Analyzer{ | ||
config.Loader, | ||
inspect.Analyzer, | ||
commentmap.Analyzer, | ||
}, | ||
} | ||
|
||
func run(pass *analysis.Pass) (any, error) { | ||
c, err := config.Load(pass) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if c != nil { | ||
disable = c.IsDisabled(name) | ||
includeGenerated = c.AnalyzersSettings.Errorstrings.IncludeGenerated | ||
} | ||
if disable { | ||
return nil, nil | ||
} | ||
i, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected result type from inspect: %T", pass.ResultOf[inspect.Analyzer]) | ||
} | ||
|
||
nodeFilter := []ast.Node{ | ||
(*ast.CallExpr)(nil), | ||
} | ||
|
||
var opts []reporter.Option | ||
if includeGenerated { | ||
opts = append(opts, reporter.IncludeGenerated()) | ||
} | ||
r, err := reporter.New(name, pass, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
i.Preorder(nodeFilter, func(n ast.Node) { | ||
switch e := n.(type) { | ||
case *ast.CallExpr: | ||
if len(e.Args) == 0 { | ||
return | ||
} | ||
fn, ok := e.Fun.(*ast.SelectorExpr) | ||
if !ok { | ||
return | ||
} | ||
if fn.Sel.Name == "Errorf" { | ||
bl, ok := e.Args[0].(*ast.BasicLit) | ||
if !ok { | ||
return | ||
} | ||
if isNG(bl.Value) { | ||
r.Append(e.Pos(), fmt.Sprintf("%s: %s", msg, bl.Value)) | ||
} | ||
return | ||
} | ||
id, ok := fn.X.(*ast.Ident) | ||
if !ok { | ||
return | ||
} | ||
if id.Name == "errors" && fn.Sel.Name == "New" { | ||
bl, ok := e.Args[0].(*ast.BasicLit) | ||
if !ok { | ||
return | ||
} | ||
if isNG(bl.Value) { | ||
r.Append(e.Pos(), fmt.Sprintf("%s: %s", msg, bl.Value)) | ||
} | ||
} | ||
} | ||
}) | ||
r.Report() | ||
return nil, nil | ||
} | ||
|
||
func isNG(in string) bool { | ||
f := strings.Trim(in, "\"'`") | ||
return unicode.IsUpper(rune(f[0])) || strings.HasSuffix(f, ".") | ||
} | ||
|
||
func init() { | ||
Analyzer.Flags.BoolVar(&disable, "disable", false, "disable "+name+" analyzer") | ||
Analyzer.Flags.BoolVar(&includeGenerated, "include-generated", false, "include generated codes") | ||
} |
14 changes: 14 additions & 0 deletions
14
analyzer/code_review_comments/errorstrings/errorstrings_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package errorstrings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gostaticanalysis/testutil" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
// TestAnalyzer is a test for Analyzer. | ||
func TestAnalyzer(t *testing.T) { | ||
td := testutil.WithModules(t, analysistest.TestData(), nil) | ||
analysistest.Run(t, td, Analyzer, "a") | ||
} |
17 changes: 17 additions & 0 deletions
17
analyzer/code_review_comments/errorstrings/testdata/src/a/a.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package a | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func f() { | ||
e := fmt.Errorf("This is %s", "world") // want "gostyle.errorstrings" | ||
print(e.Error()) | ||
e2 := fmt.Errorf("this is %s.", "world") // want "gostyle.errorstrings" | ||
print(e2.Error()) | ||
var e3 = errors.New("This is world") // want "gostyle.errorstrings" | ||
print(e3.Error()) | ||
var e4 = errors.New("this is world.") // want "gostyle.errorstrings" | ||
print(e4.Error()) | ||
} |
4 changes: 4 additions & 0 deletions
4
analyzer/code_review_comments/errorstrings/testdata/src/a/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module a | ||
|
||
go 1.21 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters