-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathstrategy_deadcode.go
71 lines (62 loc) · 2.05 KB
/
strategy_deadcode.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
package engine
import (
"strconv"
"strings"
"github.com/360EntSecGroup-Skylar/goreporter/linters/deadcode"
"github.com/360EntSecGroup-Skylar/goreporter/utils"
)
type StrategyDeadCode struct {
Sync *Synchronizer `inject:""`
}
func (s *StrategyDeadCode) GetName() string {
return "Deadcode"
}
func (s *StrategyDeadCode) GetDescription() string {
return "All useless code, or never obsolete obsolete code."
}
func (s *StrategyDeadCode) GetWeight() float64 {
return 0.05
}
// linterDead provides a function that will scans all useless code, or never
// obsolete obsolete code.It will extract from the linter need to convert
// the data.The result will be saved in the r's attributes.
func (s *StrategyDeadCode) Compute(parameters StrategyParameter) (summaries *Summaries) {
summaries = NewSummaries()
deadcodes := deadcode.DeadCode(parameters.ProjectPath)
sumProcessNumber := int64(10)
processUnit := utils.GetProcessUnit(sumProcessNumber, len(deadcodes))
for _, simpleTip := range deadcodes {
deadCodeTips := strings.Split(simpleTip, ":")
if len(deadCodeTips) == 4 {
packageName := utils.PackageNameFromGoPath(deadCodeTips[0])
line, _ := strconv.Atoi(deadCodeTips[1])
erroru := Error{
LineNumber: line,
ErrorString: utils.AbsPath(deadCodeTips[0]) + ":" + strings.Join(deadCodeTips[1:], ":"),
}
summaries.Lock()
if summary, ok := summaries.Summaries[packageName]; ok {
summary.Errors = append(summary.Errors, erroru)
summaries.Summaries[packageName] = summary
} else {
summarie := Summary{
Name: utils.PackageAbsPathExceptSuffix(deadCodeTips[0]),
Errors: make([]Error, 0),
}
summarie.Errors = append(summarie.Errors, erroru)
summaries.Summaries[packageName] = summarie
}
summaries.Unlock()
}
if sumProcessNumber > 0 {
s.Sync.LintersProcessChans <- processUnit
sumProcessNumber = sumProcessNumber - processUnit
}
}
return
}
func (s *StrategyDeadCode) Percentage(summaries *Summaries) float64 {
summaries.Lock()
defer summaries.Unlock()
return utils.CountPercentage(len(summaries.Summaries))
}