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

Detect if the deprecated RegisterLint method was used #770

Closed
wants to merge 7 commits into from
Closed
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
71 changes: 71 additions & 0 deletions v3/lint/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ package lint
*/

import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"testing"

Expand Down Expand Up @@ -484,3 +494,64 @@ func TestRegistryFilter(t *testing.T) {
})
}
}

func TestEnsureDeprecatedRegisterLintIsNotUsed(t *testing.T) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("runtime.Caller(0) did not find info")
}

lintPath := path.Join(path.Dir(filename), "..", "lints")
if err := filepath.WalkDir(lintPath, func(path string, d fs.DirEntry, err error) error {
// Skip directories
if d.IsDir() {
return nil
}
// Skip files that do not end with .go
if filepath.Ext(path) != ".go" {
return nil
}
return registerLintNotUsed(t, path)
}); err != nil {
t.Fatal(err)
}
}

func registerLintNotUsed(tb testing.TB, filePath string) error {
tb.Helper()

src, err := os.ReadFile(filePath)
if err != nil {
return err
}

fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filePath, src, parser.AllErrors)
if err != nil {
return fmt.Errorf("error parsing file: %v", err)
}

visitor := &lintVisitor{}
ast.Walk(visitor, file)
if visitor.err != nil {
return fmt.Errorf("error walking through AST of file %v: %v", filePath, visitor.err)
}

return nil
}

type lintVisitor struct {
err error // Used to pass error back to the caller.
}

func (v *lintVisitor) Visit(node ast.Node) ast.Visitor {
functionCall, ok := node.(*ast.SelectorExpr)
if !ok {
return v
}
if functionCall.Sel.Name == "RegisterLint" {
v.err = errors.New("RegisterLint is deprecated and should not be used")
return nil
}
return v
}
Loading