Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -all option to ifacenames analyzer. #4

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading