-
Notifications
You must be signed in to change notification settings - Fork 18
Compile allow and deny list as globs to allow wildcards #67
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
Open
philwitty
wants to merge
1
commit into
OpenPeeDeeP:v2
Choose a base branch
from
philwitty:allow-globs-for-pkgs
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,8 @@ type list struct { | |
| name string | ||
| files []glob.Glob | ||
| negFiles []glob.Glob | ||
| allow []string | ||
| deny []string | ||
| allow []glob.Glob | ||
| deny []glob.Glob | ||
| suggestions []string | ||
| } | ||
|
|
||
|
|
@@ -65,11 +65,18 @@ func (l *List) compile() (*list, error) { | |
| if err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| sort.Strings(l.Allow) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This modifies the input slice which can be unexpected behavior to the consumer (if there are any). That is why I had it copied to an internal slice before sort. |
||
|
|
||
| // Sort Allow | ||
| li.allow = make([]string, len(l.Allow)) | ||
| copy(li.allow, l.Allow) | ||
| sort.Strings(li.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 { | ||
|
|
@@ -80,18 +87,24 @@ func (l *List) compile() (*list, error) { | |
| } | ||
|
|
||
| // 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) | ||
| // 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) | ||
|
|
||
| // 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])) | ||
| 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])) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -107,17 +120,24 @@ func (l *List) compile() (*list, error) { | |
| } | ||
|
|
||
| func (l *list) fileMatch(fileName string) bool { | ||
| inAllowed := len(l.files) == 0 || strInGlobList(fileName, l.files) | ||
| inDenied := strInGlobList(fileName, l.negFiles) | ||
| return inAllowed && !inDenied | ||
| 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, _ = strInPrefixList(imp, l.allow) | ||
| inAllowed, _ = strInGlobList(imp, l.allow) | ||
| } | ||
| inDenied, suggIdx := strInPrefixList(imp, l.deny) | ||
| inDenied, suggIdx := strInGlobList(imp, l.deny) | ||
| sugg := "" | ||
| if inDenied && suggIdx != -1 { | ||
| sugg = l.suggestions[suggIdx] | ||
|
|
@@ -179,29 +199,22 @@ func (ls linterSettings) whichLists(fileName string) []*list { | |
| return matches | ||
| } | ||
|
|
||
| func strInGlobList(str string, globList []glob.Glob) bool { | ||
| for _, g := range globList { | ||
| func strInGlobList(str string, globList []glob.Glob) (bool, int) { | ||
| for idx, g := range globList { | ||
| if g.Match(str) { | ||
| return true | ||
| return true, idx | ||
| } | ||
| } | ||
| return false | ||
| return false, 0 | ||
| } | ||
|
|
||
| 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 | ||
| func inputPatternToGlob(pattern string) (glob.Glob, error) { | ||
| lastIdx := len(pattern) - 1 | ||
| if pattern[lastIdx] == '$' { | ||
| pattern = pattern[:lastIdx] | ||
| } else { | ||
| pattern += "**" | ||
| } | ||
| return glob.Compile(pattern, '/') | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File may not be json.