-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consts.go
57 lines (46 loc) · 1.16 KB
/
consts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package nit
import (
"go/ast"
"go/token"
"github.com/pkg/errors"
)
type (
// ConstsValidator defines the type including the rules used for validating
// the `const` sections.
ConstsValidator struct {
sortedNamesValidator
}
)
// Validate makes sure the implemented `const` declaration satisfies the
// following rules:
// * Group declaration is parenthesized
// * Declarations are sorted
func (c *ConstsValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error { //nolint: gocyclo
c.identType = "Const"
if !v.Lparen.IsValid() {
return errors.Wrap(errors.New("expected parenthesized declaration"), fset.PositionFor(v.Pos(), false).String())
}
for _, t := range v.Specs {
errPrefix := fset.PositionFor(t.Pos(), false).String()
s, ok := t.(*ast.ValueSpec)
if !ok {
return errors.Wrap(errors.Errorf("invalid token %+v", t), errPrefix)
}
for _, vss := range s.Values {
bexpr, ok := vss.(*ast.BinaryExpr)
if ok {
vss = bexpr.Y
}
_, ok = vss.(*ast.Ident)
if ok {
return nil // iota
}
}
for _, name := range s.Names {
if err := c.validateName(errPrefix, name); err != nil {
return err
}
}
}
return nil
}