Skip to content

Commit f7542dc

Browse files
authored
Add a strict and lax mode for each list given (#70)
* add a strict and lax mode for each list given Signed-off-by: Will Dixon <[email protected]> * Add tests to main configuration Signed-off-by: Will Dixon <[email protected]> --------- Signed-off-by: Will Dixon <[email protected]>
1 parent 32ab713 commit f7542dc

File tree

8 files changed

+342
-51
lines changed

8 files changed

+342
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
*.out
1313

1414
.idea
15+
.null-ls*.go

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The following is an example configuration file.
2323
"$all",
2424
"!$test"
2525
],
26+
"listMode": "Strict",
2627
"allow": [
2728
"$gostd",
2829
"github.com/OpenPeeDeeP"
@@ -35,6 +36,7 @@ The following is an example configuration file.
3536
"files": [
3637
"$test"
3738
],
39+
"listMode": "Lax",
3840
"deny": {
3941
"github.com/stretchr/testify": "Please use standard library for tests"
4042
}
@@ -47,6 +49,7 @@ the linter's output.
4749
- `files` - list of file globs that will match this list of settings to compare against
4850
- `allow` - list of allowed packages
4951
- `deny` - map of packages that are not allowed where the value is a suggestion
52+
= `listMode` - the mode to use for package matching
5053

5154
Files are matched using [Globs](https://github.com/gobwas/glob). If the files
5255
list is empty, then all files will match that list. Prefixing a file
@@ -66,6 +69,21 @@ A Prefix List just means that a package will match a value, if the value is a
6669
prefix of the package. Example `github.com/OpenPeeDeeP/depguard` package will match
6770
a value of `github.com/OpenPeeDeeP` but won't match `github.com/OpenPeeDeeP/depguard/v2`.
6871

72+
ListMode is used to determine the package matching priority. There are three
73+
different modes; Original, Strict, and Lax.
74+
75+
Original is the original way that the package was written to use. It is not recommended
76+
to stay with this and is only here for backwards compatibility.
77+
78+
Strict, at its roots, is everything is denied unless in allowed.
79+
80+
Lax, at its roots, is everything is allowed unless it is denied.
81+
82+
There are cases where a package can be matched in both the allow and denied lists.
83+
You may allow a subpackage but deny the root or vice versa. The `settings_tests.go` file
84+
has many scenarios listed out under `TestListImportAllowed`. These tests will stay
85+
up to date as features are added.
86+
6987
### Variables
7088

7189
There are variable replacements for each type of list (file or package). This is

cmd/depguard/main_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ var testfiles embed.FS
1313

1414
var expectedConfigStruct = &depguard.LinterSettings{
1515
"main": &depguard.List{
16-
Files: []string{"$all", "!$test"},
17-
Allow: []string{"$gostd", "github.com/"},
16+
ListMode: "Strict",
17+
Files: []string{"$all", "!$test"},
18+
Allow: []string{"$gostd", "github.com/"},
1819
Deny: map[string]string{
1920
"reflect": "Who needs reflection",
2021
"github.com/OpenPeeDeeP": "Use Something Else",

cmd/depguard/testfiles/.depguard.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"$all",
55
"!$test"
66
],
7+
"listMode": "Strict",
78
"allow": [
89
"$gostd",
910
"github.com/"
@@ -24,4 +25,4 @@
2425
"github.com/OpenPeeDeeP/": "Use Something Else"
2526
}
2627
}
27-
}
28+
}

cmd/depguard/testfiles/.depguard.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ files = [
33
"$all",
44
"!$test"
55
]
6+
listMode = "Strict"
67
allow = [
78
"$gostd",
89
"github.com/"
@@ -19,4 +20,4 @@ allow = [
1920
"github.com/test"
2021
]
2122
[tests.deny]
22-
"github.com/OpenPeeDeeP/" = "Use Something Else"
23+
"github.com/OpenPeeDeeP/" = "Use Something Else"

cmd/depguard/testfiles/.depguard.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ main:
22
files:
33
- "$all"
44
- "!$test"
5+
listMode: Strict
56
allow:
67
- "$gostd"
78
- github.com/
@@ -14,4 +15,4 @@ tests:
1415
allow:
1516
- github.com/test
1617
deny:
17-
github.com/OpenPeeDeeP/: Use Something Else
18+
github.com/OpenPeeDeeP/: Use Something Else

settings.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ type List struct {
2020
type listMode int
2121

2222
const (
23-
listModeStrict listMode = iota
23+
listModeOriginal listMode = iota
24+
listModeStrict
25+
listModeLax
2426
)
2527

2628
type list struct {
@@ -44,9 +46,13 @@ func (l *List) compile() (*list, error) {
4446
// Determine List Mode
4547
switch strings.ToLower(l.ListMode) {
4648
case "":
47-
li.listMode = listModeStrict
49+
li.listMode = listModeOriginal
50+
case "original":
51+
li.listMode = listModeOriginal
4852
case "strict":
4953
li.listMode = listModeStrict
54+
case "lax":
55+
li.listMode = listModeLax
5056
default:
5157
errs = append(errs, fmt.Errorf("%s is not a known list mode", l.ListMode))
5258
}
@@ -131,16 +137,25 @@ func (l *list) fileMatch(fileName string) bool {
131137
}
132138

133139
func (l *list) importAllowed(imp string) (bool, string) {
134-
inAllowed := len(l.allow) == 0
135-
if !inAllowed {
136-
inAllowed, _ = strInPrefixList(imp, l.allow)
140+
inAllowed, aIdx := strInPrefixList(imp, l.allow)
141+
inDenied, dIdx := strInPrefixList(imp, l.deny)
142+
var allowed bool
143+
switch l.listMode {
144+
case listModeOriginal:
145+
inAllowed = len(l.allow) == 0 || inAllowed
146+
allowed = inAllowed && !inDenied
147+
case listModeStrict:
148+
allowed = inAllowed && (!inDenied || len(l.allow[aIdx]) > len(l.deny[dIdx]))
149+
case listModeLax:
150+
allowed = !inDenied || (inAllowed && len(l.allow[aIdx]) > len(l.deny[dIdx]))
151+
default:
152+
allowed = false
137153
}
138-
inDenied, suggIdx := strInPrefixList(imp, l.deny)
139154
sugg := ""
140-
if inDenied && suggIdx != -1 {
141-
sugg = l.suggestions[suggIdx]
155+
if !allowed && inDenied && dIdx != -1 {
156+
sugg = l.suggestions[dIdx]
142157
}
143-
return inAllowed && !inDenied, sugg
158+
return allowed, sugg
144159
}
145160

146161
type LinterSettings map[string]*List

0 commit comments

Comments
 (0)