From 96291f11ac4659094b1e3d0d197cb9348fd34f85 Mon Sep 17 00:00:00 2001 From: Nahshon Unna-Tsameret Date: Tue, 29 Oct 2024 06:10:25 +0200 Subject: [PATCH] encapsulate the GomegaExpression fields --- internal/expression/actual/actual.go | 2 +- internal/expression/actual/actualarg.go | 2 +- internal/expression/expression.go | 102 +++++++++++++++++----- internal/rules/asyncfunccallrule.go | 4 +- internal/rules/asynctimeintervalsrule.go | 4 +- internal/rules/caprule.go | 24 ++--- internal/rules/comparepointerrule.go | 4 +- internal/rules/comparisonrule.go | 6 +- internal/rules/doublenegativerule.go | 2 +- internal/rules/equalboolrule.go | 4 +- internal/rules/equaldifferenttypesrule.go | 4 +- internal/rules/equalnilrule.go | 2 +- internal/rules/errorequalnilrule.go | 6 +- internal/rules/havelen0.go | 2 +- internal/rules/lenrule.go | 32 ++++--- internal/rules/matcherrorrule.go | 12 +-- internal/rules/nilcomparerule.go | 8 +- linter/ginkgo_linter.go | 9 +- 18 files changed, 140 insertions(+), 89 deletions(-) diff --git a/internal/expression/actual/actual.go b/internal/expression/actual/actual.go index 8eebedc..8575b41 100644 --- a/internal/expression/actual/actual.go +++ b/internal/expression/actual/actual.go @@ -1,13 +1,13 @@ package actual import ( - "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" "go/ast" gotypes "go/types" "golang.org/x/tools/go/analysis" "github.com/nunnatsa/ginkgolinter/internal/gomegahandler" + "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" ) type Actual struct { diff --git a/internal/expression/actual/actualarg.go b/internal/expression/actual/actualarg.go index d1054bb..6b27719 100644 --- a/internal/expression/actual/actualarg.go +++ b/internal/expression/actual/actualarg.go @@ -1,7 +1,6 @@ package actual import ( - "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" "go/ast" "go/token" gotypes "go/types" @@ -10,6 +9,7 @@ import ( "golang.org/x/tools/go/analysis" "github.com/nunnatsa/ginkgolinter/internal/expression/value" + "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" "github.com/nunnatsa/ginkgolinter/internal/interfaces" "github.com/nunnatsa/ginkgolinter/internal/reverseassertion" ) diff --git a/internal/expression/expression.go b/internal/expression/expression.go index 6ae0d42..5b70dfc 100644 --- a/internal/expression/expression.go +++ b/internal/expression/expression.go @@ -2,9 +2,10 @@ package expression import ( "fmt" - "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" + "github.com/nunnatsa/ginkgolinter/internal/formatter" "go/ast" "go/token" + gotypes "go/types" "github.com/go-toolsmith/astcopy" "golang.org/x/tools/go/analysis" @@ -13,12 +14,13 @@ import ( "github.com/nunnatsa/ginkgolinter/internal/expression/matcher" "github.com/nunnatsa/ginkgolinter/internal/expression/value" "github.com/nunnatsa/ginkgolinter/internal/gomegahandler" + "github.com/nunnatsa/ginkgolinter/internal/gomegainfo" "github.com/nunnatsa/ginkgolinter/internal/reverseassertion" ) type GomegaExpression struct { - Orig *ast.CallExpr - Clone *ast.CallExpr + orig *ast.CallExpr + clone *ast.CallExpr assertionFuncName string origAssertionFuncName string @@ -26,8 +28,8 @@ type GomegaExpression struct { isAsync bool - Actual *actual.Actual - Matcher *matcher.Matcher + actual *actual.Actual + matcher *matcher.Matcher handler gomegahandler.Handler } @@ -41,7 +43,7 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand origSel, ok := origExpr.Fun.(*ast.SelectorExpr) if !ok || !gomegainfo.IsAssertionFunc(origSel.Sel.Name) { return &GomegaExpression{ - Orig: origExpr, + orig: origExpr, actualFuncName: actualMethodName, }, true } @@ -79,8 +81,8 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand exprClone.Args[0] = mtchr.Clone gexp := &GomegaExpression{ - Orig: origExpr, - Clone: exprClone, + orig: origExpr, + clone: exprClone, assertionFuncName: origSel.Sel.Name, origAssertionFuncName: origSel.Sel.Name, @@ -88,8 +90,8 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand isAsync: actl.IsAsync(), - Actual: actl, - Matcher: mtchr, + actual: actl, + matcher: mtchr, handler: handler, } @@ -102,7 +104,7 @@ func New(origExpr *ast.CallExpr, pass *analysis.Pass, handler gomegahandler.Hand } func (e *GomegaExpression) IsMissingAssertion() bool { - return e.Matcher == nil + return e.matcher == nil } func (e *GomegaExpression) GetActualFuncName() string { @@ -131,38 +133,38 @@ func (e *GomegaExpression) IsAsync() bool { } func (e *GomegaExpression) ReverseAssertionFuncLogic() { - assertionFunc := e.Clone.Fun.(*ast.SelectorExpr).Sel + assertionFunc := e.clone.Fun.(*ast.SelectorExpr).Sel newName := reverseassertion.ChangeAssertionLogic(assertionFunc.Name) assertionFunc.Name = newName e.assertionFuncName = newName } func (e *GomegaExpression) ReplaceAssertionMethod(name string) { - e.Clone.Fun.(*ast.SelectorExpr).Sel.Name = name + e.clone.Fun.(*ast.SelectorExpr).Sel.Name = name } func (e *GomegaExpression) ReplaceMatcherFuncName(name string) { - e.Matcher.ReplaceMatcherFuncName(name) + e.matcher.ReplaceMatcherFuncName(name) } func (e *GomegaExpression) ReplaceMatcherArgs(newArgs []ast.Expr) { - e.Matcher.ReplaceMatcherArgs(newArgs) + e.matcher.ReplaceMatcherArgs(newArgs) } func (e *GomegaExpression) RemoveMatcherArgs() { - e.Matcher.ReplaceMatcherArgs(nil) + e.matcher.ReplaceMatcherArgs(nil) } func (e *GomegaExpression) ReplaceActual(newArg ast.Expr) { - e.Actual.ReplaceActual(newArg) + e.actual.ReplaceActual(newArg) } func (e *GomegaExpression) ReplaceActualWithItsFirstArg() { - e.Actual.ReplaceActualWithItsFirstArg() + e.actual.ReplaceActualWithItsFirstArg() } func (e *GomegaExpression) replaceMathcerFuncNoArgs(name string) { - e.Matcher.ReplaceMatcherFuncName(name) + e.matcher.ReplaceMatcherFuncName(name) e.RemoveMatcherArgs() } @@ -175,7 +177,7 @@ func (e *GomegaExpression) SetMatcherBeEmpty() { } func (e *GomegaExpression) SetLenNumericMatcher() { - if m, ok := e.Matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() { + if m, ok := e.matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() { e.SetMatcherBeEmpty() } else { e.ReplaceMatcherFuncName("HaveLen") @@ -184,7 +186,7 @@ func (e *GomegaExpression) SetLenNumericMatcher() { } func (e *GomegaExpression) SetLenNumericActual() { - if m, ok := e.Matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() { + if m, ok := e.matcher.GetMatcherInfo().(value.Valuer); ok && m.IsValueZero() { e.SetMatcherBeEmpty() } else { e.ReplaceMatcherFuncName("HaveLen") @@ -228,9 +230,9 @@ func (e *GomegaExpression) SetMatcherBeFalse() { } func (e *GomegaExpression) SetMatcherHaveValue() { - newMatcherExp := e.handler.GetNewWrapperMatcher("HaveValue", e.Matcher.Clone) - e.Clone.Args[0] = newMatcherExp - e.Matcher.Clone = newMatcherExp + newMatcherExp := e.handler.GetNewWrapperMatcher("HaveValue", e.matcher.Clone) + e.clone.Args[0] = newMatcherExp + e.matcher.Clone = newMatcherExp } func (e *GomegaExpression) SetMatcherEqual(arg ast.Expr) { @@ -254,3 +256,55 @@ func (e *GomegaExpression) SetMatcherBeNumerically(op token.Token, arg ast.Expr) func (e *GomegaExpression) IsNegativeAssertion() bool { return reverseassertion.IsNegativeLogic(e.assertionFuncName) } + +func (e *GomegaExpression) GetClone() *ast.CallExpr { + return e.clone +} + +// Actual proxies: + +func (e *GomegaExpression) GetActualClone() *ast.CallExpr { + return e.actual.Clone +} + +func (e *GomegaExpression) AppendWithArgsToActual() { + e.actual.AppendWithArgsMethod() +} + +func (e *GomegaExpression) GetAsyncActualArg() *actual.AsyncArg { + return e.actual.GetAsyncArg() +} + +func (e *GomegaExpression) GetActualArg() actual.ArgPayload { + return e.actual.Arg +} + +func (e *GomegaExpression) GetActualArgExpr() ast.Expr { + return e.actual.GetActualArg() +} + +func (e *GomegaExpression) GetActualArgGOType() gotypes.Type { + return e.actual.ArgGOType() +} + +func (e *GomegaExpression) ActualArgTypeIs(other actual.ArgType) bool { + return e.actual.Arg.ArgType().Is(other) +} + +// Matcher proxies + +func (e *GomegaExpression) GetMatcher() *matcher.Matcher { + return e.matcher +} + +func (e *GomegaExpression) GetMatcherInfo() matcher.Info { + return e.matcher.GetMatcherInfo() +} + +func (e *GomegaExpression) MatcherTypeIs(other matcher.Type) bool { + return e.matcher.GetMatcherInfo().Type().Is(other) +} + +func (e *GomegaExpression) FormatOrig(frm *formatter.GoFmtFormatter) string { + return frm.Format(e.orig) +} diff --git a/internal/rules/asyncfunccallrule.go b/internal/rules/asyncfunccallrule.go index 00d90fe..e4eda7f 100644 --- a/internal/rules/asyncfunccallrule.go +++ b/internal/rules/asyncfunccallrule.go @@ -23,7 +23,7 @@ func (r AsyncFuncCallRule) isApplied(gexp *expression.GomegaExpression, config t return false } - if asyncArg := gexp.Actual.GetAsyncArg(); asyncRules != nil { + if asyncArg := gexp.GetAsyncActualArg(); asyncRules != nil { return !asyncArg.IsValid() } @@ -33,7 +33,7 @@ func (r AsyncFuncCallRule) isApplied(gexp *expression.GomegaExpression, config t func (r AsyncFuncCallRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { if r.isApplied(gexp, config) { - gexp.Actual.AppendWithArgsMethod() + gexp.AppendWithArgsToActual() reportBuilder.AddIssue(true, valueInEventually, gexp.GetActualFuncName()) } diff --git a/internal/rules/asynctimeintervalsrule.go b/internal/rules/asynctimeintervalsrule.go index 95d5291..45953ec 100644 --- a/internal/rules/asynctimeintervalsrule.go +++ b/internal/rules/asynctimeintervalsrule.go @@ -25,7 +25,7 @@ func (r AsyncTimeIntervalsRule) isApplied(gexp *expression.GomegaExpression, con func (r AsyncTimeIntervalsRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { if r.isApplied(gexp, config) { - asyncArg := gexp.Actual.GetAsyncArg() + asyncArg := gexp.GetAsyncActualArg() if asyncArg.TooManyTimeouts() { reportBuilder.AddIssue(false, multipleTimeouts) } @@ -51,7 +51,7 @@ func checkInterval(gexp *expression.GomegaExpression, durVal intervals.DurationV case *intervals.RealDurationValue, *intervals.UnknownDurationTypeValue: case *intervals.NumericDurationValue: - if checkNumericInterval(gexp.Actual.Clone, to) { + if checkNumericInterval(gexp.GetActualClone(), to) { reportBuilder.AddIssue(true, onlyUseTimeDurationForInterval) } diff --git a/internal/rules/caprule.go b/internal/rules/caprule.go index fcdc0ea..e3ad45d 100644 --- a/internal/rules/caprule.go +++ b/internal/rules/caprule.go @@ -34,19 +34,19 @@ func (r *CapRule) isApplied(gexp *expression.GomegaExpression, config types.Conf return false } - matcherType := gexp.Matcher.GetMatcherInfo().Type() - if gexp.Actual.Arg.ArgType().Is(actual.CapFuncActualArgType) { - if matcherType.Is(matcher.EqualMatcherType | matcher.BeZeroMatcherType) { + //matcherType := gexp.matcher.GetMatcherInfo().Type() + if gexp.ActualArgTypeIs(actual.CapFuncActualArgType) { + if gexp.MatcherTypeIs(matcher.EqualMatcherType | matcher.BeZeroMatcherType) { return true } - if matcherType.Is(matcher.BeNumericallyMatcherType) { - mtchr := gexp.Matcher.GetMatcherInfo().(*matcher.BeNumericallyMatcher) - return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || matcherType.Is(matcher.EqualZero|matcher.GreaterThanZero) + if gexp.MatcherTypeIs(matcher.BeNumericallyMatcherType) { + mtchr := gexp.GetMatcherInfo().(*matcher.BeNumericallyMatcher) + return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || gexp.MatcherTypeIs(matcher.EqualZero|matcher.GreaterThanZero) } } - if gexp.Actual.Arg.ArgType().Is(actual.CapComparisonActualArgType) && matcherType.Is(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) { + if gexp.ActualArgTypeIs(actual.CapComparisonActualArgType) && gexp.MatcherTypeIs(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) { return true } @@ -54,11 +54,11 @@ func (r *CapRule) isApplied(gexp *expression.GomegaExpression, config types.Conf } func (r *CapRule) fixExpression(gexp *expression.GomegaExpression) bool { - if gexp.Actual.Arg.ArgType().Is(actual.CapFuncActualArgType) { + if gexp.ActualArgTypeIs(actual.CapFuncActualArgType) { return r.fixEqual(gexp) } - if gexp.Actual.Arg.ArgType().Is(actual.CapComparisonActualArgType) { + if gexp.ActualArgTypeIs(actual.CapComparisonActualArgType) { return r.fixComparison(gexp) } @@ -66,7 +66,7 @@ func (r *CapRule) fixExpression(gexp *expression.GomegaExpression) bool { } func (r *CapRule) fixEqual(gexp *expression.GomegaExpression) bool { - matcherInfo := gexp.Matcher.GetMatcherInfo() + matcherInfo := gexp.GetMatcherInfo() switch mtchr := matcherInfo.(type) { case *matcher.EqualMatcher: gexp.SetMatcherCap(mtchr.GetValueExpr()) @@ -89,7 +89,7 @@ func (r *CapRule) fixEqual(gexp *expression.GomegaExpression) bool { } func (r *CapRule) fixComparison(gexp *expression.GomegaExpression) bool { - actl := gexp.Actual.Arg.(*actual.FuncComparisonPayload) + actl := gexp.GetActualArg().(*actual.FuncComparisonPayload) if op := actl.GetOp(); op == token.NEQ { gexp.ReverseAssertionFuncLogic() } else if op != token.EQL { @@ -99,7 +99,7 @@ func (r *CapRule) fixComparison(gexp *expression.GomegaExpression) bool { gexp.SetMatcherCap(actl.GetValueExpr()) gexp.ReplaceActual(actl.GetFuncArg()) - if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) { + if gexp.MatcherTypeIs(matcher.BoolValueFalse) { gexp.ReverseAssertionFuncLogic() } diff --git a/internal/rules/comparepointerrule.go b/internal/rules/comparepointerrule.go index 7deb601..dcbea1b 100644 --- a/internal/rules/comparepointerrule.go +++ b/internal/rules/comparepointerrule.go @@ -13,7 +13,7 @@ const comparePointerToValue = "comparing a pointer to a value will always fail" type ComparePointRule struct{} func (r ComparePointRule) isApplied(gexp *expression.GomegaExpression) bool { - actl, ok := gexp.Actual.Arg.(*actual.RegularArgPayload) + actl, ok := gexp.GetActualArg().(*actual.RegularArgPayload) if !ok { return false } @@ -26,7 +26,7 @@ func (r ComparePointRule) Apply(gexp *expression.GomegaExpression, config types. return false } - switch mtchr := gexp.Matcher.GetMatcherInfo().(type) { + switch mtchr := gexp.GetMatcherInfo().(type) { case *matcher.EqualMatcher: if mtchr.IsPointer() || mtchr.IsInterface() { return false diff --git a/internal/rules/comparisonrule.go b/internal/rules/comparisonrule.go index 0023df7..fb38529 100644 --- a/internal/rules/comparisonrule.go +++ b/internal/rules/comparisonrule.go @@ -19,7 +19,7 @@ func (r ComparisonRule) isApplied(gexp *expression.GomegaExpression, config type return false } - return gexp.Actual.Arg.ArgType().Is(actual.ComparisonActualArgType) + return gexp.ActualArgTypeIs(actual.ComparisonActualArgType) } func (r ComparisonRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { @@ -27,7 +27,7 @@ func (r ComparisonRule) Apply(gexp *expression.GomegaExpression, config types.Co return false } - actl, ok := gexp.Actual.Arg.(actual.ComparisonActualPayload) + actl, ok := gexp.GetActualArg().(actual.ComparisonActualPayload) if !ok { return false } @@ -50,7 +50,7 @@ func (r ComparisonRule) Apply(gexp *expression.GomegaExpression, config types.Co return false } - if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) { + if gexp.MatcherTypeIs(matcher.BoolValueFalse) { gexp.ReverseAssertionFuncLogic() } diff --git a/internal/rules/doublenegativerule.go b/internal/rules/doublenegativerule.go index b9b15c8..6ce7be5 100644 --- a/internal/rules/doublenegativerule.go +++ b/internal/rules/doublenegativerule.go @@ -12,7 +12,7 @@ const doubleNegativeWarningTemplate = "avoid double negative assertion" type DoubleNegativeRule struct{} func (DoubleNegativeRule) isApplied(gexp *expression.GomegaExpression) bool { - return gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BeFalseMatcherType) && + return gexp.MatcherTypeIs(matcher.BeFalseMatcherType) && gexp.IsNegativeAssertion() } diff --git a/internal/rules/equalboolrule.go b/internal/rules/equalboolrule.go index 711ecb3..e9eaa1b 100644 --- a/internal/rules/equalboolrule.go +++ b/internal/rules/equalboolrule.go @@ -12,7 +12,7 @@ const wrongBoolWarningTemplate = "wrong boolean assertion" type EqualBoolRule struct{} func (r EqualBoolRule) isApplied(gexp *expression.GomegaExpression) bool { - return gexp.Matcher.GetMatcherInfo().Type().Is(matcher.EqualBoolValueMatcherType) + return gexp.MatcherTypeIs(matcher.EqualBoolValueMatcherType) } func (r EqualBoolRule) Apply(gexp *expression.GomegaExpression, _ types.Config, reportBuilder *reports.Builder) bool { @@ -20,7 +20,7 @@ func (r EqualBoolRule) Apply(gexp *expression.GomegaExpression, _ types.Config, return false } - if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueTrue) { + if gexp.MatcherTypeIs(matcher.BoolValueTrue) { gexp.SetMatcherBeTrue() } else { if gexp.IsNegativeAssertion() { diff --git a/internal/rules/equaldifferenttypesrule.go b/internal/rules/equaldifferenttypesrule.go index 4faea5d..bb99795 100644 --- a/internal/rules/equaldifferenttypesrule.go +++ b/internal/rules/equaldifferenttypesrule.go @@ -21,11 +21,11 @@ func (r EqualDifferentTypesRule) Apply(gexp *expression.GomegaExpression, config return false } - return r.checkEqualDifferentTypes(gexp, gexp.Matcher, false, reportBuilder) + return r.checkEqualDifferentTypes(gexp, gexp.GetMatcher(), false, reportBuilder) } func (r EqualDifferentTypesRule) checkEqualDifferentTypes(gexp *expression.GomegaExpression, mtchr *matcher.Matcher, parentPointer bool, reportBuilder *reports.Builder) bool { - actualType := gexp.Actual.ArgGOType() + actualType := gexp.GetActualArgGOType() if parentPointer { if t, ok := actualType.(*gotypes.Pointer); ok { diff --git a/internal/rules/equalnilrule.go b/internal/rules/equalnilrule.go index 97396a8..5b28e7d 100644 --- a/internal/rules/equalnilrule.go +++ b/internal/rules/equalnilrule.go @@ -13,7 +13,7 @@ type EqualNilRule struct{} func (r EqualNilRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool { return !bool(config.SuppressNil) && - gexp.Matcher.GetMatcherInfo().Type().Is(matcher.EqualValueMatcherType) + gexp.MatcherTypeIs(matcher.EqualValueMatcherType) } func (r EqualNilRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { diff --git a/internal/rules/errorequalnilrule.go b/internal/rules/errorequalnilrule.go index 7b31dde..7aaf763 100644 --- a/internal/rules/errorequalnilrule.go +++ b/internal/rules/errorequalnilrule.go @@ -13,8 +13,8 @@ type ErrorEqualNilRule struct{} func (ErrorEqualNilRule) isApplied(gexp *expression.GomegaExpression, config types.Config) bool { return !bool(config.SuppressErr) && - gexp.Actual.Arg.ArgType().Is(actual.ErrorTypeArgType) && - gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BeNilMatcherType|matcher.EqualNilMatcherType) + gexp.ActualArgTypeIs(actual.ErrorTypeArgType) && + gexp.MatcherTypeIs(matcher.BeNilMatcherType|matcher.EqualNilMatcherType) } func (r ErrorEqualNilRule) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { @@ -22,7 +22,7 @@ func (r ErrorEqualNilRule) Apply(gexp *expression.GomegaExpression, config types return false } - if v, ok := gexp.Actual.Arg.(value.Valuer); ok && v.IsFunc() || gexp.Actual.Arg.ArgType().Is(actual.ErrFuncActualArgType) { + if v, ok := gexp.GetActualArg().(value.Valuer); ok && v.IsFunc() || gexp.ActualArgTypeIs(actual.ErrFuncActualArgType) { gexp.SetMatcherSucceed() } else { gexp.ReverseAssertionFuncLogic() diff --git a/internal/rules/havelen0.go b/internal/rules/havelen0.go index 4b74007..20bcb72 100644 --- a/internal/rules/havelen0.go +++ b/internal/rules/havelen0.go @@ -10,7 +10,7 @@ import ( type HaveLen0 struct{} func (r *HaveLen0) isApplied(gexp *expression.GomegaExpression, config types.Config) bool { - return gexp.Matcher.GetMatcherInfo().Type().Is(matcher.HaveLenZeroMatcherType) && !bool(config.AllowHaveLen0) + return gexp.MatcherTypeIs(matcher.HaveLenZeroMatcherType) && !bool(config.AllowHaveLen0) } func (r *HaveLen0) Apply(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder) bool { diff --git a/internal/rules/lenrule.go b/internal/rules/lenrule.go index b9ed693..06d6f2c 100644 --- a/internal/rules/lenrule.go +++ b/internal/rules/lenrule.go @@ -34,19 +34,18 @@ func (r *LenRule) isApplied(gexp *expression.GomegaExpression, config types.Conf return false } - matcherType := gexp.Matcher.GetMatcherInfo().Type() - if gexp.Actual.Arg.ArgType().Is(actual.LenFuncActualArgType) { - if matcherType.Is(matcher.EqualMatcherType | matcher.BeZeroMatcherType) { + if gexp.ActualArgTypeIs(actual.LenFuncActualArgType) { + if gexp.MatcherTypeIs(matcher.EqualMatcherType | matcher.BeZeroMatcherType) { return true } - if matcherType.Is(matcher.BeNumericallyMatcherType) { - mtchr := gexp.Matcher.GetMatcherInfo().(*matcher.BeNumericallyMatcher) - return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || matcherType.Is(matcher.EqualZero|matcher.GreaterThanZero) + if gexp.MatcherTypeIs(matcher.BeNumericallyMatcherType) { + mtchr := gexp.GetMatcherInfo().(*matcher.BeNumericallyMatcher) + return mtchr.GetOp() == token.EQL || mtchr.GetOp() == token.NEQ || gexp.MatcherTypeIs(matcher.EqualZero|matcher.GreaterThanZero) } } - if gexp.Actual.Arg.ArgType().Is(actual.LenComparisonActualArgType) && matcherType.Is(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) { + if gexp.ActualArgTypeIs(actual.LenComparisonActualArgType) && gexp.MatcherTypeIs(matcher.BeTrueMatcherType|matcher.BeFalseMatcherType|matcher.EqualBoolValueMatcherType) { return true } @@ -54,11 +53,11 @@ func (r *LenRule) isApplied(gexp *expression.GomegaExpression, config types.Conf } func (r *LenRule) fixExpression(gexp *expression.GomegaExpression) bool { - if gexp.Actual.Arg.ArgType().Is(actual.LenFuncActualArgType) { + if gexp.ActualArgTypeIs(actual.LenFuncActualArgType) { return r.fixEqual(gexp) } - if gexp.Actual.Arg.ArgType().Is(actual.LenComparisonActualArgType) { + if gexp.ActualArgTypeIs(actual.LenComparisonActualArgType) { return r.fixComparison(gexp) } @@ -67,15 +66,14 @@ func (r *LenRule) fixExpression(gexp *expression.GomegaExpression) bool { func (r *LenRule) fixEqual(gexp *expression.GomegaExpression) bool { - matcherType := gexp.Matcher.GetMatcherInfo().Type() - if matcherType.Is(matcher.EqualMatcherType) { + if gexp.MatcherTypeIs(matcher.EqualMatcherType) { gexp.SetLenNumericMatcher() - } else if matcherType.Is(matcher.BeZeroMatcherType) { + } else if gexp.MatcherTypeIs(matcher.BeZeroMatcherType) { gexp.SetMatcherBeEmpty() - } else if matcherType.Is(matcher.BeNumericallyMatcherType) { - mtchr := gexp.Matcher.GetMatcherInfo().(*matcher.BeNumericallyMatcher) + } else if gexp.MatcherTypeIs(matcher.BeNumericallyMatcherType) { + mtchr := gexp.GetMatcherInfo().(*matcher.BeNumericallyMatcher) op := mtchr.GetOp() if op == token.EQL { @@ -83,7 +81,7 @@ func (r *LenRule) fixEqual(gexp *expression.GomegaExpression) bool { } else if op == token.NEQ { gexp.ReverseAssertionFuncLogic() gexp.SetLenNumericMatcher() - } else if matcherType.Is(matcher.GreaterThanZero) { + } else if gexp.MatcherTypeIs(matcher.GreaterThanZero) { gexp.ReverseAssertionFuncLogic() gexp.SetMatcherBeEmpty() } else { @@ -98,14 +96,14 @@ func (r *LenRule) fixEqual(gexp *expression.GomegaExpression) bool { } func (r *LenRule) fixComparison(gexp *expression.GomegaExpression) bool { - actl := gexp.Actual.Arg.(*actual.FuncComparisonPayload) + actl := gexp.GetActualArg().(*actual.FuncComparisonPayload) if op := actl.GetOp(); op == token.NEQ { gexp.ReverseAssertionFuncLogic() } else if op != token.EQL { return false } - if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) { + if gexp.MatcherTypeIs(matcher.BoolValueFalse) { gexp.ReverseAssertionFuncLogic() } diff --git a/internal/rules/matcherrorrule.go b/internal/rules/matcherrorrule.go index 904ca17..767b4b6 100644 --- a/internal/rules/matcherrorrule.go +++ b/internal/rules/matcherrorrule.go @@ -30,7 +30,7 @@ const ( type MatchErrorRule struct{} func (r MatchErrorRule) isApplied(gexp *expression.GomegaExpression) bool { - return gexp.Matcher.GetMatcherInfo().Type().Is(matcher.MatchErrorMatcherType | matcher.MultipleMatcherMatherType) + return gexp.MatcherTypeIs(matcher.MatchErrorMatcherType | matcher.MultipleMatcherMatherType) } func (r MatchErrorRule) Apply(gexp *expression.GomegaExpression, _ types.Config, reportBuilder *reports.Builder) bool { @@ -42,17 +42,17 @@ func (r MatchErrorRule) Apply(gexp *expression.GomegaExpression, _ types.Config, } func checkMatchError(gexp *expression.GomegaExpression, reportBuilder *reports.Builder) bool { - mtchr := gexp.Matcher.GetMatcherInfo() + mtchr := gexp.GetMatcherInfo() switch m := mtchr.(type) { case matcher.MatchErrorMatcher: - return checkMatchErrorMatcher(gexp, gexp.Matcher, m, reportBuilder) + return checkMatchErrorMatcher(gexp, gexp.GetMatcher(), m, reportBuilder) case *matcher.MultipleMatchersMatcher: res := false for i := range m.Len() { nested := m.At(i) if specific, ok := nested.GetMatcherInfo().(matcher.MatchErrorMatcher); ok { - if valid := checkMatchErrorMatcher(gexp, gexp.Matcher, specific, reportBuilder); valid { + if valid := checkMatchErrorMatcher(gexp, gexp.GetMatcher(), specific, reportBuilder); valid { res = true } } @@ -64,8 +64,8 @@ func checkMatchError(gexp *expression.GomegaExpression, reportBuilder *reports.B } func checkMatchErrorMatcher(gexp *expression.GomegaExpression, mtchr *matcher.Matcher, mtchrInfo matcher.MatchErrorMatcher, reportBuilder *reports.Builder) bool { - if !gexp.Actual.Arg.ArgType().Is(actual.ErrorTypeArgType) { - reportBuilder.AddIssue(false, matchErrorArgWrongType, reportBuilder.FormatExpr(gexp.Actual.GetActualArg())) + if !gexp.ActualArgTypeIs(actual.ErrorTypeArgType) { + reportBuilder.AddIssue(false, matchErrorArgWrongType, reportBuilder.FormatExpr(gexp.GetActualArgExpr())) } switch m := mtchrInfo.(type) { diff --git a/internal/rules/nilcomparerule.go b/internal/rules/nilcomparerule.go index 8f3fa19..d04e6e3 100644 --- a/internal/rules/nilcomparerule.go +++ b/internal/rules/nilcomparerule.go @@ -22,21 +22,21 @@ func (r NilCompareRule) Apply(gexp *expression.GomegaExpression, config types.Co return false } - if gexp.Matcher.GetMatcherInfo().Type().Is(matcher.BoolValueFalse) { + if gexp.MatcherTypeIs(matcher.BoolValueFalse) { gexp.ReverseAssertionFuncLogic() } - r.handleNilBeBoolMatcher(gexp, gexp.Actual.Arg.(*actual.NilComparisonPayload), reportBuilder, isErr) + r.handleNilBeBoolMatcher(gexp, gexp.GetActualArg().(*actual.NilComparisonPayload), reportBuilder, isErr) return true } func (r NilCompareRule) isApplied(gexp *expression.GomegaExpression, config types.Config) (bool, bool) { - if !gexp.Matcher.GetMatcherInfo().Type().Is(matcher.EqualBoolValueMatcherType | matcher.BeTrueMatcherType | matcher.BeFalseMatcherType) { + if !gexp.MatcherTypeIs(matcher.EqualBoolValueMatcherType | matcher.BeTrueMatcherType | matcher.BeFalseMatcherType) { return false, false } - actl, ok := gexp.Actual.Arg.(*actual.NilComparisonPayload) + actl, ok := gexp.GetActualArg().(*actual.NilComparisonPayload) if !ok { return false, false } diff --git a/linter/ginkgo_linter.go b/linter/ginkgo_linter.go index 98c6a8a..3ece993 100644 --- a/linter/ginkgo_linter.go +++ b/linter/ginkgo_linter.go @@ -99,7 +99,8 @@ func (l *GinkgoLinter) Run(pass *analysis.Pass) (any, error) { return true } - return checkGomegaExpression(pass, config, gexp) + reportBuilder := reports.NewBuilder(assertionExp, formatter.NewGoFmtFormatter(pass.Fset)) + return checkGomegaExpression(gexp, config, reportBuilder, pass) }) } return nil, nil @@ -220,9 +221,7 @@ func checkFocusContainer(pass *analysis.Pass, ginkgoHndlr ginkgohandler.Handler, return foundFocus } -func checkGomegaExpression(pass *analysis.Pass, config types.Config, gexp *expression.GomegaExpression) bool { - reportBuilder := reports.NewBuilder(gexp.Orig, formatter.NewGoFmtFormatter(pass.Fset)) - +func checkGomegaExpression(gexp *expression.GomegaExpression, config types.Config, reportBuilder *reports.Builder, pass *analysis.Pass) bool { goNested := false if rules.GetMissingAssertionRule().Apply(gexp, config, reportBuilder) { goNested = true @@ -236,7 +235,7 @@ func checkGomegaExpression(pass *analysis.Pass, config types.Config, gexp *expre } if reportBuilder.HasReport() { - reportBuilder.SetFixOffer(gexp.Clone) + reportBuilder.SetFixOffer(gexp.GetClone()) pass.Report(reportBuilder.Build()) }