Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a strict and lax mode for each list given #70

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
*.out

.idea
.null-ls*.go
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following is an example configuration file.
"$all",
"!$test"
],
"listMode": "Strict",
"allow": [
"$gostd",
"github.com/OpenPeeDeeP"
Expand All @@ -35,6 +36,7 @@ The following is an example configuration file.
"files": [
"$test"
],
"listMode": "Lax",
"deny": {
"github.com/stretchr/testify": "Please use standard library for tests"
}
Expand All @@ -47,6 +49,7 @@ the linter's output.
- `files` - list of file globs that will match this list of settings to compare against
- `allow` - list of allowed packages
- `deny` - map of packages that are not allowed where the value is a suggestion
= `listMode` - the mode to use for package matching

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

ListMode is used to determine the package matching priority. There are three
different modes; Original, Strict, and Lax.

Original is the original way that the package was written to use. It is not recommended
to stay with this and is only here for backwards compatibility.

Strict, at its roots, is everything is denied unless in allowed.

Lax, at its roots, is everything is allowed unless it is denied.

There are cases where a package can be matched in both the allow and denied lists.
You may allow a subpackage but deny the root or vice versa. The `settings_tests.go` file
has many scenarios listed out under `TestListImportAllowed`. These tests will stay
up to date as features are added.

### Variables

There are variable replacements for each type of list (file or package). This is
Expand Down
5 changes: 3 additions & 2 deletions cmd/depguard/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ var testfiles embed.FS

var expectedConfigStruct = &depguard.LinterSettings{
"main": &depguard.List{
Files: []string{"$all", "!$test"},
Allow: []string{"$gostd", "github.com/"},
ListMode: "Strict",
Files: []string{"$all", "!$test"},
Allow: []string{"$gostd", "github.com/"},
Deny: map[string]string{
"reflect": "Who needs reflection",
"github.com/OpenPeeDeeP": "Use Something Else",
Expand Down
3 changes: 2 additions & 1 deletion cmd/depguard/testfiles/.depguard.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"$all",
"!$test"
],
"listMode": "Strict",
"allow": [
"$gostd",
"github.com/"
Expand All @@ -24,4 +25,4 @@
"github.com/OpenPeeDeeP/": "Use Something Else"
}
}
}
}
3 changes: 2 additions & 1 deletion cmd/depguard/testfiles/.depguard.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ files = [
"$all",
"!$test"
]
listMode = "Strict"
allow = [
"$gostd",
"github.com/"
Expand All @@ -19,4 +20,4 @@ allow = [
"github.com/test"
]
[tests.deny]
"github.com/OpenPeeDeeP/" = "Use Something Else"
"github.com/OpenPeeDeeP/" = "Use Something Else"
3 changes: 2 additions & 1 deletion cmd/depguard/testfiles/.depguard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ main:
files:
- "$all"
- "!$test"
listMode: Strict
allow:
- "$gostd"
- github.com/
Expand All @@ -14,4 +15,4 @@ tests:
allow:
- github.com/test
deny:
github.com/OpenPeeDeeP/: Use Something Else
github.com/OpenPeeDeeP/: Use Something Else
33 changes: 24 additions & 9 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type List struct {
type listMode int

const (
listModeStrict listMode = iota
listModeOriginal listMode = iota
listModeStrict
listModeLax
)

type list struct {
Expand All @@ -44,9 +46,13 @@ func (l *List) compile() (*list, error) {
// Determine List Mode
switch strings.ToLower(l.ListMode) {
case "":
li.listMode = listModeStrict
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))
}
Expand Down Expand Up @@ -131,16 +137,25 @@ func (l *list) fileMatch(fileName string) bool {
}

func (l *list) importAllowed(imp string) (bool, string) {
inAllowed := len(l.allow) == 0
if !inAllowed {
inAllowed, _ = strInPrefixList(imp, l.allow)
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
}
inDenied, suggIdx := strInPrefixList(imp, l.deny)
sugg := ""
if inDenied && suggIdx != -1 {
sugg = l.suggestions[suggIdx]
if !allowed && inDenied && dIdx != -1 {
sugg = l.suggestions[dIdx]
}
return inAllowed && !inDenied, sugg
return allowed, sugg
}

type LinterSettings map[string]*List
Expand Down
Loading