Skip to content

Commit

Permalink
Merge pull request #4 from k1LoW/ifacenames-all
Browse files Browse the repository at this point in the history
Add `-all` option to ifacenames analyzer.
  • Loading branch information
k1LoW committed Sep 11, 2023
2 parents 7827796 + 146ac92 commit f1b2dcf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lint:
govulncheck ./...

build:
go build -ldflags="$(BUILD_LDFLAGS)" -o gostyle cmd/gostyle/main.go
go build -ldflags="$(BUILD_LDFLAGS)" -o gostyle

depsdev:
go install github.com/Songmu/ghch/cmd/ghch@latest
Expand Down
20 changes: 16 additions & 4 deletions analyzer/effective/ifacenames/ifacenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"golang.org/x/tools/go/ast/inspector"
)

const doc = "https://go.dev/doc/effective_go#interface-names"
const doc = "Analyzer based on https://go.dev/doc/effective_go#interface-names."

// Analyzer for https://go.dev/doc/effective_go#interface-names
var all bool

// Analyzer based on https://go.dev/doc/effective_go#interface-names.
var Analyzer = &analysis.Analyzer{
Name: "ifacenames",
Doc: doc,
URL: "https://github.com/k1LoW/gostyle/tree/main/analyzer/effective/ifacenames",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
Expand All @@ -39,14 +42,23 @@ func run(pass *analysis.Pass) (any, error) {
case *ast.InterfaceType:
if len(n.Methods.List) == 1 {
mn := n.Methods.List[0].Names[0].Name
if !strings.HasPrefix(ii.Name, mn) || !strings.HasSuffix(ii.Name, "er") {
pass.Reportf(n.Pos(), "By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun. (ref: https://go.dev/doc/effective_go#interface-names)")
if !strings.HasPrefix(ii.Name, mn) || !strings.HasSuffix(ii.Name, "er") { // huristic
pass.Reportf(n.Pos(), "by convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun. (ref: https://go.dev/doc/effective_go#interface-names)")
return
}
}
if all && !strings.HasSuffix(ii.Name, "er") {
pass.Reportf(n.Pos(), "all interface names with the -er suffix are required.")
return
}
case *ast.Ident:
ii = n
}
})

return nil, nil
}

func init() {
Analyzer.Flags.BoolVar(&all, "all", false, "all interface names with the -er suffix are required")
}
14 changes: 12 additions & 2 deletions analyzer/effective/ifacenames/ifacenames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (

// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, Analyzer, "a")
tests := []struct {
all bool // -all flag
pkg string
}{
{false, "a"},
{true, "b"},
}
for _, tt := range tests {
all = tt.all
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, Analyzer, tt.pkg)
}
}
9 changes: 7 additions & 2 deletions analyzer/effective/ifacenames/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package a

type Query interface { // want "By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun."
type Query interface { // want "-er suffix"
Do() error
}

type Closer interface { // want "By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun."
type Closer interface { // want "-er suffix"
Do() error
}

type Writer interface {
Write() error
}

type Add interface {
One() error
Two() error
}
18 changes: 18 additions & 0 deletions analyzer/effective/ifacenames/testdata/src/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package b

type Query interface { // want "-er suffix"
Do() error
}

type Closer interface { // want "-er suffix"
Do() error
}

type Writer interface {
Write() error
}

type Add interface { // want "-er suffix"
One() error
Two() error
}
3 changes: 3 additions & 0 deletions analyzer/effective/ifacenames/testdata/src/b/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module b

go 1.21

0 comments on commit f1b2dcf

Please sign in to comment.