-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
112 lines (90 loc) · 2.75 KB
/
rules.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
package difflint
import (
"log"
"os"
"github.com/pkg/errors"
)
// Target represents a file or range of code that must be present in the diff
// if a diff hunk is present.
type Target struct {
// File specifier expected to contain a diff hunk.
File *string
// ID is the ID of the range of code in which a diff hunk intersects.
ID *string
}
// A rule says that file or range of code must be present in the diff if another range is present.
type Rule struct {
// Hunk is the diff hunk that must be present in the diff.
Hunk Hunk
// Targets are the files or ranges of code that must be present in the diff if the hunk is present.
Targets []Target
// Present is true if the change is present in the diff from which the rules were parsed.
Present bool
// ID is an optional, unique identifier for the rule.
ID *string
}
// RulesMapFromHunks parses rules from the given hunks by file name and
// returns the map of rules and the set of all the target keys that are present.
func RulesMapFromHunks(hunks []Hunk, options LintOptions) (map[string][]Rule, map[string]struct{}, error) {
targetsMap := make(map[string]struct{}, len(hunks))
rangesMap := make(map[string][]Range, len(hunks))
for _, hunk := range hunks {
targetsMap[TargetKey(hunk.File, Target{})] = struct{}{}
if _, ok := rangesMap[hunk.File]; ok {
rangesMap[hunk.File] = append(rangesMap[hunk.File], hunk.Range)
continue
}
rangesMap[hunk.File] = []Range{hunk.Range}
}
rulesMap := make(map[string][]Rule, len(hunks))
err := Walk(".", nil, nil, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
f, err := os.Open(file)
if err != nil {
return errors.Wrapf(err, "failed to open file %s", file)
}
defer f.Close()
templates, err := options.TemplatesFromFile(file)
if err != nil {
return errors.Wrapf(err, "failed to parse templates for file %s", file)
}
tokens, err := lex(f, lexOptions{file, templates})
if err != nil {
return errors.Wrapf(err, "failed to lex file %s", file)
}
rules, err := parseRules(file, tokens, rangesMap[file])
if err != nil {
return errors.Wrapf(err, "failed to parse rules for file %s", file)
}
log.Printf("parsed %d rules for file %s", len(rules), file)
for _, rule := range rules {
if rule.Hunk.File != file {
continue
}
ranges, ok := rangesMap[file]
if !ok {
continue
}
for _, rng := range ranges {
if !Intersects(rule.Hunk.Range, rng) {
continue
}
key := TargetKey(file, Target{
File: &rule.Hunk.File,
ID: rule.ID,
})
targetsMap[key] = struct{}{}
}
}
if len(rules) > 0 {
rulesMap[file] = rules
}
return nil
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to walk files")
}
return rulesMap, targetsMap, nil
}