Skip to content

Commit

Permalink
Assumption for array error types (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom authored Oct 4, 2024
1 parent 2ed8830 commit 8b08ccd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
20 changes: 14 additions & 6 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
name := v.Name.Name
if _, ok := v.Type.(*ast.ArrayType); ok {
if !isValidErrorArrayTypeName(name) {
reportAboutErrorType(pass, v.Pos(), name, true)
reportAboutArrayErrorType(pass, v.Pos(), name)
}
} else if !isValidErrorTypeName(name) {
reportAboutErrorType(pass, v.Pos(), name, false)
reportAboutErrorType(pass, v.Pos(), name)
}
return false
}
Expand All @@ -75,20 +75,28 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil //nolint:nilnil
}

func reportAboutErrorType(pass *analysis.Pass, typePos token.Pos, typeName string, isArrayType bool) {
func reportAboutErrorType(pass *analysis.Pass, typePos token.Pos, typeName string) {
var form string
if unicode.IsLower([]rune(typeName)[0]) {
form = "xxxError"
} else {
form = "XxxError"
}

if isArrayType {
form += "s"
}
pass.Reportf(typePos, "the error type name `%s` should conform to the `%s` format", typeName, form)
}

func reportAboutArrayErrorType(pass *analysis.Pass, typePos token.Pos, typeName string) {
var forms string
if unicode.IsLower([]rune(typeName)[0]) {
forms = "`xxxErrors` or `xxxError`"
} else {
forms = "`XxxErrors` or `XxxError`"
}

pass.Reportf(typePos, "the error type name `%s` should conform to the %s format", typeName, forms)
}

func reportAboutSentinelError(pass *analysis.Pass, pos token.Pos, varName string) {
var form string
if unicode.IsLower([]rune(varName)[0]) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/analyzer/facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func isValidErrorArrayTypeName(s string) bool {
words := split(s)
wordsCnt := wordsCount(words)

if wordsCnt["errors"] != 1 {
if wordsCnt["errors"] != 1 && wordsCnt["error"] != 1 {
return false
}
return words[len(words)-1] == "errors"

lastWord := words[len(words)-1]
return lastWord == "errors" || lastWord == "error"
}

func isValidErrorVarName(s string) bool {
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/facts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func Test_isValidErrorArrayTypeName(t *testing.T) {
"validationErrors",
"ERRORS",
"errors",
"IncompatiblePairError",
"multiError",
} {
if !isValidErrorArrayTypeName(tt) {
t.Errorf("%q must be valid error array type name", tt)
Expand All @@ -197,6 +199,10 @@ func Test_isValidErrorArrayTypeName(t *testing.T) {
for _, tt := range []string{
"ErrorsFromValidation",
"errorsFromValidation",
"ErrorMulti",
"ErrorsOfProcessing",
"processingErrs",
"ValidationErr",
} {
if isValidErrorArrayTypeName(tt) {
t.Errorf("%q must be invalid error array type name", tt)
Expand Down
24 changes: 20 additions & 4 deletions pkg/analyzer/testdata/src/regular/error_types_exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ type tenErrors [10]string

func (te tenErrors) Error() string { return strings.Join(te[:], "\n") }

type MultiError []error // want "the error type name `MultiError` should conform to the `XxxErrors` format"
type MultiErr []error // want "the error type name `MultiErr` should conform to the `XxxErrors` or `XxxError` format"
func (me MultiErr) Error() string { return "" }

type multiErr []error // want "the error type name `multiErr` should conform to the `xxxErrors` or `xxxError` format"
func (me multiErr) Error() string { return "" }

type Twoerr [2]error // want "the error type name `Twoerr` should conform to the `XxxErrors` or `XxxError` format"
func (te Twoerr) Error() string { return te[0].Error() + "\n" + te[1].Error() }

type twoErrorss [2]error // want "the error type name `twoErrorss` should conform to the `xxxErrors` or `xxxError` format"
func (te twoErrorss) Error() string { return te[0].Error() + "\n" + te[1].Error() }

type MultiError []error

func (me MultiError) Error() string { return "" }

type multiError []error // want "the error type name `multiError` should conform to the `xxxErrors` format"
type multiError []error

func (me multiError) Error() string { return "" }

type TwoError [2]error // want "the error type name `TwoError` should conform to the `XxxErrors` format"
type TwoError [2]error

func (te TwoError) Error() string { return te[0].Error() + "\n" + te[1].Error() }

type twoError [2]error // want "the error type name `twoError` should conform to the `xxxErrors` format"
type twoError [2]error

func (te twoError) Error() string { return te[0].Error() + "\n" + te[1].Error() }

0 comments on commit 8b08ccd

Please sign in to comment.