-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nitpicking.go
141 lines (119 loc) · 2.88 KB
/
nitpicking.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package nit
import (
"go/ast"
"go/parser"
"go/token"
"github.com/pkg/errors"
)
type (
// Nitpicker defines the linter.
Nitpicker struct {
LocalPath string
SkipGeneratedFile bool
NoLint bool
//-
fset *token.FileSet
fsm *FileSectionMachine
comments *BreakComments
tvalidator *TypesValidator
fvalidator *FuncsValidator
mvalidator *MethodsValidator
}
)
// Validate nitpicks the filename.
func (v *Nitpicker) Validate(filename string) error {
v.fset = token.NewFileSet()
f, err := parser.ParseFile(v.fset, filename, nil, parser.ParseComments)
if err != nil {
return errors.Wrap(err, "parsing file failed")
}
v.comments = NewBreakComments(v.fset, f.Comments)
if v.comments.HasGeneratedCode() && v.SkipGeneratedFile {
return nil
}
if v.comments.HasNoLintNit() && v.NoLint {
return nil
}
for _, s := range f.Decls {
if err := v.validateToken(s); err != nil {
return err
}
}
return nil
}
//nolint:gocyclo,funlen
func (v *Nitpicker) validateToken(d ast.Decl) error {
var (
err error
genDecl *ast.GenDecl
funcDecl *ast.FuncDecl
nextState FileSection
)
switch t := d.(type) {
case *ast.GenDecl:
genDecl = t
nextState, err = NewGenDeclFileSection(genDecl)
case *ast.FuncDecl:
funcDecl = t
nextState, err = NewFuncDeclFileSection(funcDecl)
default:
return errors.New("unknown declaration state")
}
if err != nil {
return err
}
if v.fsm == nil {
fsm, err := NewFileSectionMachine(nextState)
if err != nil {
return errors.Wrap(err, v.fset.PositionFor(d.Pos(), false).String())
}
v.fsm = fsm
}
if err = v.fsm.Transition(nextState); err != nil {
return errors.Wrap(err, v.fset.PositionFor(d.Pos(), false).String())
}
switch nextState {
case FileSectionImports:
validator := NewImportsValidator(v.LocalPath)
if err := validator.Validate(genDecl, v.fset); err != nil {
return err
}
case FileSectionTypes:
if v.tvalidator != nil {
return errors.New("only one `type` section block is allowed per file")
}
v.tvalidator = NewTypesValidator(v.comments)
if err := v.tvalidator.Validate(genDecl, v.fset); err != nil {
return err
}
case FileSectionConsts:
validator := &ConstsValidator{}
if err := validator.Validate(genDecl, v.fset); err != nil {
return err
}
case FileSectionVars:
validator := &VarsValidator{}
if err := validator.Validate(genDecl, v.fset); err != nil {
return err
}
case FileSectionFuncs:
if v.fvalidator == nil {
v.fvalidator = NewFuncsValidator(v.comments)
}
if err := v.fvalidator.Validate(funcDecl, v.fset); err != nil {
return err
}
case FileSectionMethods:
if v.mvalidator == nil {
mvalidator, err := NewMethodsValidator(v.comments, v.tvalidator)
if err != nil {
return err
}
v.mvalidator = mvalidator
}
if err := v.mvalidator.Validate(funcDecl, v.fset); err != nil {
return err
}
}
return nil
}