Skip to content

Commit

Permalink
Merge pull request #3 from Antonboom/feat/another-yet-self-review
Browse files Browse the repository at this point in the history
self-review fixes: no panic, no extra cycles of analysis
  • Loading branch information
Antonboom authored Oct 3, 2023
2 parents 28ab901 + eb3bf2f commit 7caf9ef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
33 changes: 17 additions & 16 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"

"github.com/Antonboom/testifylint/internal/analysisutil"
Expand All @@ -27,10 +26,9 @@ func New() *analysis.Analyzer {
cfg := config.NewDefault()

analyzer := &analysis.Analyzer{
Name: name,
Doc: doc,
URL: url,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Name: name,
Doc: doc,
URL: url,
Run: func(pass *analysis.Pass) (any, error) {
regularCheckers, advancedCheckers, err := newCheckers(cfg)
if err != nil {
Expand All @@ -55,28 +53,31 @@ type testifyLint struct {
}

func (tl *testifyLint) run(pass *analysis.Pass) (any, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

filesToAnalysis := make([]*ast.File, 0, len(pass.Files))
for _, f := range pass.Files {
if !analysisutil.IsTestFile(pass.Fset, f) {
continue
}
if !analysisutil.Imports(f, testify.AssertPkgPath, testify.RequirePkgPath, testify.SuitePkgPath) {
continue
}
filesToAnalysis = append(filesToAnalysis, f)
}

// Regular checkers.
insp.Preorder([]ast.Node{(*ast.CallExpr)(nil)}, func(node ast.Node) {
tl.regularCheck(pass, node.(*ast.CallExpr))
})
insp := inspector.New(filesToAnalysis)

// Advanced checkers.
for _, ch := range tl.advancedCheckers {
for _, d := range ch.Check(pass, insp) {
pass.Report(d)
}
// Regular checkers.
insp.Preorder([]ast.Node{(*ast.CallExpr)(nil)}, func(node ast.Node) {
tl.regularCheck(pass, node.(*ast.CallExpr))
})

// Advanced checkers.
for _, ch := range tl.advancedCheckers {
for _, d := range ch.Check(pass, insp) {
pass.Report(d)
}
}

return nil, nil
}

Expand Down
14 changes: 14 additions & 0 deletions analyzer/testdata/src/not-test-file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNotFake(t *testing.T) {
var predicate bool

assert.Equal(t, true, predicate) // want "bool-compare: use assert\\.True"
assert.Equalf(t, true, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
}
14 changes: 14 additions & 0 deletions analyzer/testdata/src/not-test-file/file_test.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNotFake(t *testing.T) {
var predicate bool

assert.True(t, predicate) // want "bool-compare: use assert\\.True"
assert.Truef(t, predicate, "msg with args %d %s", 42, "42") // want "bool-compare: use assert\\.True"
}
4 changes: 0 additions & 4 deletions internal/checkers/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func newDiagnostic(
msg string,
fix *analysis.SuggestedFix,
) *analysis.Diagnostic {
if !rng.Pos().IsValid() || !rng.End().IsValid() {
panic("invalid report position")
}

d := analysis.Diagnostic{
Pos: rng.Pos(),
End: rng.End(),
Expand Down

0 comments on commit 7caf9ef

Please sign in to comment.