From 70b1ff912b77c262ec30c7a939d51f1899b46a00 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Fri, 29 Nov 2024 14:04:46 +0200 Subject: [PATCH] chore(fix): fix make generaate command (#81) * chore(fix): fix `make generaate` command * dev: adding `maphash.String/Bytes` * dev: updating tests & generator --- MIRROR_FUNCS.md | 1 + Makefile | 3 +- analyzer.go | 4 +- checkers_maphash.go | 87 +++++++++++++++++--------- cmd/internal/mirror-table/main.go | 1 + cmd/internal/mirror-table/support.go | 3 +- cmd/internal/tests/main.go | 1 + cmd/internal/tests/templates/case.tmpl | 4 +- testdata/maphash.go | 60 ++++++++++++++++++ 9 files changed, 130 insertions(+), 34 deletions(-) diff --git a/MIRROR_FUNCS.md b/MIRROR_FUNCS.md index d068432..da30c8e 100644 --- a/MIRROR_FUNCS.md +++ b/MIRROR_FUNCS.md @@ -25,6 +25,7 @@ | `func bytes.LastIndexFunc([]byte, func(rune) bool) int` | `func strings.LastIndexFunc(string, func(rune) bool) int` | | `func bytes.NewBuffer([]byte) *bytes.Buffer` | `func bytes.NewBufferString(string) *bytes.Buffer` | | `func (*httptest.ResponseRecorder) Write([]byte) (int, error)` | `func (*httptest.ResponseRecorder) WriteString(string) (int, error)` | +| `func maphash.Bytes([]byte) uint64` | `func maphash.String(string) uint64` | | `func (*maphash.Hash) Write([]byte) (int, error)` | `func (*maphash.Hash) WriteString(string) (int, error)` | | `func (*os.File) Write([]byte) (int, error)` | `func (*os.File) WriteString(string) (int, error)` | | `func regexp.Match(string, []byte) (bool, error)` | `func regexp.MatchString(string, string) (bool, error)` | diff --git a/Makefile b/Makefile index 969d6b9..dab6f16 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ endef # Generate Artifacts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ generate: ## Generate Assets - $(MAKE) + $(MAKE) generate-tests + $(MAKE) generate-mirror-table generate-tests: ## Generates Assets at testdata go run ./cmd/internal/tests/ "$(PWD)/testdata" diff --git a/analyzer.go b/analyzer.go index 13ded46..b15019c 100644 --- a/analyzer.go +++ b/analyzer.go @@ -44,9 +44,9 @@ func Run(pass *analysis.Pass, withTests bool) []*checker.Violation { BytesFunctions, BytesBufferMethods, RegexpFunctions, RegexpRegexpMethods, StringFunctions, StringsBuilderMethods, + MaphashMethods, MaphashFunctions, BufioMethods, HTTPTestMethods, - OsFileMethods, MaphashMethods, - UTF8Functions, + OsFileMethods, UTF8Functions, ) check.Type = checker.WrapType(pass.TypesInfo) diff --git a/checkers_maphash.go b/checkers_maphash.go index 0aa43ff..345a641 100644 --- a/checkers_maphash.go +++ b/checkers_maphash.go @@ -2,35 +2,66 @@ package mirror import "github.com/butuzov/mirror/internal/checker" -var MaphashMethods = []checker.Violation{ - { // (*hash/maphash).Write - Targets: checker.Bytes, - Type: checker.Method, - Package: "hash/maphash", - Struct: "Hash", - Caller: "Write", - Args: []int{0}, - AltCaller: "WriteString", +var ( + MaphashFunctions = []checker.Violation{ + { // maphash.Bytes + Targets: checker.Bytes, + Type: checker.Function, + Package: "hash/maphash", + Caller: "Bytes", + Args: []int{1}, + AltCaller: "String", - Generate: &checker.Generate{ - PreCondition: `h := maphash.Hash{}`, - Pattern: `Write($0)`, - Returns: []string{"int", "error"}, + Generate: &checker.Generate{ + Pattern: `Bytes(maphash.MakeSeed(), $0)`, + Returns: []string{"uint64"}, + }, }, - }, - { // (*hash/maphash).WriteString - Targets: checker.Strings, - Type: checker.Method, - Package: "hash/maphash", - Struct: "Hash", - Caller: "WriteString", - Args: []int{0}, - AltCaller: "Write", + { // maphash.String + Targets: checker.Strings, + Type: checker.Function, + Package: "hash/maphash", + Caller: "String", + Args: []int{1}, + AltCaller: "Bytes", - Generate: &checker.Generate{ - PreCondition: `h := maphash.Hash{}`, - Pattern: `WriteString($0)`, - Returns: []string{"int", "error"}, + Generate: &checker.Generate{ + Pattern: `String(maphash.MakeSeed(), $0)`, + Returns: []string{"uint64"}, + }, }, - }, -} + } + + MaphashMethods = []checker.Violation{ + { // (*hash/maphash).Write + Targets: checker.Bytes, + Type: checker.Method, + Package: "hash/maphash", + Struct: "Hash", + Caller: "Write", + Args: []int{0}, + AltCaller: "WriteString", + + Generate: &checker.Generate{ + PreCondition: `h := maphash.Hash{}`, + Pattern: `Write($0)`, + Returns: []string{"int", "error"}, + }, + }, + { // (*hash/maphash).WriteString + Targets: checker.Strings, + Type: checker.Method, + Package: "hash/maphash", + Struct: "Hash", + Caller: "WriteString", + Args: []int{0}, + AltCaller: "Write", + + Generate: &checker.Generate{ + PreCondition: `h := maphash.Hash{}`, + Pattern: `WriteString($0)`, + Returns: []string{"int", "error"}, + }, + }, + } +) diff --git a/cmd/internal/mirror-table/main.go b/cmd/internal/mirror-table/main.go index c09842e..dd05d11 100644 --- a/cmd/internal/mirror-table/main.go +++ b/cmd/internal/mirror-table/main.go @@ -17,6 +17,7 @@ func main() { mirror.BytesBufferMethods, mirror.BytesFunctions, mirror.HTTPTestMethods, + mirror.MaphashFunctions, mirror.MaphashMethods, mirror.OsFileMethods, mirror.RegexpFunctions, diff --git a/cmd/internal/mirror-table/support.go b/cmd/internal/mirror-table/support.go index 60a2548..e9fb48f 100644 --- a/cmd/internal/mirror-table/support.go +++ b/cmd/internal/mirror-table/support.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "regexp" "strings" @@ -80,7 +81,7 @@ func formArgs(v checker.Violation, isAlt bool) string { f := strings.Split(i, "{") a = append(a, strings.TrimSpace(f[0])) default: - fmt.Println(">", i) + fmt.Fprintln(os.Stderr, ">", i) } } } diff --git a/cmd/internal/tests/main.go b/cmd/internal/tests/main.go index 67de9f0..7ca685c 100644 --- a/cmd/internal/tests/main.go +++ b/cmd/internal/tests/main.go @@ -61,6 +61,7 @@ func main() { { // hash/maphash tests := []string{} + tests = append(tests, generateTests("maphash", mirror.MaphashFunctions)...) tests = append(tests, generateTests("maphash", mirror.MaphashMethods)...) err := GenerateTestFile(filepath.Join(testdata, "maphash.go"), "hash/maphash", tests) diff --git a/cmd/internal/tests/templates/case.tmpl b/cmd/internal/tests/templates/case.tmpl index 72ea3f5..3ecbb48 100644 --- a/cmd/internal/tests/templates/case.tmpl +++ b/cmd/internal/tests/templates/case.tmpl @@ -1,6 +1,6 @@ { - {{if .PreCond}}{{.PreCond}}{{end}} - {{- range $v := .Arguments}} + {{if .PreCond}}{{.PreCond}}{{end -}} + {{range $v := .Arguments}} {{$v}} {{- end}} {{.Returns}} = {{if .Package}}{{.Package}}.{{end}}{{.Func}} {{if .Want}}// want `{{.Want}}`{{end}} diff --git a/testdata/maphash.go b/testdata/maphash.go index c02ed0d..a865db0 100644 --- a/testdata/maphash.go +++ b/testdata/maphash.go @@ -10,6 +10,66 @@ import ( func main_maphash() { + { + + _ = maphash.Bytes(maphash.MakeSeed(), []byte("foobar")) // want `avoid allocations with maphash\.String` + } + + { + + _ = maphash.Bytes(maphash.MakeSeed(), []byte{'f','o','o','b','a','r'}) + } + + { + + _ = Bytes(maphash.MakeSeed(), []byte("foobar")) // want `avoid allocations with maphash\.String` + } + + { + + _ = Bytes(maphash.MakeSeed(), []byte{'f','o','o','b','a','r'}) + } + + { + + _ = pkg.Bytes(maphash.MakeSeed(), []byte("foobar")) // want `avoid allocations with maphash\.String` + } + + { + + _ = pkg.Bytes(maphash.MakeSeed(), []byte{'f','o','o','b','a','r'}) + } + + { + + _ = maphash.String(maphash.MakeSeed(), string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with maphash\.Bytes` + } + + { + + _ = maphash.String(maphash.MakeSeed(), "foobar") + } + + { + + _ = String(maphash.MakeSeed(), string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with maphash\.Bytes` + } + + { + + _ = String(maphash.MakeSeed(), "foobar") + } + + { + + _ = pkg.String(maphash.MakeSeed(), string([]byte{'f','o','o','b','a','r'})) // want `avoid allocations with maphash\.Bytes` + } + + { + + _ = pkg.String(maphash.MakeSeed(), "foobar") + } + { h := maphash.Hash{} _,_ = h.Write([]byte("foobar")) // want `avoid allocations with \(\*maphash\.Hash\)\.WriteString`