@@ -18,8 +18,13 @@ import (
18
18
"strconv"
19
19
"strings"
20
20
"unicode"
21
+
22
+ "github.com/armon/go-radix"
21
23
)
22
24
25
+ // wildcard ignore suffix
26
+ const wcIgnoreSuffix = "*"
27
+
23
28
// Package represents a Go package. It contains a subset of the information
24
29
// go/build.Package does.
25
30
type Package struct {
@@ -550,6 +555,9 @@ func (t PackageTree) ToReachMap(main, tests, backprop bool, ignore map[string]bo
550
555
ignore = make (map [string ]bool )
551
556
}
552
557
558
+ // Radix tree for ignore prefixes.
559
+ xt := CreateIgnorePrefixTree (ignore )
560
+
553
561
// world's simplest adjacency list
554
562
workmap := make (map [string ]wm )
555
563
@@ -572,6 +580,14 @@ func (t PackageTree) ToReachMap(main, tests, backprop bool, ignore map[string]bo
572
580
continue
573
581
}
574
582
583
+ if xt != nil {
584
+ // Skip ignored packages prefix matches
585
+ _ , _ , ok := xt .LongestPrefix (ip )
586
+ if ok {
587
+ continue
588
+ }
589
+ }
590
+
575
591
// TODO (kris-nova) Disable to get staticcheck passing
576
592
//imps = imps[:0]
577
593
@@ -1026,3 +1042,42 @@ func uniq(a []string) []string {
1026
1042
}
1027
1043
return a [:i ]
1028
1044
}
1045
+
1046
+ // CreateIgnorePrefixTree takes a set of strings to be ignored and returns a
1047
+ // trie consisting of strings prefixed with wildcard ignore suffix (*).
1048
+ func CreateIgnorePrefixTree (ig map [string ]bool ) * radix.Tree {
1049
+ var xt * radix.Tree
1050
+
1051
+ // Create a sorted list of all the ignores to have a proper order in
1052
+ // ignores parsing.
1053
+ sortedIgnores := make ([]string , len (ig ))
1054
+ for k := range ig {
1055
+ sortedIgnores = append (sortedIgnores , k )
1056
+ }
1057
+ sort .Strings (sortedIgnores )
1058
+
1059
+ for _ , i := range sortedIgnores {
1060
+ // Skip global ignore.
1061
+ if i == "*" {
1062
+ continue
1063
+ }
1064
+
1065
+ // Check if it's a recursive ignore.
1066
+ if strings .HasSuffix (i , wcIgnoreSuffix ) {
1067
+ // Create trie if it doesn't exists.
1068
+ if xt == nil {
1069
+ xt = radix .New ()
1070
+ }
1071
+ // Check if it is ineffectual.
1072
+ _ , _ , ok := xt .LongestPrefix (i )
1073
+ if ok {
1074
+ // Skip ineffectual wildcard ignore.
1075
+ continue
1076
+ }
1077
+ // Create the ignore prefix and insert in the radix tree.
1078
+ xt .Insert (i [:len (i )- len (wcIgnoreSuffix )], true )
1079
+ }
1080
+ }
1081
+
1082
+ return xt
1083
+ }
0 commit comments