From a5d7611a5387b29124bc7dc90d7f4f5eea91aad1 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Sun, 7 Aug 2022 15:04:10 +0300 Subject: [PATCH] fix: convert HTTP method current value to upper --- pkg/analyzer/analyzer.go | 2 +- pkg/analyzer/internal/gen.go | 12 ++- .../internal/template/test-issue32.go.tmpl | 37 +++++++++ pkg/analyzer/testdata/src/a/http/issue32.go | 83 +++++++++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 pkg/analyzer/internal/template/test-issue32.go.tmpl create mode 100755 pkg/analyzer/testdata/src/a/http/issue32.go diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index d3ef70d..d3781c9 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -158,7 +158,7 @@ func lookupFlag(pass *analysis.Pass, name string) bool { } func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) { - currentVal := getBasicLitValue(basicLit) + currentVal := strings.ToUpper(getBasicLitValue(basicLit)) if newVal, ok := mapping.HTTPMethod[currentVal]; ok { report(pass, basicLit.Pos(), currentVal, newVal) diff --git a/pkg/analyzer/internal/gen.go b/pkg/analyzer/internal/gen.go index 3a99322..45d5117 100644 --- a/pkg/analyzer/internal/gen.go +++ b/pkg/analyzer/internal/gen.go @@ -10,6 +10,7 @@ import ( "log" "os" "regexp" + "strings" "text/template" "github.com/sashamelentyev/usestdlibvars/pkg/analyzer/internal/mapping" @@ -21,7 +22,10 @@ var templateDir embed.FS func main() { t := template.Must( template.New("template"). - Funcs(map[string]any{"quoteMeta": regexp.QuoteMeta}). + Funcs(map[string]any{ + "quoteMeta": regexp.QuoteMeta, + "lower": strings.ToLower, + }). ParseFS(templateDir, "template/*.tmpl"), ) @@ -73,6 +77,12 @@ func main() { templateName: "test-template.go.tmpl", fileName: "pkg/analyzer/testdata/src/a/time/layout.go", }, + { + mapping: mapping.HTTPMethod, + packageName: "http_test", + templateName: "test-issue32.go.tmpl", + fileName: "pkg/analyzer/testdata/src/a/http/issue32.go", + }, } for _, operation := range operations { diff --git a/pkg/analyzer/internal/template/test-issue32.go.tmpl b/pkg/analyzer/internal/template/test-issue32.go.tmpl new file mode 100644 index 0000000..d1ea87a --- /dev/null +++ b/pkg/analyzer/internal/template/test-issue32.go.tmpl @@ -0,0 +1,37 @@ +// Code generated by usestdlibvars, DO NOT EDIT. + +package {{ .PackageName }} + +import "net/http" + +var ( +{{- range $key, $value := .Mapping }} + _ = "{{ lower $key }}" +{{- end }} +) + +const ( +{{- range $key, $value := .Mapping }} + _ = "{{ lower $key }}" +{{- end }} +) + +func _() { +{{- range $key, $value := .Mapping }} + _, _ = http.NewRequest("{{ lower $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` +{{- end }} +} + +func _() { +{{- range $key, $value := .Mapping }} + _, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", nil) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` +{{- end }} +} + +func _() { +{{- range $key, $value := .Mapping }} + _ = &http.Request{ + Method: "{{ lower $key }}", // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` + } +{{- end }} +} diff --git a/pkg/analyzer/testdata/src/a/http/issue32.go b/pkg/analyzer/testdata/src/a/http/issue32.go new file mode 100755 index 0000000..f3aa091 --- /dev/null +++ b/pkg/analyzer/testdata/src/a/http/issue32.go @@ -0,0 +1,83 @@ +// Code generated by usestdlibvars, DO NOT EDIT. + +package http_test + +import "net/http" + +var ( + _ = "connect" + _ = "delete" + _ = "get" + _ = "head" + _ = "options" + _ = "patch" + _ = "post" + _ = "put" + _ = "trace" +) + +const ( + _ = "connect" + _ = "delete" + _ = "get" + _ = "head" + _ = "options" + _ = "patch" + _ = "post" + _ = "put" + _ = "trace" +) + +func _() { + _, _ = http.NewRequest("connect", "", nil) // want `"CONNECT" can be replaced by http\.MethodConnect` + _, _ = http.NewRequest("delete", "", nil) // want `"DELETE" can be replaced by http\.MethodDelete` + _, _ = http.NewRequest("get", "", nil) // want `"GET" can be replaced by http\.MethodGet` + _, _ = http.NewRequest("head", "", nil) // want `"HEAD" can be replaced by http\.MethodHead` + _, _ = http.NewRequest("options", "", nil) // want `"OPTIONS" can be replaced by http\.MethodOptions` + _, _ = http.NewRequest("patch", "", nil) // want `"PATCH" can be replaced by http\.MethodPatch` + _, _ = http.NewRequest("post", "", nil) // want `"POST" can be replaced by http\.MethodPost` + _, _ = http.NewRequest("put", "", nil) // want `"PUT" can be replaced by http\.MethodPut` + _, _ = http.NewRequest("trace", "", nil) // want `"TRACE" can be replaced by http\.MethodTrace` +} + +func _() { + _, _ = http.NewRequestWithContext(nil, "connect", "", nil) // want `"CONNECT" can be replaced by http\.MethodConnect` + _, _ = http.NewRequestWithContext(nil, "delete", "", nil) // want `"DELETE" can be replaced by http\.MethodDelete` + _, _ = http.NewRequestWithContext(nil, "get", "", nil) // want `"GET" can be replaced by http\.MethodGet` + _, _ = http.NewRequestWithContext(nil, "head", "", nil) // want `"HEAD" can be replaced by http\.MethodHead` + _, _ = http.NewRequestWithContext(nil, "options", "", nil) // want `"OPTIONS" can be replaced by http\.MethodOptions` + _, _ = http.NewRequestWithContext(nil, "patch", "", nil) // want `"PATCH" can be replaced by http\.MethodPatch` + _, _ = http.NewRequestWithContext(nil, "post", "", nil) // want `"POST" can be replaced by http\.MethodPost` + _, _ = http.NewRequestWithContext(nil, "put", "", nil) // want `"PUT" can be replaced by http\.MethodPut` + _, _ = http.NewRequestWithContext(nil, "trace", "", nil) // want `"TRACE" can be replaced by http\.MethodTrace` +} + +func _() { + _ = &http.Request{ + Method: "connect", // want `"CONNECT" can be replaced by http\.MethodConnect` + } + _ = &http.Request{ + Method: "delete", // want `"DELETE" can be replaced by http\.MethodDelete` + } + _ = &http.Request{ + Method: "get", // want `"GET" can be replaced by http\.MethodGet` + } + _ = &http.Request{ + Method: "head", // want `"HEAD" can be replaced by http\.MethodHead` + } + _ = &http.Request{ + Method: "options", // want `"OPTIONS" can be replaced by http\.MethodOptions` + } + _ = &http.Request{ + Method: "patch", // want `"PATCH" can be replaced by http\.MethodPatch` + } + _ = &http.Request{ + Method: "post", // want `"POST" can be replaced by http\.MethodPost` + } + _ = &http.Request{ + Method: "put", // want `"PUT" can be replaced by http\.MethodPut` + } + _ = &http.Request{ + Method: "trace", // want `"TRACE" can be replaced by http\.MethodTrace` + } +}