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

emptycase: allow to test this linter with external testdata #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
81 changes: 46 additions & 35 deletions emptycase.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package emptycase

import (
"github.com/gostaticanalysis/comment"
"go/ast"
"strings"

"github.com/gostaticanalysis/comment"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
Expand All @@ -16,53 +17,63 @@ const doc = "emptycase find empty cases"
var Analyzer = &analysis.Analyzer{
Name: "emptycase",
Doc: doc,
Run: run,
Run: run(false),
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

var analyzerTest = false

func run(pass *analysis.Pass) (interface{}, error) {
commentMap := comment.New(pass.Fset, pass.Files)
// AnalyzerForTesting is ...
var AnalyzerForTesting = &analysis.Analyzer{
Name: "emptycase",
Doc: doc,
Run: run(true),
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

// Report a case with no body.
// However, if a comment is attached to it, it is considered an intentional empty case and not reported.
var emptyCaseChecker = func(caseNode ast.Node, body []ast.Stmt) {
if len(body) != 0 {
return
}
func run(analyzerTest bool) func(pass *analysis.Pass) (interface{}, error) {
return func(pass *analysis.Pass) (interface{}, error) {
commentMap := comment.New(pass.Fset, pass.Files)

commentGroup := commentMap.Comments(caseNode)
if len(commentGroup) == 0 {
pass.Reportf(caseNode.Pos(), "empty case")
return
}
// Report a case with no body.
// However, if a comment is attached to it, it is considered an intentional empty case and not reported.
var emptyCaseChecker = func(caseNode ast.Node, body []ast.Stmt) {
if len(body) != 0 {
return
}

if analyzerTest {
// Comments that indicate the expected value of the analyzer test are not considered comments.
if len(commentGroup) == 1 && strings.HasPrefix(strings.TrimLeft(commentGroup[0].List[0].Text[2:], " "), "want") {
commentGroup := commentMap.Comments(caseNode)
if len(commentGroup) == 0 {
pass.Reportf(caseNode.Pos(), "empty case")
return
}
}
}

inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if analyzerTest {
// Comments that indicate the expected value of the analyzer test are not considered comments.
if len(commentGroup) == 1 && strings.HasPrefix(strings.TrimLeft(commentGroup[0].List[0].Text[2:], " "), "want") {
pass.Reportf(caseNode.Pos(), "empty case")
}
}
}

nodeFilter := []ast.Node{
(*ast.CaseClause)(nil),
(*ast.CommClause)(nil),
}
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.CaseClause:
emptyCaseChecker(n, n.Body)
case *ast.CommClause:
emptyCaseChecker(n, n.Body)
nodeFilter := []ast.Node{
(*ast.CaseClause)(nil),
(*ast.CommClause)(nil),
}
})

return nil, nil
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.CaseClause:
emptyCaseChecker(n, n.Body)
case *ast.CommClause:
emptyCaseChecker(n, n.Body)
}
})

return nil, nil
}
}
3 changes: 1 addition & 2 deletions emptycase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ import (
// TestAnalyzer is a analyzerTest for Analyzer.
func TestAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analyzerTest = true
analysistest.Run(t, testdata, Analyzer, "a")
analysistest.Run(t, testdata, AnalyzerForTesting, "a")
}