Skip to content

Commit

Permalink
Merge pull request #81 from sashamelentyev/feat/forstmt
Browse files Browse the repository at this point in the history
feat: add check forstmt
  • Loading branch information
sashamelentyev committed Sep 25, 2022
2 parents 5da3638 + 83d199f commit 8d0bbb0
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pkg/analyzer/analyzer.go
Expand Up @@ -63,6 +63,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.CompositeLit)(nil),
(*ast.IfStmt)(nil),
(*ast.SwitchStmt)(nil),
(*ast.ForStmt)(nil),
}

insp.Preorder(types, func(node ast.Node) {
Expand Down Expand Up @@ -138,6 +139,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
} else {
switchStmtAsIfElseStmt(pass, n.Body.List)
}

case *ast.ForStmt:
cond, ok := n.Cond.(*ast.BinaryExpr)
if !ok {
return
}

x, ok := cond.X.(*ast.SelectorExpr)
if !ok {
return
}

y, ok := cond.Y.(*ast.BasicLit)
if !ok {
return
}

ifElseStmt(pass, x, y)
}
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/internal/gen.go
Expand Up @@ -50,8 +50,8 @@ func main() {
{
mapping: mapping.HTTPStatusCode,
packageName: "http_test",
templateName: "test-httpstatuscode.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/http/statuscode.go",
templateName: "test-httpstatus.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/http/status.go",
},
{
mapping: mapping.RPCDefaultPath,
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/internal/template/test-httpmethod.go.tmpl
Expand Up @@ -53,6 +53,11 @@ func _() error {
if resp.Request.Method == "{{ $key }}" { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return nil
}
{{- end }}
{{- range $key, $value := .Mapping }}
for resp.Request.Method == "{{ $key }}" { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return nil
}
{{- end }}
return nil
}
Expand All @@ -67,6 +72,11 @@ func _() error {
if resp.Request.Method == {{ $value }} {
return nil
}
{{- end }}
{{- range $key, $value := .Mapping }}
for resp.Request.Method == {{ $value }} {
return nil
}
{{- end }}
return nil
}
Expand Down
Expand Up @@ -69,6 +69,11 @@ func _() error {
} else if resp.StatusCode == {{ $key }} { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return nil
}
{{- end }}
{{- range $key, $value := .Mapping }}
for resp.StatusCode == {{ $key }} { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return nil
}
{{- end }}
return nil
}
Expand All @@ -85,6 +90,11 @@ func _() error {
} else if resp.StatusCode == {{ $value }} {
return nil
}
{{- end }}
{{- range $key, $value := .Mapping }}
for resp.StatusCode == {{ $value }} {
return nil
}
{{- end }}
return nil
}
Expand Down
54 changes: 54 additions & 0 deletions pkg/analyzer/testdata/src/a/http/method.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d0bbb0

Please sign in to comment.