Skip to content

Commit 8d0bbb0

Browse files
Merge pull request #81 from sashamelentyev/feat/forstmt
feat: add check forstmt
2 parents 5da3638 + 83d199f commit 8d0bbb0

File tree

6 files changed

+467
-2
lines changed

6 files changed

+467
-2
lines changed

pkg/analyzer/analyzer.go

+19
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
6363
(*ast.CompositeLit)(nil),
6464
(*ast.IfStmt)(nil),
6565
(*ast.SwitchStmt)(nil),
66+
(*ast.ForStmt)(nil),
6667
}
6768

6869
insp.Preorder(types, func(node ast.Node) {
@@ -138,6 +139,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
138139
} else {
139140
switchStmtAsIfElseStmt(pass, n.Body.List)
140141
}
142+
143+
case *ast.ForStmt:
144+
cond, ok := n.Cond.(*ast.BinaryExpr)
145+
if !ok {
146+
return
147+
}
148+
149+
x, ok := cond.X.(*ast.SelectorExpr)
150+
if !ok {
151+
return
152+
}
153+
154+
y, ok := cond.Y.(*ast.BasicLit)
155+
if !ok {
156+
return
157+
}
158+
159+
ifElseStmt(pass, x, y)
141160
}
142161
})
143162

pkg/analyzer/internal/gen.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func main() {
5050
{
5151
mapping: mapping.HTTPStatusCode,
5252
packageName: "http_test",
53-
templateName: "test-httpstatuscode.go.tmpl",
54-
fileName: "pkg/analyzer/testdata/src/a/http/statuscode.go",
53+
templateName: "test-httpstatus.go.tmpl",
54+
fileName: "pkg/analyzer/testdata/src/a/http/status.go",
5555
},
5656
{
5757
mapping: mapping.RPCDefaultPath,

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

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func _() error {
5353
if resp.Request.Method == "{{ $key }}" { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
5454
return nil
5555
}
56+
{{- end }}
57+
{{- range $key, $value := .Mapping }}
58+
for resp.Request.Method == "{{ $key }}" { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
59+
return nil
60+
}
5661
{{- end }}
5762
return nil
5863
}
@@ -67,6 +72,11 @@ func _() error {
6772
if resp.Request.Method == {{ $value }} {
6873
return nil
6974
}
75+
{{- end }}
76+
{{- range $key, $value := .Mapping }}
77+
for resp.Request.Method == {{ $value }} {
78+
return nil
79+
}
7080
{{- end }}
7181
return nil
7282
}

pkg/analyzer/internal/template/test-httpstatuscode.go.tmpl renamed to pkg/analyzer/internal/template/test-httpstatus.go.tmpl

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func _() error {
6969
} else if resp.StatusCode == {{ $key }} { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
7070
return nil
7171
}
72+
{{- end }}
73+
{{- range $key, $value := .Mapping }}
74+
for resp.StatusCode == {{ $key }} { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
75+
return nil
76+
}
7277
{{- end }}
7378
return nil
7479
}
@@ -85,6 +90,11 @@ func _() error {
8590
} else if resp.StatusCode == {{ $value }} {
8691
return nil
8792
}
93+
{{- end }}
94+
{{- range $key, $value := .Mapping }}
95+
for resp.StatusCode == {{ $value }} {
96+
return nil
97+
}
8898
{{- end }}
8999
return nil
90100
}

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

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

0 commit comments

Comments
 (0)