github.com/prysmaticlabs/prysm@v1.4.4/tools/analyzers/featureconfig/analyzer.go (about)

     1  // Package featureconfig implements a static analyzer to prevent leaking globals in tests.
     2  package featureconfig
     3  
     4  import (
     5  	"errors"
     6  	"go/ast"
     7  	"go/token"
     8  
     9  	"golang.org/x/tools/go/analysis"
    10  	"golang.org/x/tools/go/analysis/passes/inspect"
    11  	"golang.org/x/tools/go/ast/inspector"
    12  )
    13  
    14  // Doc explaining the tool.
    15  const Doc = "Enforce usage of featureconfig.InitWithReset to prevent leaking globals in tests."
    16  
    17  // Analyzer runs static analysis.
    18  var Analyzer = &analysis.Analyzer{
    19  	Name:     "featureconfig",
    20  	Doc:      Doc,
    21  	Requires: []*analysis.Analyzer{inspect.Analyzer},
    22  	Run:      run,
    23  }
    24  
    25  func run(pass *analysis.Pass) (interface{}, error) {
    26  	inspection, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
    27  	if !ok {
    28  		return nil, errors.New("analyzer is not type *inspector.Inspector")
    29  	}
    30  
    31  	nodeFilter := []ast.Node{
    32  		(*ast.CallExpr)(nil),
    33  		(*ast.ExprStmt)(nil),
    34  		(*ast.GoStmt)(nil),
    35  		(*ast.DeferStmt)(nil),
    36  		(*ast.AssignStmt)(nil),
    37  	}
    38  
    39  	inspection.Preorder(nodeFilter, func(node ast.Node) {
    40  		if ce, ok := node.(*ast.CallExpr); ok && isPkgDot(ce.Fun, "featureconfig", "Init") {
    41  			reportForbiddenUsage(pass, ce.Pos())
    42  			return
    43  		}
    44  		switch stmt := node.(type) {
    45  		case *ast.ExprStmt:
    46  			if call, ok := stmt.X.(*ast.CallExpr); ok && isPkgDot(call.Fun, "featureconfig", "InitWithReset") {
    47  				reportUnhandledReset(pass, call.Lparen)
    48  			}
    49  		case *ast.GoStmt:
    50  			if isPkgDot(stmt.Call, "featureconfig", "InitWithReset") {
    51  				reportUnhandledReset(pass, stmt.Call.Lparen)
    52  			}
    53  		case *ast.DeferStmt:
    54  			if isPkgDot(stmt.Call, "featureconfig", "InitWithReset") {
    55  				reportUnhandledReset(pass, stmt.Call.Lparen)
    56  			}
    57  		case *ast.AssignStmt:
    58  			if ce, ok := stmt.Rhs[0].(*ast.CallExpr); ok && isPkgDot(ce.Fun, "featureconfig", "InitWithReset") {
    59  				for i := 0; i < len(stmt.Lhs); i++ {
    60  					if id, ok := stmt.Lhs[i].(*ast.Ident); ok {
    61  						if id.Name == "_" {
    62  							reportUnhandledReset(pass, id.NamePos)
    63  						}
    64  					}
    65  				}
    66  			}
    67  		default:
    68  		}
    69  	})
    70  
    71  	return nil, nil
    72  }
    73  
    74  func reportForbiddenUsage(pass *analysis.Pass, pos token.Pos) {
    75  	pass.Reportf(pos, "Use of featureconfig.Init is forbidden in test code. Please use "+
    76  		"featureconfig.InitWithReset and call reset in the same test function.")
    77  }
    78  
    79  func reportUnhandledReset(pass *analysis.Pass, pos token.Pos) {
    80  	pass.Reportf(pos, "Unhandled reset featureconfig not found in test "+
    81  		"method. Be sure to call the returned reset function from featureconfig.InitWithReset "+
    82  		"within this test method.")
    83  }
    84  
    85  func isPkgDot(expr ast.Expr, pkg, name string) bool {
    86  	sel, ok := expr.(*ast.SelectorExpr)
    87  	return ok && isIdent(sel.X, pkg) && isIdent(sel.Sel, name)
    88  }
    89  
    90  func isIdent(expr ast.Expr, ident string) bool {
    91  	id, ok := expr.(*ast.Ident)
    92  	return ok && id.Name == ident
    93  }