From de2e494d408745593a3ae0f9435ea6d6462b6a9c Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Sun, 7 Aug 2022 18:16:43 +0300 Subject: [PATCH 1/2] feat: add lower HTTP method in issue --- pkg/analyzer/analyzer.go | 4 +- .../internal/template/test-issue32.go.tmpl | 6 +-- pkg/analyzer/testdata/src/a/http/issue32.go | 54 +++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 27a9f30..b5ba36d 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -168,9 +168,9 @@ func lookupFlag(pass *analysis.Pass, name string) bool { } func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) { - currentVal := strings.ToUpper(getBasicLitValue(basicLit)) + currentVal := getBasicLitValue(basicLit) - if newVal, ok := mapping.HTTPMethod[currentVal]; ok { + if newVal, ok := mapping.HTTPMethod[strings.ToUpper(currentVal)]; ok { report(pass, basicLit.Pos(), currentVal, newVal) } } diff --git a/pkg/analyzer/internal/template/test-issue32.go.tmpl b/pkg/analyzer/internal/template/test-issue32.go.tmpl index b195e84..ff6225b 100644 --- a/pkg/analyzer/internal/template/test-issue32.go.tmpl +++ b/pkg/analyzer/internal/template/test-issue32.go.tmpl @@ -18,20 +18,20 @@ const ( func _() { {{- range $key, $value := .Mapping }} - _, _ = http.NewRequest("{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` + _, _ = http.NewRequest("{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta (lower $key) }}" can be replaced by {{ quoteMeta $value }}` {{- end }} } func _() { {{- range $key, $value := .Mapping }} - _, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}` + _, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta (lower $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 }}` + Method: "{{ lower $key }}", // want `"{{ quoteMeta (lower $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 index 8046415..fcd9b30 100755 --- a/pkg/analyzer/testdata/src/a/http/issue32.go +++ b/pkg/analyzer/testdata/src/a/http/issue32.go @@ -29,55 +29,55 @@ const ( ) func _() { - _, _ = http.NewRequest("connect", "", http.NoBody) // want `"CONNECT" can be replaced by http\.MethodConnect` - _, _ = http.NewRequest("delete", "", http.NoBody) // want `"DELETE" can be replaced by http\.MethodDelete` - _, _ = http.NewRequest("get", "", http.NoBody) // want `"GET" can be replaced by http\.MethodGet` - _, _ = http.NewRequest("head", "", http.NoBody) // want `"HEAD" can be replaced by http\.MethodHead` - _, _ = http.NewRequest("options", "", http.NoBody) // want `"OPTIONS" can be replaced by http\.MethodOptions` - _, _ = http.NewRequest("patch", "", http.NoBody) // want `"PATCH" can be replaced by http\.MethodPatch` - _, _ = http.NewRequest("post", "", http.NoBody) // want `"POST" can be replaced by http\.MethodPost` - _, _ = http.NewRequest("put", "", http.NoBody) // want `"PUT" can be replaced by http\.MethodPut` - _, _ = http.NewRequest("trace", "", http.NoBody) // want `"TRACE" can be replaced by http\.MethodTrace` + _, _ = http.NewRequest("connect", "", http.NoBody) // want `"connect" can be replaced by http\.MethodConnect` + _, _ = http.NewRequest("delete", "", http.NoBody) // want `"delete" can be replaced by http\.MethodDelete` + _, _ = http.NewRequest("get", "", http.NoBody) // want `"get" can be replaced by http\.MethodGet` + _, _ = http.NewRequest("head", "", http.NoBody) // want `"head" can be replaced by http\.MethodHead` + _, _ = http.NewRequest("options", "", http.NoBody) // want `"options" can be replaced by http\.MethodOptions` + _, _ = http.NewRequest("patch", "", http.NoBody) // want `"patch" can be replaced by http\.MethodPatch` + _, _ = http.NewRequest("post", "", http.NoBody) // want `"post" can be replaced by http\.MethodPost` + _, _ = http.NewRequest("put", "", http.NoBody) // want `"put" can be replaced by http\.MethodPut` + _, _ = http.NewRequest("trace", "", http.NoBody) // want `"trace" can be replaced by http\.MethodTrace` } func _() { - _, _ = http.NewRequestWithContext(nil, "connect", "", http.NoBody) // want `"CONNECT" can be replaced by http\.MethodConnect` - _, _ = http.NewRequestWithContext(nil, "delete", "", http.NoBody) // want `"DELETE" can be replaced by http\.MethodDelete` - _, _ = http.NewRequestWithContext(nil, "get", "", http.NoBody) // want `"GET" can be replaced by http\.MethodGet` - _, _ = http.NewRequestWithContext(nil, "head", "", http.NoBody) // want `"HEAD" can be replaced by http\.MethodHead` - _, _ = http.NewRequestWithContext(nil, "options", "", http.NoBody) // want `"OPTIONS" can be replaced by http\.MethodOptions` - _, _ = http.NewRequestWithContext(nil, "patch", "", http.NoBody) // want `"PATCH" can be replaced by http\.MethodPatch` - _, _ = http.NewRequestWithContext(nil, "post", "", http.NoBody) // want `"POST" can be replaced by http\.MethodPost` - _, _ = http.NewRequestWithContext(nil, "put", "", http.NoBody) // want `"PUT" can be replaced by http\.MethodPut` - _, _ = http.NewRequestWithContext(nil, "trace", "", http.NoBody) // want `"TRACE" can be replaced by http\.MethodTrace` + _, _ = http.NewRequestWithContext(nil, "connect", "", http.NoBody) // want `"connect" can be replaced by http\.MethodConnect` + _, _ = http.NewRequestWithContext(nil, "delete", "", http.NoBody) // want `"delete" can be replaced by http\.MethodDelete` + _, _ = http.NewRequestWithContext(nil, "get", "", http.NoBody) // want `"get" can be replaced by http\.MethodGet` + _, _ = http.NewRequestWithContext(nil, "head", "", http.NoBody) // want `"head" can be replaced by http\.MethodHead` + _, _ = http.NewRequestWithContext(nil, "options", "", http.NoBody) // want `"options" can be replaced by http\.MethodOptions` + _, _ = http.NewRequestWithContext(nil, "patch", "", http.NoBody) // want `"patch" can be replaced by http\.MethodPatch` + _, _ = http.NewRequestWithContext(nil, "post", "", http.NoBody) // want `"post" can be replaced by http\.MethodPost` + _, _ = http.NewRequestWithContext(nil, "put", "", http.NoBody) // want `"put" can be replaced by http\.MethodPut` + _, _ = http.NewRequestWithContext(nil, "trace", "", http.NoBody) // want `"trace" can be replaced by http\.MethodTrace` } func _() { _ = &http.Request{ - Method: "connect", // want `"CONNECT" can be replaced by http\.MethodConnect` + Method: "connect", // want `"connect" can be replaced by http\.MethodConnect` } _ = &http.Request{ - Method: "delete", // want `"DELETE" can be replaced by http\.MethodDelete` + Method: "delete", // want `"delete" can be replaced by http\.MethodDelete` } _ = &http.Request{ - Method: "get", // want `"GET" can be replaced by http\.MethodGet` + Method: "get", // want `"get" can be replaced by http\.MethodGet` } _ = &http.Request{ - Method: "head", // want `"HEAD" can be replaced by http\.MethodHead` + Method: "head", // want `"head" can be replaced by http\.MethodHead` } _ = &http.Request{ - Method: "options", // want `"OPTIONS" can be replaced by http\.MethodOptions` + Method: "options", // want `"options" can be replaced by http\.MethodOptions` } _ = &http.Request{ - Method: "patch", // want `"PATCH" can be replaced by http\.MethodPatch` + Method: "patch", // want `"patch" can be replaced by http\.MethodPatch` } _ = &http.Request{ - Method: "post", // want `"POST" can be replaced by http\.MethodPost` + Method: "post", // want `"post" can be replaced by http\.MethodPost` } _ = &http.Request{ - Method: "put", // want `"PUT" can be replaced by http\.MethodPut` + Method: "put", // want `"put" can be replaced by http\.MethodPut` } _ = &http.Request{ - Method: "trace", // want `"TRACE" can be replaced by http\.MethodTrace` + Method: "trace", // want `"trace" can be replaced by http\.MethodTrace` } } From 8bd2bfe6c157d1304c66113abd2e1e1b683cd033 Mon Sep 17 00:00:00 2001 From: Sasha Melentyev Date: Sun, 7 Aug 2022 18:19:21 +0300 Subject: [PATCH 2/2] feat: rename lower func in gen --- pkg/analyzer/internal/gen.go | 2 +- pkg/analyzer/internal/template/test-issue32.go.tmpl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/analyzer/internal/gen.go b/pkg/analyzer/internal/gen.go index 961de79..1ae3b93 100644 --- a/pkg/analyzer/internal/gen.go +++ b/pkg/analyzer/internal/gen.go @@ -24,7 +24,7 @@ func main() { template.New("template"). Funcs(map[string]any{ "quoteMeta": regexp.QuoteMeta, - "lower": strings.ToLower, + "toLower": strings.ToLower, }). ParseFS(templateDir, "template/*.tmpl"), ) diff --git a/pkg/analyzer/internal/template/test-issue32.go.tmpl b/pkg/analyzer/internal/template/test-issue32.go.tmpl index ff6225b..467aad6 100644 --- a/pkg/analyzer/internal/template/test-issue32.go.tmpl +++ b/pkg/analyzer/internal/template/test-issue32.go.tmpl @@ -6,32 +6,32 @@ import "net/http" var ( {{- range $key, $value := .Mapping }} - _ = "{{ lower $key }}" + _ = "{{ toLower $key }}" {{- end }} ) const ( {{- range $key, $value := .Mapping }} - _ = "{{ lower $key }}" + _ = "{{ toLower $key }}" {{- end }} ) func _() { {{- range $key, $value := .Mapping }} - _, _ = http.NewRequest("{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta (lower $key) }}" can be replaced by {{ quoteMeta $value }}` + _, _ = http.NewRequest("{{ toLower $key }}", "", http.NoBody) // want `"{{ quoteMeta (toLower $key) }}" can be replaced by {{ quoteMeta $value }}` {{- end }} } func _() { {{- range $key, $value := .Mapping }} - _, _ = http.NewRequestWithContext(nil, "{{ lower $key }}", "", http.NoBody) // want `"{{ quoteMeta (lower $key) }}" can be replaced by {{ quoteMeta $value }}` + _, _ = http.NewRequestWithContext(nil, "{{ toLower $key }}", "", http.NoBody) // want `"{{ quoteMeta (toLower $key) }}" can be replaced by {{ quoteMeta $value }}` {{- end }} } func _() { {{- range $key, $value := .Mapping }} _ = &http.Request{ - Method: "{{ lower $key }}", // want `"{{ quoteMeta (lower $key) }}" can be replaced by {{ quoteMeta $value }}` + Method: "{{ toLower $key }}", // want `"{{ quoteMeta (toLower $key) }}" can be replaced by {{ quoteMeta $value }}` } {{- end }} }