Skip to content

Commit 8b08ccd

Browse files
authored
Assumption for array error types (#47)
1 parent 2ed8830 commit 8b08ccd

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

pkg/analyzer/analyzer.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
6161
name := v.Name.Name
6262
if _, ok := v.Type.(*ast.ArrayType); ok {
6363
if !isValidErrorArrayTypeName(name) {
64-
reportAboutErrorType(pass, v.Pos(), name, true)
64+
reportAboutArrayErrorType(pass, v.Pos(), name)
6565
}
6666
} else if !isValidErrorTypeName(name) {
67-
reportAboutErrorType(pass, v.Pos(), name, false)
67+
reportAboutErrorType(pass, v.Pos(), name)
6868
}
6969
return false
7070
}
@@ -75,20 +75,28 @@ func run(pass *analysis.Pass) (interface{}, error) {
7575
return nil, nil //nolint:nilnil
7676
}
7777

78-
func reportAboutErrorType(pass *analysis.Pass, typePos token.Pos, typeName string, isArrayType bool) {
78+
func reportAboutErrorType(pass *analysis.Pass, typePos token.Pos, typeName string) {
7979
var form string
8080
if unicode.IsLower([]rune(typeName)[0]) {
8181
form = "xxxError"
8282
} else {
8383
form = "XxxError"
8484
}
8585

86-
if isArrayType {
87-
form += "s"
88-
}
8986
pass.Reportf(typePos, "the error type name `%s` should conform to the `%s` format", typeName, form)
9087
}
9188

89+
func reportAboutArrayErrorType(pass *analysis.Pass, typePos token.Pos, typeName string) {
90+
var forms string
91+
if unicode.IsLower([]rune(typeName)[0]) {
92+
forms = "`xxxErrors` or `xxxError`"
93+
} else {
94+
forms = "`XxxErrors` or `XxxError`"
95+
}
96+
97+
pass.Reportf(typePos, "the error type name `%s` should conform to the %s format", typeName, forms)
98+
}
99+
92100
func reportAboutSentinelError(pass *analysis.Pass, pos token.Pos, varName string) {
93101
var form string
94102
if unicode.IsLower([]rune(varName)[0]) {

pkg/analyzer/facts.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ func isValidErrorArrayTypeName(s string) bool {
4141
words := split(s)
4242
wordsCnt := wordsCount(words)
4343

44-
if wordsCnt["errors"] != 1 {
44+
if wordsCnt["errors"] != 1 && wordsCnt["error"] != 1 {
4545
return false
4646
}
47-
return words[len(words)-1] == "errors"
47+
48+
lastWord := words[len(words)-1]
49+
return lastWord == "errors" || lastWord == "error"
4850
}
4951

5052
func isValidErrorVarName(s string) bool {

pkg/analyzer/facts_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ func Test_isValidErrorArrayTypeName(t *testing.T) {
188188
"validationErrors",
189189
"ERRORS",
190190
"errors",
191+
"IncompatiblePairError",
192+
"multiError",
191193
} {
192194
if !isValidErrorArrayTypeName(tt) {
193195
t.Errorf("%q must be valid error array type name", tt)
@@ -197,6 +199,10 @@ func Test_isValidErrorArrayTypeName(t *testing.T) {
197199
for _, tt := range []string{
198200
"ErrorsFromValidation",
199201
"errorsFromValidation",
202+
"ErrorMulti",
203+
"ErrorsOfProcessing",
204+
"processingErrs",
205+
"ValidationErr",
200206
} {
201207
if isValidErrorArrayTypeName(tt) {
202208
t.Errorf("%q must be invalid error array type name", tt)

pkg/analyzer/testdata/src/regular/error_types_exceptions.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,30 @@ type tenErrors [10]string
1818

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

21-
type MultiError []error // want "the error type name `MultiError` should conform to the `XxxErrors` format"
21+
type MultiErr []error // want "the error type name `MultiErr` should conform to the `XxxErrors` or `XxxError` format"
22+
func (me MultiErr) Error() string { return "" }
23+
24+
type multiErr []error // want "the error type name `multiErr` should conform to the `xxxErrors` or `xxxError` format"
25+
func (me multiErr) Error() string { return "" }
26+
27+
type Twoerr [2]error // want "the error type name `Twoerr` should conform to the `XxxErrors` or `XxxError` format"
28+
func (te Twoerr) Error() string { return te[0].Error() + "\n" + te[1].Error() }
29+
30+
type twoErrorss [2]error // want "the error type name `twoErrorss` should conform to the `xxxErrors` or `xxxError` format"
31+
func (te twoErrorss) Error() string { return te[0].Error() + "\n" + te[1].Error() }
32+
33+
type MultiError []error
34+
2235
func (me MultiError) Error() string { return "" }
2336

24-
type multiError []error // want "the error type name `multiError` should conform to the `xxxErrors` format"
37+
type multiError []error
38+
2539
func (me multiError) Error() string { return "" }
2640

27-
type TwoError [2]error // want "the error type name `TwoError` should conform to the `XxxErrors` format"
41+
type TwoError [2]error
42+
2843
func (te TwoError) Error() string { return te[0].Error() + "\n" + te[1].Error() }
2944

30-
type twoError [2]error // want "the error type name `twoError` should conform to the `xxxErrors` format"
45+
type twoError [2]error
46+
3147
func (te twoError) Error() string { return te[0].Error() + "\n" + te[1].Error() }

0 commit comments

Comments
 (0)