Skip to content

Commit ac883f9

Browse files
Merge pull request #51 from sashamelentyev/feat/check-http-method-in-ifstmt
feat: add check HTTP method in ifstmt
2 parents bbe9031 + 6624450 commit ac883f9

File tree

3 files changed

+257
-8
lines changed

3 files changed

+257
-8
lines changed

pkg/analyzer/analyzer.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,25 @@ func run(pass *analysis.Pass) (interface{}, error) {
205205
return
206206
}
207207

208-
if selectorExpr.Sel.Name != "StatusCode" {
209-
return
210-
}
211-
212208
basicLit, ok := binaryExpr.Y.(*ast.BasicLit)
213209
if !ok {
214210
return
215211
}
216212

217-
if !lookupFlag(pass, HTTPStatusCodeFlag) {
218-
return
219-
}
213+
switch selectorExpr.Sel.Name {
214+
case "StatusCode":
215+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
216+
return
217+
}
218+
219+
checkHTTPStatusCode(pass, basicLit)
220+
case "Method":
221+
if !lookupFlag(pass, HTTPMethodFlag) {
222+
return
223+
}
220224

221-
checkHTTPStatusCode(pass, basicLit)
225+
checkHTTPMethod(pass, basicLit)
226+
}
222227
}
223228
})
224229

pkg/analyzer/internal/template/test-httpmethod.go.tmpl

+28
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,31 @@ func _() {
3535
}
3636
{{- end }}
3737
}
38+
39+
{{ range $key, $value := .Mapping }}
40+
func _() error {
41+
resp, err := http.DefaultClient.Do(&http.Request{})
42+
if err != nil {
43+
return err
44+
}
45+
defer func() { _ = resp.Body.Close() }()
46+
if resp.Request.Method == "{{ $key }}" { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
47+
return nil
48+
}
49+
return nil
50+
}
51+
{{ end -}}
52+
53+
{{ range $key, $value := .Mapping }}
54+
func _() error {
55+
resp, err := http.DefaultClient.Do(&http.Request{})
56+
if err != nil {
57+
return err
58+
}
59+
defer func() { _ = resp.Body.Close() }()
60+
if resp.Request.Method == {{ $value }} {
61+
return nil
62+
}
63+
return nil
64+
}
65+
{{ end -}}

pkg/analyzer/testdata/src/a/http/method.go

+216
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)