Skip to content

Commit

Permalink
Add a strict and lax mode for each list given (#70)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
dixonwille authored Oct 31, 2023
1 parent 32ab713 commit f7542dc
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 51 deletions.
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

0 comments on commit f7542dc

Please sign in to comment.