extracted common code to helper file

This commit is contained in:
rkapka
2020-08-21 10:26:00 +02:00
parent 7e7b1c5ed7
commit e9ccea7360
8 changed files with 56 additions and 35 deletions

View File

@@ -0,0 +1,8 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["helper.go"],
importpath = "github.com/prysmaticlabs/prysm/tools/analyzers",
visibility = ["//visibility:public"],
)

View File

@@ -2,14 +2,13 @@ package comparesame
import (
"bytes"
"errors"
"go/ast"
"go/printer"
"go/token"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -26,16 +25,16 @@ var Analyzer = &analysis.Analyzer{
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
(*ast.BinaryExpr)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
expr, ok := node.(*ast.BinaryExpr)
if !ok {
return

View File

@@ -6,9 +6,9 @@ import (
"go/ast"
"strings"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -25,9 +25,9 @@ var Analyzer = &analysis.Analyzer{
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
@@ -40,7 +40,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
disallowedFns := []string{"NewSource", "New", "Seed", "Int63", "Uint32", "Uint64", "Int31", "Int",
"Int63n", "Int31n", "Intn", "Float64", "Float32", "Perm", "Shuffle", "Read"}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
switch stmt := node.(type) {
case *ast.File:
// Reset aliases (per file).

View File

@@ -3,15 +3,14 @@
package errcheck
import (
"errors"
"fmt"
"go/ast"
"go/token"
"go/types"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -63,9 +62,9 @@ func init() {
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
@@ -76,7 +75,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.AssignStmt)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
switch stmt := node.(type) {
case *ast.ExprStmt:
if call, ok := stmt.X.(*ast.CallExpr); ok {

View File

@@ -1,13 +1,12 @@
package featureconfig
import (
"errors"
"go/ast"
"go/token"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -22,9 +21,9 @@ var Analyzer = &analysis.Analyzer{
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
@@ -35,7 +34,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.AssignStmt)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
if ce, ok := node.(*ast.CallExpr); ok && isPkgDot(ce.Fun, "featureconfig", "Init") {
reportForbiddenUsage(pass, ce.Pos())
return

18
tools/analyzers/helper.go Normal file
View File

@@ -0,0 +1,18 @@
package analyzers
import (
"errors"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// GetInspector returns the inspector obtained from passing through inspect.Analyzer
func GetInspector(pass *analysis.Pass) (*inspector.Inspector, error) {
inspector, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
}
return inspector, nil
}

View File

@@ -1,13 +1,12 @@
package maligned
import (
"errors"
"go/ast"
"go/types"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -22,16 +21,16 @@ var Analyzer = &analysis.Analyzer{
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
(*ast.StructType)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
if s, ok := node.(*ast.StructType); ok {
if err := malign(node.Pos(), pass.TypesInfo.Types[s].Type.(*types.Struct)); err != nil {
pass.Reportf(node.Pos(), err.Error())

View File

@@ -1,12 +1,11 @@
package roughtime
import (
"errors"
"go/ast"
"github.com/prysmaticlabs/prysm/tools/analyzers"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
@@ -23,16 +22,16 @@ var Analyzer = &analysis.Analyzer{
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
inspector, err := analyzers.GetInspector(pass)
if err != nil {
return nil, err
}
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
inspector.Preorder(nodeFilter, func(node ast.Node) {
if ce, ok := node.(*ast.CallExpr); ok {
if isPkgDot(ce.Fun, "time", "Now") {
pass.Reportf(node.Pos(), "Do not use time.Now(), use "+