-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.go
127 lines (104 loc) · 3.17 KB
/
command.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package tmplextractors
import (
"context"
"strings"
"text/template/parse"
"time"
log "github.com/sirupsen/logrus"
"github.com/vorlif/xspreak/extract/extractors"
"github.com/vorlif/xspreak/result"
"github.com/vorlif/xspreak/tmpl"
"github.com/vorlif/xspreak/util"
)
type commandExtractor struct{}
func NewCommandExtractor() extractors.Extractor {
return &commandExtractor{}
}
func (c *commandExtractor) Run(_ context.Context, extractCtx *extractors.Context) ([]result.Issue, error) {
util.TrackTime(time.Now(), "extract templates")
var issues []result.Issue
if len(extractCtx.Config.Keywords) == 0 {
log.Debug("Skip template extraction, no keywords present")
return issues, nil
}
for _, template := range extractCtx.Templates {
template.ExtractComments()
template.Inspector.WithStack([]parse.Node{&parse.PipeNode{}}, func(n parse.Node, push bool, stack []parse.Node) (proceed bool) {
proceed = false
if !push {
return
}
pipe := n.(*parse.PipeNode)
if pipe.IsAssign {
return
}
for _, cmd := range pipe.Cmds {
iss := extractIssue(cmd, extractCtx, template)
if iss == nil {
continue
}
iss.Flags = append(iss.Flags, "go-template")
issues = append(issues, *iss)
}
return
})
}
return issues, nil
}
func (c *commandExtractor) Name() string {
return "tmpl_command"
}
func extractIssue(cmd *parse.CommandNode, extractCtx *extractors.Context, template *tmpl.Template) *result.Issue {
if cmd == nil {
return nil
}
raw := cmd.String()
for _, keyword := range extractCtx.Config.Keywords {
if !strings.HasPrefix(raw, keyword.Name+" ") {
continue
}
if keyword.MaxPosition() >= len(cmd.Args)-1 { // The first index contains the keyword itself
log.Warnf("Template keyword found but not enough arguments available: %s %s", template.Position(cmd.Pos), raw)
continue
}
return extractArgs(cmd.Args[1:], keyword, template)
}
return nil
}
func extractArgs(args []parse.Node, keyword *tmpl.Keyword, template *tmpl.Template) *result.Issue {
iss := &result.Issue{}
if stringNode, ok := args[keyword.SingularPos].(*parse.StringNode); ok {
iss.MsgID = stringNode.Text
iss.IDToken = keyword.IDToken
iss.Comments = template.GetComments(args[keyword.SingularPos].Position())
iss.Pos = template.Position(args[keyword.SingularPos].Position())
} else {
log.Warnf("Template keyword is not passed a string: %s", args[keyword.SingularPos])
return nil
}
if keyword.PluralPos >= 0 {
if stringNode, ok := args[keyword.PluralPos].(*parse.StringNode); ok {
iss.PluralID = stringNode.Text
} else {
log.Warnf("Template keyword is not passed a string: %s", args[keyword.PluralPos])
return nil
}
}
if keyword.ContextPos >= 0 {
if stringNode, ok := args[keyword.ContextPos].(*parse.StringNode); ok {
iss.Context = stringNode.Text
} else {
log.Warnf("Template keyword is not passed a string: %s", args[keyword.ContextPos])
return nil
}
}
if keyword.DomainPos >= 0 {
if stringNode, ok := args[keyword.DomainPos].(*parse.StringNode); ok {
iss.Domain = stringNode.Text
} else {
log.Warnf("Template keyword is not passed a string: %s", args[keyword.DomainPos])
return nil
}
}
return iss
}