Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check forstmt #81

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.

Loading