-
Notifications
You must be signed in to change notification settings - Fork 17
/
settings.go
240 lines (214 loc) · 5.46 KB
/
settings.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package depguard
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/OpenPeeDeeP/depguard/v2/internal/utils"
"github.com/gobwas/glob"
)
type List struct {
ListMode string `json:"listMode" yaml:"listMode" toml:"listMode" mapstructure:"listMode"`
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 listMode int
const (
listModeOriginal listMode = iota
listModeStrict
listModeLax
)
type list struct {
listMode listMode
name string
files []glob.Glob
negFiles []glob.Glob
allow []string
deny []string
suggestions []string
}
func (l *List) compile() (*list, error) {
if l == nil {
return nil, nil
}
li := &list{}
var errs utils.MultiError
var err error
// Determine List Mode
switch strings.ToLower(l.ListMode) {
case "":
li.listMode = listModeOriginal
case "original":
li.listMode = listModeOriginal
case "strict":
li.listMode = listModeStrict
case "lax":
li.listMode = listModeLax
default:
errs = append(errs, fmt.Errorf("%s is not a known list mode", l.ListMode))
}
// 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 Allow
li.allow = make([]string, len(l.Allow))
copy(li.allow, l.Allow)
sort.Strings(li.allow)
}
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
li.deny = make([]string, 0, len(l.Deny))
for pkg := range l.Deny {
li.deny = append(li.deny, pkg)
}
// Sort Deny
sort.Strings(li.deny)
// Populate Suggestions to match the Deny order
li.suggestions = make([]string, 0, len(li.deny))
for _, dp := range li.deny {
li.suggestions = append(li.suggestions, strings.TrimSpace(l.Deny[dp]))
}
}
// 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 {
inAllowed := len(l.files) == 0 || strInGlobList(fileName, l.files)
inDenied := strInGlobList(fileName, l.negFiles)
return inAllowed && !inDenied
}
func (l *list) importAllowed(imp string) (bool, string) {
inAllowed, aIdx := strInPrefixList(imp, l.allow)
inDenied, dIdx := strInPrefixList(imp, l.deny)
var allowed bool
switch l.listMode {
case listModeOriginal:
inAllowed = len(l.allow) == 0 || inAllowed
allowed = inAllowed && !inDenied
case listModeStrict:
allowed = inAllowed && (!inDenied || len(l.allow[aIdx]) > len(l.deny[dIdx]))
case listModeLax:
allowed = !inDenied || (inAllowed && len(l.allow[aIdx]) > len(l.deny[dIdx]))
default:
allowed = false
}
sugg := ""
if !allowed && inDenied && dIdx != -1 {
sugg = l.suggestions[dIdx]
}
return allowed, 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 {
for _, g := range globList {
if g.Match(str) {
return true
}
}
return false
}
func strInPrefixList(str string, prefixList []string) (bool, int) {
// Idx represents where in the prefix slice the passed in string would go
// when sorted. -1 Just means that it would be at the very front of the slice.
idx := sort.Search(len(prefixList), func(i int) bool {
return strings.TrimRight(prefixList[i], "$") > str
}) - 1
// This means that the string passed in has no way to be prefixed by anything
// in the prefix list as it is already smaller then everything
if idx == -1 {
return false, idx
}
ioc := prefixList[idx]
if ioc[len(ioc)-1] == '$' {
return str == ioc[:len(ioc)-1], idx
}
return strings.HasPrefix(str, prefixList[idx]), idx
}