-
Notifications
You must be signed in to change notification settings - Fork 163
/
rule_if_cond.go
50 lines (44 loc) · 1.13 KB
/
rule_if_cond.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
package actionlint
import (
"strings"
)
// RuleIfCond is a rule to check if: conditions.
type RuleIfCond struct {
RuleBase
}
// NewRuleIfCond creates new RuleIfCond instance.
func NewRuleIfCond() *RuleIfCond {
return &RuleIfCond{
RuleBase: RuleBase{
name: "if-cond",
desc: "Checks for if: conditions which are always true/false",
},
}
}
// VisitStep is callback when visiting Step node.
func (rule *RuleIfCond) VisitStep(n *Step) error {
rule.checkIfCond(n.If)
return nil
}
// VisitJobPre is callback when visiting Job node before visiting its children.
func (rule *RuleIfCond) VisitJobPre(n *Job) error {
rule.checkIfCond(n.If)
return nil
}
func (rule *RuleIfCond) checkIfCond(n *String) {
if n == nil {
return
}
if !n.ContainsExpression() {
return
}
// Check number of ${{ }} for conditions like `${{ false }} || ${{ true }}` which are always evaluated to true
if strings.HasPrefix(n.Value, "${{") && strings.HasSuffix(n.Value, "}}") && strings.Count(n.Value, "${{") == 1 {
return
}
rule.Errorf(
n.Pos,
"if: condition %q is always evaluated to true because extra characters are around ${{ }}",
n.Value,
)
}