-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.go
88 lines (76 loc) · 1.78 KB
/
lib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package lib Golangci-lint: altered version of main.go
package lib
import (
"os"
"sort"
"github.com/golangci/dupl/job"
"github.com/golangci/dupl/printer"
"github.com/golangci/dupl/syntax"
)
func Run(files []string, threshold int) ([]printer.Issue, error) {
fchan := make(chan string, 1024)
go func() {
for _, f := range files {
fchan <- f
}
close(fchan)
}()
schan := job.Parse(fchan)
t, data, done := job.BuildTree(schan)
<-done
// finish stream
t.Update(&syntax.Node{Type: -1})
mchan := t.FindDuplOver(threshold)
duplChan := make(chan syntax.Match)
go func() {
for m := range mchan {
match := syntax.FindSyntaxUnits(*data, m, threshold)
if len(match.Frags) > 0 {
duplChan <- match
}
}
close(duplChan)
}()
return makeIssues(duplChan)
}
func makeIssues(duplChan <-chan syntax.Match) ([]printer.Issue, error) {
groups := make(map[string][][]*syntax.Node)
for dupl := range duplChan {
groups[dupl.Hash] = append(groups[dupl.Hash], dupl.Frags...)
}
keys := make([]string, 0, len(groups))
for k := range groups {
keys = append(keys, k)
}
sort.Strings(keys)
p := printer.NewIssuer(os.ReadFile)
var issues []printer.Issue
for _, k := range keys {
uniq := unique(groups[k])
if len(uniq) > 1 {
i, err := p.MakeIssues(uniq)
if err != nil {
return nil, err
}
issues = append(issues, i...)
}
}
return issues, nil
}
func unique(group [][]*syntax.Node) [][]*syntax.Node {
fileMap := make(map[string]map[int]struct{})
var newGroup [][]*syntax.Node
for _, seq := range group {
node := seq[0]
file, ok := fileMap[node.Filename]
if !ok {
file = make(map[int]struct{})
fileMap[node.Filename] = file
}
if _, ok := file[node.Pos]; !ok {
file[node.Pos] = struct{}{}
newGroup = append(newGroup, seq)
}
}
return newGroup
}