Skip to content

Commit

Permalink
chore: new util function to check types
Browse files Browse the repository at this point in the history
  • Loading branch information
nunnatsa committed Mar 24, 2024
1 parent 9724af7 commit 5ce34c8
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions linter/ginkgo_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func checkAssignments(pass *analysis.Pass, list []ast.Stmt) bool {

case *ast.AssignStmt:
for i, val := range st.Rhs {
if _, isFunc := val.(*ast.FuncLit); !isFunc {
if !is[*ast.FuncLit](val) {
if id, isIdent := st.Lhs[i].(*ast.Ident); isIdent && id.Name != "_" {
reportNoFix(pass, id.Pos(), useBeforeEachTemplate, id.Name)
foundSomething = true
Expand Down Expand Up @@ -268,7 +268,7 @@ func checkAssignments(pass *analysis.Pass, list []ast.Stmt) bool {
func checkAssignmentsValues(pass *analysis.Pass, names []*ast.Ident, values []ast.Expr) bool {
foundSomething := false
for i, val := range values {
if _, isFunc := val.(*ast.FuncLit); !isFunc {
if !is[*ast.FuncLit](val) {
reportNoFix(pass, names[i].Pos(), useBeforeEachTemplate, names[i].Name)
foundSomething = true
}
Expand Down Expand Up @@ -894,7 +894,7 @@ func handleEqualComparison(pass *analysis.Pass, matcher *ast.CallExpr, first ast
t := pass.TypesInfo.TypeOf(first)
if gotypes.IsInterface(t) {
handler.ReplaceFunction(matcher, ast.NewIdent(beIdenticalTo))
} else if _, ok := t.(*gotypes.Pointer); ok {
} else if is[*gotypes.Pointer](t) {
handler.ReplaceFunction(matcher, ast.NewIdent(beIdenticalTo))
} else {
handler.ReplaceFunction(matcher, ast.NewIdent(equal))
Expand Down Expand Up @@ -1120,7 +1120,7 @@ func checkNilError(pass *analysis.Pass, assertionExp *ast.CallExpr, handler gome
}

var newFuncName string
if _, ok := actualArg.(*ast.CallExpr); ok {
if is[*ast.CallExpr](actualArg) {
newFuncName = succeed
} else {
reverseAssertionFuncLogic(assertionExp)
Expand Down Expand Up @@ -1462,7 +1462,7 @@ func handleNilComparisonErr(pass *analysis.Pass, exp *ast.CallExpr, nilable ast.
newFuncName := beNil
isItError := isExprError(pass, nilable)
if isItError {
if _, ok := nilable.(*ast.CallExpr); ok {
if is[*ast.CallExpr](nilable) {
newFuncName = succeed
} else {
reverseAssertionFuncLogic(exp)
Expand Down Expand Up @@ -1577,7 +1577,7 @@ func isComparison(pass *analysis.Pass, actualArg ast.Expr) (ast.Expr, ast.Expr,
case *ast.Ident: // check if const
info, ok := pass.TypesInfo.Types[realFirst]
if ok {
if _, ok := info.Type.(*gotypes.Basic); ok && info.Value != nil {
if is[*gotypes.Basic](info.Type) && info.Value != nil {
replace = true
}
}
Expand Down Expand Up @@ -1631,8 +1631,7 @@ func isExprError(pass *analysis.Pass, expr ast.Expr) bool {

func isPointer(pass *analysis.Pass, expr ast.Expr) bool {
t := pass.TypesInfo.TypeOf(expr)
_, ok := t.(*gotypes.Pointer)
return ok
return is[*gotypes.Pointer](t)
}

func isInterface(pass *analysis.Pass, expr ast.Expr) bool {
Expand Down Expand Up @@ -1667,3 +1666,8 @@ func checkNoAssertion(pass *analysis.Pass, expr *ast.CallExpr, handler gomegahan
reportNoFix(pass, expr.Pos(), missingAssertionMessage, funcName, allowedFunction)
}
}

func is[T any](x any) bool {
_, matchType := x.(T)
return matchType
}

0 comments on commit 5ce34c8

Please sign in to comment.