Skip to content

Commit

Permalink
Merge pull request #54 from sashamelentyev/feat/more-cases-for-switch…
Browse files Browse the repository at this point in the history
…stmt

feat: add more cases for switch stmt
  • Loading branch information
sashamelentyev committed Aug 21, 2022
2 parents 1796d3a + 0b092cc commit f729765
Show file tree
Hide file tree
Showing 5 changed files with 1,240 additions and 24 deletions.
87 changes: 64 additions & 23 deletions pkg/analyzer/analyzer.go
Expand Up @@ -228,38 +228,79 @@ func run(pass *analysis.Pass) (interface{}, error) {

case *ast.SwitchStmt:
selectorExpr, ok := n.Tag.(*ast.SelectorExpr)
if !ok {
return
}
if ok {
var checkFunc func(pass *analysis.Pass, basicLit *ast.BasicLit)

var checkFunc func(pass *analysis.Pass, basicLit *ast.BasicLit)
switch selectorExpr.Sel.Name {
case "StatusCode":
if !lookupFlag(pass, HTTPStatusCodeFlag) {
return
}

switch selectorExpr.Sel.Name {
case "StatusCode":
if !lookupFlag(pass, HTTPStatusCodeFlag) {
return
}
checkFunc = checkHTTPStatusCode
case "Method":
if !lookupFlag(pass, HTTPMethodFlag) {
checkFunc = checkHTTPStatusCode
case "Method":
if !lookupFlag(pass, HTTPMethodFlag) {
return
}

checkFunc = checkHTTPMethod
default:
return
}
checkFunc = checkHTTPMethod
default:
return
}

for _, stmt := range n.Body.List {
caseClause, ok := stmt.(*ast.CaseClause)
if !ok {
continue
for _, stmt := range n.Body.List {
caseClause, ok := stmt.(*ast.CaseClause)
if !ok {
continue
}

for _, expr := range caseClause.List {
basicLit, ok := expr.(*ast.BasicLit)
if !ok {
continue
}

checkFunc(pass, basicLit)
}
}
for _, expr := range caseClause.List {
basicLit, ok := expr.(*ast.BasicLit)
} else {
for _, stmt := range n.Body.List {
caseClause, ok := stmt.(*ast.CaseClause)
if !ok {
continue
}
checkFunc(pass, basicLit)

for _, expr := range caseClause.List {
binaryExpr, ok := expr.(*ast.BinaryExpr)
if !ok {
continue
}

selectorExpr, ok := binaryExpr.X.(*ast.SelectorExpr)
if !ok {
continue
}

basicLit, ok := binaryExpr.Y.(*ast.BasicLit)
if !ok {
continue
}

switch selectorExpr.Sel.Name {
case "StatusCode":
if !lookupFlag(pass, HTTPStatusCodeFlag) {
continue
}

checkHTTPStatusCode(pass, basicLit)
case "Method":
if !lookupFlag(pass, HTTPMethodFlag) {
continue
}

checkHTTPMethod(pass, basicLit)
}
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/analyzer/internal/template/test-httpmethod.go.tmpl
Expand Up @@ -83,3 +83,23 @@ func _() {
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
var r http.Request
switch {
case r.Method == "{{ $key }}": // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
var r http.Request
switch {
case r.Method == {{ $value }}:
return
}
}
{{ end -}}
21 changes: 20 additions & 1 deletion pkg/analyzer/internal/template/test-httpstatuscode.go.tmpl
Expand Up @@ -127,7 +127,6 @@ func _() {
}
{{ end -}}


{{ range $key, $value := .Mapping }}
func _() {
var resp http.Response
Expand All @@ -147,3 +146,23 @@ func _() {
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
var resp http.Response
switch {
case resp.StatusCode == {{ $key }}: // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
return
}
}
{{ end -}}

{{ range $key, $value := .Mapping }}
func _() {
var resp http.Response
switch {
case resp.StatusCode == {{ $value }}:
return
}
}
{{ end -}}
144 changes: 144 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 f729765

Please sign in to comment.