forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautogenerated_exclude.go
172 lines (134 loc) · 4.33 KB
/
autogenerated_exclude.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package processors
import (
"errors"
"fmt"
"go/parser"
"go/token"
"path/filepath"
"regexp"
"strings"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
const (
genCodeGenerated = "code generated"
genDoNotEdit = "do not edit"
genAutoFile = "autogenerated file" // easyjson
)
var _ Processor = &AutogeneratedExclude{}
type fileSummary struct {
generated bool
}
type AutogeneratedExclude struct {
debugf logutils.DebugFunc
strict bool
strictPattern *regexp.Regexp
fileSummaryCache map[string]*fileSummary
}
func NewAutogeneratedExclude(strict bool) *AutogeneratedExclude {
return &AutogeneratedExclude{
debugf: logutils.Debug(logutils.DebugKeyAutogenExclude),
strict: strict,
strictPattern: regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`),
fileSummaryCache: map[string]*fileSummary{},
}
}
func (p *AutogeneratedExclude) Name() string {
return "autogenerated_exclude"
}
func (p *AutogeneratedExclude) Process(issues []result.Issue) ([]result.Issue, error) {
return filterIssuesErr(issues, p.shouldPassIssue)
}
func (p *AutogeneratedExclude) Finish() {}
func (p *AutogeneratedExclude) shouldPassIssue(issue *result.Issue) (bool, error) {
if issue.FromLinter == "typecheck" {
// don't hide typechecking errors in generated files: users expect to see why the project isn't compiling
return true, nil
}
if filepath.Base(issue.FilePath()) == "go.mod" {
return true, nil
}
if !isGoFile(issue.FilePath()) {
return false, nil
}
// The file is already known.
fs := p.fileSummaryCache[issue.FilePath()]
if fs != nil {
return !fs.generated, nil
}
fs = &fileSummary{}
p.fileSummaryCache[issue.FilePath()] = fs
if issue.FilePath() == "" {
return false, errors.New("no file path for issue")
}
if p.strict {
var err error
fs.generated, err = p.isGeneratedFileStrict(issue.FilePath())
if err != nil {
return false, fmt.Errorf("failed to get doc of file %s: %w", issue.FilePath(), err)
}
} else {
doc, err := getComments(issue.FilePath())
if err != nil {
return false, fmt.Errorf("failed to get doc of file %s: %w", issue.FilePath(), err)
}
fs.generated = p.isGeneratedFileLax(doc)
}
p.debugf("file %q is generated: %t", issue.FilePath(), fs.generated)
// don't report issues for autogenerated files
return !fs.generated, nil
}
// isGeneratedFileLax reports whether the source file is generated code.
// Using a bit laxer rules than https://go.dev/s/generatedcode to match more generated code.
// See https://github.com/golangci/golangci-lint/issues/48 and https://github.com/golangci/golangci-lint/issues/72.
func (p *AutogeneratedExclude) isGeneratedFileLax(doc string) bool {
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile}
doc = strings.ToLower(doc)
for _, marker := range markers {
if strings.Contains(doc, marker) {
p.debugf("doc contains marker %q: file is generated", marker)
return true
}
}
p.debugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
return false
}
// Based on https://go.dev/s/generatedcode
// > This line must appear before the first non-comment, non-blank text in the file.
func (p *AutogeneratedExclude) isGeneratedFileStrict(filePath string) (bool, error) {
file, err := parser.ParseFile(token.NewFileSet(), filePath, nil, parser.PackageClauseOnly|parser.ParseComments)
if err != nil {
return false, fmt.Errorf("failed to parse file: %w", err)
}
if file == nil || len(file.Comments) == 0 {
return false, nil
}
for _, comment := range file.Comments {
if comment.Pos() > file.Package {
return false, nil
}
for _, line := range comment.List {
generated := p.strictPattern.MatchString(line.Text)
if generated {
p.debugf("doc contains ignore expression: file is generated")
return true, nil
}
}
}
return false, nil
}
func getComments(filePath string) (string, error) {
fset := token.NewFileSet()
syntax, err := parser.ParseFile(fset, filePath, nil, parser.PackageClauseOnly|parser.ParseComments)
if err != nil {
return "", fmt.Errorf("failed to parse file: %w", err)
}
var docLines []string
for _, c := range syntax.Comments {
docLines = append(docLines, strings.TrimSpace(c.Text()))
}
return strings.Join(docLines, "\n"), nil
}
func isGoFile(name string) bool {
return filepath.Ext(name) == ".go"
}