Skip to content

Commit 5ce34c8

Browse files
committed
chore: new util function to check types
1 parent 9724af7 commit 5ce34c8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

linter/ginkgo_linter.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func checkAssignments(pass *analysis.Pass, list []ast.Stmt) bool {
238238

239239
case *ast.AssignStmt:
240240
for i, val := range st.Rhs {
241-
if _, isFunc := val.(*ast.FuncLit); !isFunc {
241+
if !is[*ast.FuncLit](val) {
242242
if id, isIdent := st.Lhs[i].(*ast.Ident); isIdent && id.Name != "_" {
243243
reportNoFix(pass, id.Pos(), useBeforeEachTemplate, id.Name)
244244
foundSomething = true
@@ -268,7 +268,7 @@ func checkAssignments(pass *analysis.Pass, list []ast.Stmt) bool {
268268
func checkAssignmentsValues(pass *analysis.Pass, names []*ast.Ident, values []ast.Expr) bool {
269269
foundSomething := false
270270
for i, val := range values {
271-
if _, isFunc := val.(*ast.FuncLit); !isFunc {
271+
if !is[*ast.FuncLit](val) {
272272
reportNoFix(pass, names[i].Pos(), useBeforeEachTemplate, names[i].Name)
273273
foundSomething = true
274274
}
@@ -894,7 +894,7 @@ func handleEqualComparison(pass *analysis.Pass, matcher *ast.CallExpr, first ast
894894
t := pass.TypesInfo.TypeOf(first)
895895
if gotypes.IsInterface(t) {
896896
handler.ReplaceFunction(matcher, ast.NewIdent(beIdenticalTo))
897-
} else if _, ok := t.(*gotypes.Pointer); ok {
897+
} else if is[*gotypes.Pointer](t) {
898898
handler.ReplaceFunction(matcher, ast.NewIdent(beIdenticalTo))
899899
} else {
900900
handler.ReplaceFunction(matcher, ast.NewIdent(equal))
@@ -1120,7 +1120,7 @@ func checkNilError(pass *analysis.Pass, assertionExp *ast.CallExpr, handler gome
11201120
}
11211121

11221122
var newFuncName string
1123-
if _, ok := actualArg.(*ast.CallExpr); ok {
1123+
if is[*ast.CallExpr](actualArg) {
11241124
newFuncName = succeed
11251125
} else {
11261126
reverseAssertionFuncLogic(assertionExp)
@@ -1462,7 +1462,7 @@ func handleNilComparisonErr(pass *analysis.Pass, exp *ast.CallExpr, nilable ast.
14621462
newFuncName := beNil
14631463
isItError := isExprError(pass, nilable)
14641464
if isItError {
1465-
if _, ok := nilable.(*ast.CallExpr); ok {
1465+
if is[*ast.CallExpr](nilable) {
14661466
newFuncName = succeed
14671467
} else {
14681468
reverseAssertionFuncLogic(exp)
@@ -1577,7 +1577,7 @@ func isComparison(pass *analysis.Pass, actualArg ast.Expr) (ast.Expr, ast.Expr,
15771577
case *ast.Ident: // check if const
15781578
info, ok := pass.TypesInfo.Types[realFirst]
15791579
if ok {
1580-
if _, ok := info.Type.(*gotypes.Basic); ok && info.Value != nil {
1580+
if is[*gotypes.Basic](info.Type) && info.Value != nil {
15811581
replace = true
15821582
}
15831583
}
@@ -1631,8 +1631,7 @@ func isExprError(pass *analysis.Pass, expr ast.Expr) bool {
16311631

16321632
func isPointer(pass *analysis.Pass, expr ast.Expr) bool {
16331633
t := pass.TypesInfo.TypeOf(expr)
1634-
_, ok := t.(*gotypes.Pointer)
1635-
return ok
1634+
return is[*gotypes.Pointer](t)
16361635
}
16371636

16381637
func isInterface(pass *analysis.Pass, expr ast.Expr) bool {
@@ -1667,3 +1666,8 @@ func checkNoAssertion(pass *analysis.Pass, expr *ast.CallExpr, handler gomegahan
16671666
reportNoFix(pass, expr.Pos(), missingAssertionMessage, funcName, allowedFunction)
16681667
}
16691668
}
1669+
1670+
func is[T any](x any) bool {
1671+
_, matchType := x.(T)
1672+
return matchType
1673+
}

0 commit comments

Comments
 (0)