This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
/
analyzer.go
144 lines (124 loc) · 3.8 KB
/
analyzer.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
142
143
144
package partialfields
import (
"errors"
"flag"
"fmt"
"go/ast"
"regexp"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"github.com/kamiaka/go-partialfields/internal/pkg"
)
func NewAnalyzer() *analysis.Analyzer {
var (
f flag.FlagSet
argExcludesEmptyLiteral = true
argExcludesOptionalField = false
argExcludesExportedField = false
argExcludesUnexportedField = false
argExcludeFileRegExpPat = ""
)
f.BoolVar(&argExcludesEmptyLiteral, "excludes-empty-literal", argExcludesEmptyLiteral, "excludes struct literal that has no field.")
f.BoolVar(&argExcludesOptionalField, "excludes-optional-field", argExcludesOptionalField, "excludes field that has comment starts with optional.")
f.BoolVar(&argExcludesExportedField, "excludes-exported-field", argExcludesExportedField, "excludes exported field")
f.BoolVar(&argExcludesUnexportedField, "excludes-unexported-field", argExcludesUnexportedField, "excludes unexported field")
f.StringVar(&argExcludeFileRegExpPat, "exclude-file-pattern", argExcludeFileRegExpPat, `exclude filename pattern (e.g., "_test\\.go$")`)
return &analysis.Analyzer{
Name: "partialfields",
Doc: "check for all fields are defined in the struct",
Flags: f,
RunDespiteErrors: false,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
ResultType: nil,
FactTypes: nil,
Run: func(pass *analysis.Pass) (interface{}, error) {
var skipFileRegExp *regexp.Regexp
if argExcludeFileRegExpPat != "" {
r, err := regexp.Compile(argExcludeFileRegExpPat)
if err != nil {
return nil, fmt.Errorf("invalid ")
}
skipFileRegExp = r
}
p := pkg.New()
for _, f := range pass.Files {
if isGeneratedFile(f) {
continue
}
if err := p.SetImports(f.Imports); err != nil {
return nil, err
}
v := &visitor{
excludesExportedField: argExcludesExportedField,
excludesUnexportedField: argExcludesUnexportedField,
excludeFilePattern: skipFileRegExp,
errors: nil,
pkg: p,
files: pass.Files,
cmap: ast.NewCommentMap(pass.Fset, f, f.Comments),
prev: nil,
prevs: map[ast.Node]ast.Node{},
pass: pass,
}
ast.Walk(v, f)
// for k, ls := range v.cmap {
// fmt.Println("")
// for _, cg := range ls {
// for _, c := range cg.List {
// fmt.Println(c.Text)
// }
// }
// fmt.Printf("%#v\n", k)
// }
// fmt.Println("===========================")
// for k, ls := range v.cmap {
// fmt.Printf("\n%#v\n", k)
// if id, ok := k.(*ast.Ident); ok && id.Obj != nil {
// fmt.Printf(" Decl: %#v\n", id.Obj.Decl)
// }
// for _, cg := range ls {
// for _, c := range cg.List {
// fmt.Printf(" %v\n", c.Text)
// }
// }
// }
// fmt.Println("---------------------------")
// ast.Inspect(f, func(n ast.Node) bool {
// switch n.(type) {
// case *ast.StructType:
// fmt.Printf("\nStruct")
// default:
// fmt.Printf("\nX: %#v\n", n)
// }
// return true
// })
fmt.Println("\n-------------------------")
for _, err := range v.errors {
var e *IncompleteError
if !errors.As(err, &e) {
return nil, err
}
pass.Report(analysis.Diagnostic{
Pos: e.Pos(),
End: e.End(),
Message: e.Error(),
})
}
}
return nil, nil
},
}
}
var generatedCommentPat = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
func isGeneratedFile(f *ast.File) bool {
for _, g := range f.Comments {
for _, c := range g.List {
if generatedCommentPat.MatchString(c.Text) {
return true
}
}
}
return false
}