aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Antonboom/errname/pkg
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2025-01-22 16:07:17 +0100
committerTaras Madan <tarasmadan@google.com>2025-01-23 10:42:36 +0000
commit7b4377ad9d8a7205416df8d6217ef2b010f89481 (patch)
treee6fec4fd12ff807a16d847923f501075bf71d16c /vendor/github.com/Antonboom/errname/pkg
parent475a4c203afb8b7d3af51c4fd32bb170ff32a45e (diff)
vendor: delete
Diffstat (limited to 'vendor/github.com/Antonboom/errname/pkg')
-rw-r--r--vendor/github.com/Antonboom/errname/pkg/analyzer/analyzer.go108
-rw-r--r--vendor/github.com/Antonboom/errname/pkg/analyzer/facts.go95
2 files changed, 0 insertions, 203 deletions
diff --git a/vendor/github.com/Antonboom/errname/pkg/analyzer/analyzer.go b/vendor/github.com/Antonboom/errname/pkg/analyzer/analyzer.go
deleted file mode 100644
index 2b8794dc2..000000000
--- a/vendor/github.com/Antonboom/errname/pkg/analyzer/analyzer.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package analyzer
-
-import (
- "go/ast"
- "go/token"
- "go/types"
- "unicode"
-
- "golang.org/x/tools/go/analysis"
- "golang.org/x/tools/go/analysis/passes/inspect"
- "golang.org/x/tools/go/ast/inspector"
-)
-
-// New returns new errname analyzer.
-func New() *analysis.Analyzer {
- return &analysis.Analyzer{
- Name: "errname",
- Doc: "Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.",
- Run: run,
- Requires: []*analysis.Analyzer{inspect.Analyzer},
- }
-}
-
-func run(pass *analysis.Pass) (interface{}, error) {
- insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
-
- insp.Nodes([]ast.Node{
- (*ast.TypeSpec)(nil),
- (*ast.ValueSpec)(nil),
- (*ast.FuncDecl)(nil),
- }, func(node ast.Node, push bool) bool {
- if !push {
- return false
- }
-
- switch v := node.(type) {
- case *ast.FuncDecl:
- return false
-
- case *ast.ValueSpec:
- if len(v.Names) != 1 {
- return false
- }
- ident := v.Names[0]
-
- if exprImplementsError(pass, ident) && !isValidErrorVarName(ident.Name) {
- reportAboutSentinelError(pass, v.Pos(), ident.Name)
- }
- return false
-
- case *ast.TypeSpec:
- tt := pass.TypesInfo.TypeOf(v.Name)
- if tt == nil {
- return false
- }
- // NOTE(a.telyshev): Pointer is the hack against Error() method with pointer receiver.
- if !typeImplementsError(types.NewPointer(tt)) {
- return false
- }
-
- name := v.Name.Name
- if _, ok := v.Type.(*ast.ArrayType); ok {
- if !isValidErrorArrayTypeName(name) {
- reportAboutArrayErrorType(pass, v.Pos(), name)
- }
- } else if !isValidErrorTypeName(name) {
- reportAboutErrorType(pass, v.Pos(), name)
- }
- return false
- }
-
- return true
- })
-
- return nil, nil //nolint:nilnil
-}
-
-func reportAboutErrorType(pass *analysis.Pass, typePos token.Pos, typeName string) {
- var form string
- if unicode.IsLower([]rune(typeName)[0]) {
- form = "xxxError"
- } else {
- form = "XxxError"
- }
-
- 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]) {
- form = "errXxx"
- } else {
- form = "ErrXxx"
- }
- pass.Reportf(pos, "the sentinel error name `%s` should conform to the `%s` format", varName, form)
-}
diff --git a/vendor/github.com/Antonboom/errname/pkg/analyzer/facts.go b/vendor/github.com/Antonboom/errname/pkg/analyzer/facts.go
deleted file mode 100644
index 04e14fb68..000000000
--- a/vendor/github.com/Antonboom/errname/pkg/analyzer/facts.go
+++ /dev/null
@@ -1,95 +0,0 @@
-package analyzer
-
-import (
- "go/ast"
- "go/types"
- "strings"
- "unicode"
-
- "golang.org/x/tools/go/analysis"
-)
-
-var errorIface = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
-
-func exprImplementsError(pass *analysis.Pass, e ast.Expr) bool {
- return typeImplementsError(pass.TypesInfo.TypeOf(e))
-}
-
-func typeImplementsError(t types.Type) bool {
- return t != nil && types.Implements(t, errorIface)
-}
-
-func isValidErrorTypeName(s string) bool {
- if isInitialism(s) {
- return true
- }
-
- words := split(s)
- wordsCnt := wordsCount(words)
-
- if wordsCnt["error"] != 1 {
- return false
- }
- return words[len(words)-1] == "error"
-}
-
-func isValidErrorArrayTypeName(s string) bool {
- if isInitialism(s) {
- return true
- }
-
- words := split(s)
- wordsCnt := wordsCount(words)
-
- if wordsCnt["errors"] != 1 && wordsCnt["error"] != 1 {
- return false
- }
-
- lastWord := words[len(words)-1]
- return lastWord == "errors" || lastWord == "error"
-}
-
-func isValidErrorVarName(s string) bool {
- if isInitialism(s) {
- return true
- }
-
- words := split(s)
- wordsCnt := wordsCount(words)
-
- if wordsCnt["err"] != 1 {
- return false
- }
- return words[0] == "err"
-}
-
-func isInitialism(s string) bool {
- return strings.ToLower(s) == s || strings.ToUpper(s) == s
-}
-
-func split(s string) []string {
- var words []string
- ss := []rune(s)
-
- var b strings.Builder
- b.WriteRune(ss[0])
-
- for _, r := range ss[1:] {
- if unicode.IsUpper(r) {
- words = append(words, strings.ToLower(b.String()))
- b.Reset()
- }
- b.WriteRune(r)
- }
-
- words = append(words, strings.ToLower(b.String()))
- return words
-}
-
-func wordsCount(w []string) map[string]int {
- result := make(map[string]int, len(w))
- for _, ww := range w {
- result[ww]++
- }
- return result
-}