Skip to content

Commit a99d92e

Browse files
committed
Improve IsGomegaVar() and IsGomegaType()
Don't check non ast.Ident expressions for been Gomega variables. Make more accurate check for the Gomega types (struct or interface). check the alias value instead of the alias.
1 parent 95ce7b3 commit a99d92e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

internal/gomegainfo/gomegainfo.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,35 @@ func IsAssertionFunc(name string) bool {
8686
}
8787

8888
func IsGomegaVar(x ast.Expr, pass *analysis.Pass) bool {
89-
if tx, ok := pass.TypesInfo.Types[x]; ok {
90-
return IsGomegaType(tx.Type)
89+
if _, isIdent := x.(*ast.Ident); !isIdent {
90+
return false
9191
}
9292

93-
return false
93+
tx, ok := pass.TypesInfo.Types[x]
94+
if !ok {
95+
return false
96+
}
97+
98+
return IsGomegaType(tx.Type)
9499
}
95100

101+
const (
102+
gomegaStructType = "github.com/onsi/gomega/internal.Gomega"
103+
gomegaInterface = "github.com/onsi/gomega/types.Gomega"
104+
)
105+
96106
func IsGomegaType(t gotypes.Type) bool {
97-
var typeStr string
98107
switch ttx := t.(type) {
99108
case *gotypes.Pointer:
100-
tp := ttx.Elem()
101-
typeStr = tp.String()
109+
return IsGomegaType(ttx.Elem())
102110

103111
case *gotypes.Named:
104-
typeStr = ttx.String()
112+
name := ttx.String()
113+
return strings.HasSuffix(name, gomegaStructType) || strings.HasSuffix(name, gomegaInterface)
105114

106115
case *gotypes.Alias:
107-
typeStr = ttx.String()
108-
109-
default:
110-
return false
116+
return IsGomegaType(ttx.Rhs())
111117
}
112118

113-
return strings.Contains(typeStr, "github.com/onsi/gomega") &&
114-
(strings.HasSuffix(typeStr, "Gomega") || strings.HasSuffix(typeStr, "WithT"))
119+
return false
115120
}

0 commit comments

Comments
 (0)