Skip to content

Commit 61bb831

Browse files
vsvasteyVladimir Sokolov
authored and
Vladimir Sokolov
committed
feat: add check HTTP Method and HTTP Status is SwitchStmt
1 parent 87d3556 commit 61bb831

File tree

5 files changed

+1215
-0
lines changed

5 files changed

+1215
-0
lines changed

pkg/analyzer/analyzer.go

+38
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
5454
(*ast.BasicLit)(nil),
5555
(*ast.CompositeLit)(nil),
5656
(*ast.IfStmt)(nil),
57+
(*ast.SwitchStmt)(nil),
5758
}
5859

5960
insp.Preorder(filter, func(node ast.Node) {
@@ -224,6 +225,43 @@ func run(pass *analysis.Pass) (interface{}, error) {
224225

225226
checkHTTPMethod(pass, basicLit)
226227
}
228+
229+
case *ast.SwitchStmt:
230+
selectorExpr, ok := n.Tag.(*ast.SelectorExpr)
231+
if !ok {
232+
return
233+
}
234+
235+
var checkFunc func(pass *analysis.Pass, basicLit *ast.BasicLit)
236+
237+
switch selectorExpr.Sel.Name {
238+
case "StatusCode":
239+
if !lookupFlag(pass, HTTPStatusCodeFlag) {
240+
return
241+
}
242+
checkFunc = checkHTTPStatusCode
243+
case "Method":
244+
if !lookupFlag(pass, HTTPMethodFlag) {
245+
return
246+
}
247+
checkFunc = checkHTTPMethod
248+
default:
249+
return
250+
}
251+
252+
for _, stmt := range n.Body.List {
253+
caseClause, ok := stmt.(*ast.CaseClause)
254+
if !ok {
255+
continue
256+
}
257+
for _, expr := range caseClause.List {
258+
basicLit, ok := expr.(*ast.BasicLit)
259+
if !ok {
260+
continue
261+
}
262+
checkFunc(pass, basicLit)
263+
}
264+
}
227265
}
228266
})
229267

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

+20
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ func _() error {
6363
return nil
6464
}
6565
{{ end -}}
66+
67+
{{ range $key, $value := .Mapping }}
68+
func _() {
69+
var r http.Request
70+
switch r.Method {
71+
case "{{ $key }}": // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
72+
return
73+
}
74+
}
75+
{{ end -}}
76+
77+
{{ range $key, $value := .Mapping }}
78+
func _() {
79+
var r http.Request
80+
switch r.Method {
81+
case {{ $value }}:
82+
return
83+
}
84+
}
85+
{{ end -}}

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

+21
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,24 @@ func _() {
126126
http.RedirectHandler("", {{ $value }})
127127
}
128128
{{ end -}}
129+
130+
131+
{{ range $key, $value := .Mapping }}
132+
func _() {
133+
var resp http.Response
134+
switch resp.StatusCode {
135+
case {{ $key }}: // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
136+
return
137+
}
138+
}
139+
{{ end -}}
140+
141+
{{ range $key, $value := .Mapping }}
142+
func _() {
143+
var resp http.Response
144+
switch resp.StatusCode {
145+
case {{ $value }}:
146+
return
147+
}
148+
}
149+
{{ end -}}

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

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

0 commit comments

Comments
 (0)