|
| 1 | +// Copyright (C) 2019-2021 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package linter |
| 18 | + |
| 19 | +import ( |
| 20 | + "go/ast" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | +) |
| 25 | + |
| 26 | +const packageName string = "partitiontest" |
| 27 | +const functionName string = "PartitionTest" |
| 28 | +const fileNameSuffix string = "_test.go" |
| 29 | +const functionNamePrefix string = "Test" |
| 30 | +const parameterType string = "T" |
| 31 | + |
| 32 | +// Analyzer initilization |
| 33 | +var Analyzer = &analysis.Analyzer{ |
| 34 | + Name: "lint", |
| 35 | + Doc: "This custom linter checks inside files that end in '_test.go', and inside functions that start with 'Test' and have testing argument, for a line 'partitiontest.ParitionTest(<testing arg>)'", |
| 36 | + Run: run, |
| 37 | +} |
| 38 | + |
| 39 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 40 | + for _, f := range pass.Files { |
| 41 | + currentFileName := pass.Fset.File(f.Pos()).Name() |
| 42 | + if !strings.HasSuffix(currentFileName, fileNameSuffix) { |
| 43 | + continue |
| 44 | + } |
| 45 | + for _, decl := range f.Decls { |
| 46 | + fn, ok := decl.(*ast.FuncDecl) |
| 47 | + if !ok || fn.Recv != nil { |
| 48 | + // Ignore non-functions or functions with receivers. |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + // Check that function name starts with "Test" |
| 53 | + if !strings.HasPrefix(fn.Name.Name, functionNamePrefix) { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + if !isTestParameterInFunction(fn.Type.Params.List[0].Type, parameterType) { |
| 58 | + continue |
| 59 | + } |
| 60 | + if !isSearchLineInFunction(fn) { |
| 61 | + pass.Reportf(fn.Pos(), "%s function is missing %s.%s(<%s type parameter>)", fn.Name.Name, packageName, functionName, parameterType) |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + } |
| 66 | + return nil, nil |
| 67 | +} |
| 68 | + |
| 69 | +func isTestParameterInFunction(typ ast.Expr, wantType string) bool { |
| 70 | + ptr, ok := typ.(*ast.StarExpr) |
| 71 | + if !ok { |
| 72 | + // Not a pointer. |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + if name, ok := ptr.X.(*ast.Ident); ok { |
| 77 | + return name.Name == wantType |
| 78 | + } |
| 79 | + if sel, ok := ptr.X.(*ast.SelectorExpr); ok { |
| 80 | + return sel.Sel.Name == wantType |
| 81 | + } |
| 82 | + return false |
| 83 | +} |
| 84 | + |
| 85 | +func isSearchLineInFunction(fn *ast.FuncDecl) bool { |
| 86 | + for _, oneline := range fn.Body.List { |
| 87 | + if exprStmt, ok := oneline.(*ast.ExprStmt); ok { |
| 88 | + if call, ok := exprStmt.X.(*ast.CallExpr); ok { |
| 89 | + if fun, ok := call.Fun.(*ast.SelectorExpr); ok { |
| 90 | + if !doesPackageNameMatch(fun) { |
| 91 | + continue |
| 92 | + } |
| 93 | + if !doesFunctionNameMatch(fun) { |
| 94 | + continue |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if !doesParameterNameMatch(call, fn) { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return false |
| 107 | +} |
| 108 | + |
| 109 | +func doesPackageNameMatch(fun *ast.SelectorExpr) bool { |
| 110 | + if packageobject, ok := fun.X.(*ast.Ident); ok { |
| 111 | + if packageobject.Name == packageName { |
| 112 | + return true |
| 113 | + } |
| 114 | + } |
| 115 | + return false |
| 116 | +} |
| 117 | + |
| 118 | +func doesFunctionNameMatch(fun *ast.SelectorExpr) bool { |
| 119 | + return fun.Sel.Name == functionName |
| 120 | +} |
| 121 | + |
| 122 | +func doesParameterNameMatch(call *ast.CallExpr, fn *ast.FuncDecl) bool { |
| 123 | + for _, oneArg := range call.Args { |
| 124 | + |
| 125 | + if realArg, ok := oneArg.(*ast.Ident); ok { |
| 126 | + if realArg.Obj.Name == fn.Type.Params.List[0].Names[0].Obj.Name { |
| 127 | + return true |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return false |
| 132 | +} |
0 commit comments