Skip to content

Commit

Permalink
fix: use stricter checking type for package from golang and uber (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrywiranto authored Nov 22, 2023
1 parent 777b47c commit 555d4aa
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
)

const (
reportMsg = "calling Finish on gomock.Controller is no longer needed"
mock = "mock"
controller = "gomock.Controller"
pkgLen = 3
finish = "Finish"
reportMsg = "calling Finish on gomock.Controller is no longer needed"
pkgGolangMock = "github.com"
subPkgGolangMock = "golang"
pkgUberMock = "go.uber.org"
mock = "mock"
controller = "gomock.Controller"
golangPkgLen = 4
uberPkgLen = 3
finish = "Finish"
)

var pkgSourcesMap = map[string]bool{
"golang": true,
"go.uber.org": true,
}

// New returns new gomocklinter analyzer.
func New() *analysis.Analyzer {
return &analysis.Analyzer{
Expand Down Expand Up @@ -62,22 +61,37 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

// isValidType checks whether t is a valid package source for gomock controller or not
// currently supports golang/mock/gomock.Controller and go.uber.org/mock/gomock.Controller
//
// value of t can be *examples/vendor/go.uber.org/mock/gomock.Controller
// hence the checking is only the last 3 part.
// currently supports github.com/golang/mock/gomock.Controller and go.uber.org/mock/gomock.Controller.
func isValidType(t string) bool {
if t[0] != '*' {
return false
}
t = t[1:]

strs := strings.Split(t, "/")
return isTypeGomock(strs) || isTypeUberMock(strs)
}

if len(strs) < pkgLen {
// isTypeGomock checks if the given string slice represents gomock type from github.com/golang/mock/gomock.
// It returns true if the last four elements of the slice match the expected values for a gomock type.
// Otherwise, it returns false.
func isTypeGomock(strs []string) bool {
if len(strs) < golangPkgLen {
return false
}
strs = strs[len(strs)-golangPkgLen:]

return strs[0] == pkgGolangMock && strs[1] == subPkgGolangMock && strs[2] == mock && strs[3] == controller
}

// get the last 3 elements
strs = strs[len(strs)-pkgLen:]
// isTypeUberMock checks if the given string slice represents gomock type from go.uber.org/mock/gomock.
// It returns true if the last three elements of the slice match the expected values for an Uber gomock type.
// Otherwise, it returns false.
func isTypeUberMock(strs []string) bool {
if len(strs) < uberPkgLen {
return false
}
strs = strs[len(strs)-uberPkgLen:]

// first element has to be either golang or go.uber.org
// second element has to be mock
// third element has to be gomock.Controller
return pkgSourcesMap[strs[0]] && strs[1] == mock && strs[2] == controller
return strs[0] == pkgUberMock && strs[1] == mock && strs[2] == controller
}

0 comments on commit 555d4aa

Please sign in to comment.