-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsettings.go
More file actions
220 lines (196 loc) · 4.56 KB
/
settings.go
File metadata and controls
220 lines (196 loc) · 4.56 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package depguard
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/OpenPeeDeeP/depguard/v2/internal/utils"
"github.com/gobwas/glob"
)
type List struct {
Files []string `json:"files" yaml:"files" toml:"files" mapstructure:"files"`
Allow []string `json:"allow" yaml:"allow" toml:"allow" mapstructure:"allow"`
Deny map[string]string `json:"deny" yaml:"deny" toml:"deny" mapstructure:"deny"`
}
type list struct {
name string
files []glob.Glob
negFiles []glob.Glob
allow []glob.Glob
deny []glob.Glob
suggestions []string
}
func (l *List) compile() (*list, error) {
if l == nil {
return nil, nil
}
li := &list{}
var errs utils.MultiError
var err error
// Compile Files
for _, f := range l.Files {
var negate bool
if len(f) > 0 && f[0] == '!' {
negate = true
f = f[1:]
}
// Expand File if needed
fs, err := utils.ExpandSlice([]string{f}, utils.PathExpandable)
if err != nil {
errs = append(errs, err)
}
for _, exp := range fs {
g, err := glob.Compile(exp, '/')
if err != nil {
errs = append(errs, fmt.Errorf("%s could not be compiled: %w", exp, err))
continue
}
if negate {
li.negFiles = append(li.negFiles, g)
continue
}
li.files = append(li.files, g)
}
}
if len(l.Allow) > 0 {
// Expand Allow
l.Allow, err = utils.ExpandSlice(l.Allow, utils.PackageExpandable)
if err != nil {
errs = append(errs, err)
}
sort.Strings(l.Allow)
// Sort Allow
li.allow = make([]glob.Glob, 0, len(l.Allow))
for _, pkg := range l.Allow {
glob, err := inputPatternToGlob(pkg)
if err != nil {
errs = append(errs, err)
continue
}
li.allow = append(li.allow, glob)
}
}
if l.Deny != nil {
// Expand Deny Map (to keep suggestions)
err = utils.ExpandMap(l.Deny, utils.PackageExpandable)
if err != nil {
errs = append(errs, err)
}
// Split Deny Into Package Slice
// sort before compiling as globs are opaque
pkgs := make([]string, 0, len(l.Deny))
for k := range l.Deny {
pkgs = append(pkgs, k)
}
sort.Strings(pkgs)
li.deny = make([]glob.Glob, 0, len(pkgs))
li.suggestions = make([]string, 0, len(pkgs))
for _, pkg := range pkgs {
glob, err := inputPatternToGlob(pkg)
if err != nil {
errs = append(errs, err)
continue
}
li.deny = append(li.deny, glob)
li.suggestions = append(li.suggestions, strings.TrimSpace(l.Deny[pkg]))
}
}
// Populate the type of this list
if len(li.allow) == 0 && len(li.deny) == 0 {
errs = append(errs, errors.New("must have an Allow and/or Deny package list"))
}
if len(errs) > 0 {
return nil, errs
}
return li, nil
}
func (l *list) fileMatch(fileName string) bool {
inDenied, _ := strInGlobList(fileName, l.negFiles)
if inDenied {
return false
}
if len(l.files) == 0 {
// No allow list matches all
return true
}
inAllowed, _ := strInGlobList(fileName, l.files)
return inAllowed
}
func (l *list) importAllowed(imp string) (bool, string) {
inAllowed := len(l.allow) == 0
if !inAllowed {
inAllowed, _ = strInGlobList(imp, l.allow)
}
inDenied, suggIdx := strInGlobList(imp, l.deny)
sugg := ""
if inDenied && suggIdx != -1 {
sugg = l.suggestions[suggIdx]
}
return inAllowed && !inDenied, sugg
}
type LinterSettings map[string]*List
type linterSettings []*list
func (l LinterSettings) compile() (linterSettings, error) {
if len(l) == 0 {
// Only allow $gostd in all files
set := &List{
Files: []string{"$all"},
Allow: []string{"$gostd"},
}
li, err := set.compile()
if err != nil {
return nil, err
}
li.name = "Main"
return linterSettings{li}, nil
}
names := make([]string, 0, len(l))
for name := range l {
names = append(names, name)
}
sort.Strings(names)
li := make(linterSettings, 0, len(l))
var errs utils.MultiError
for _, name := range names {
c, err := l[name].compile()
if err != nil {
errs = append(errs, err)
continue
}
if c == nil {
continue
}
c.name = name
li = append(li, c)
}
if len(errs) > 0 {
return nil, errs
}
return li, nil
}
func (ls linterSettings) whichLists(fileName string) []*list {
var matches []*list
for _, l := range ls {
if l.fileMatch(fileName) {
matches = append(matches, l)
}
}
return matches
}
func strInGlobList(str string, globList []glob.Glob) (bool, int) {
for idx, g := range globList {
if g.Match(str) {
return true, idx
}
}
return false, 0
}
func inputPatternToGlob(pattern string) (glob.Glob, error) {
lastIdx := len(pattern) - 1
if pattern[lastIdx] == '$' {
pattern = pattern[:lastIdx]
} else {
pattern += "**"
}
return glob.Compile(pattern, '/')
}