1
1
package difflint
2
2
3
3
import (
4
+ "fmt"
4
5
"io"
5
- "log"
6
6
"path/filepath"
7
7
"strings"
8
8
@@ -87,10 +87,39 @@ type UnsatisfiedRule struct {
87
87
UnsatisfiedTargets map [int ]struct {}
88
88
}
89
89
90
+ type UnsatisfiedRules []UnsatisfiedRule
91
+
92
+ // String returns a string representation of the unsatisfied rules.
93
+ func (r * UnsatisfiedRules ) String () string {
94
+ var b strings.Builder
95
+ for _ , rule := range * r {
96
+ b .WriteString ("rule (" )
97
+ b .WriteString (rule .Rule .Hunk .File )
98
+ b .WriteString (":" )
99
+ b .WriteString (fmt .Sprintf ("%d" , rule .Rule .Hunk .Range .Start ))
100
+ b .WriteString ("," )
101
+ b .WriteString (rule .Rule .Hunk .File )
102
+ b .WriteString (":" )
103
+ b .WriteString (fmt .Sprintf ("%d" , rule .Rule .Hunk .Range .End ))
104
+ b .WriteString (") not satisfied for targets:\n " )
105
+ for i , target := range rule .Targets {
106
+ if _ , ok := rule .UnsatisfiedTargets [i ]; ! ok {
107
+ continue
108
+ }
109
+
110
+ key := TargetKey (rule .Rule .Hunk .File , target )
111
+ b .WriteString (" " )
112
+ b .WriteString (key )
113
+ b .WriteString ("\n " )
114
+ }
115
+ }
116
+ return b .String ()
117
+ }
118
+
90
119
// Result of a linting operation.
91
120
type LintResult struct {
92
121
// List of rules that were not satisfied.
93
- UnsatisfiedRules [] UnsatisfiedRule
122
+ UnsatisfiedRules UnsatisfiedRules
94
123
}
95
124
96
125
// Lint lints the given hunks against the given rules and returns the result.
@@ -147,8 +176,8 @@ func isRelativeToCurrentDirectory(path string) bool {
147
176
}
148
177
149
178
// Check returns the list of unsatisfied rules for the given map of rules.
150
- func Check (rulesMap map [string ][]Rule ) ([] UnsatisfiedRule , error ) {
151
- var unsatisfiedRules [] UnsatisfiedRule
179
+ func Check (rulesMap map [string ][]Rule ) (UnsatisfiedRules , error ) {
180
+ var unsatisfiedRules UnsatisfiedRules
152
181
for pathnameA , rulesA := range rulesMap {
153
182
outer:
154
183
for i , ruleA := range rulesA {
@@ -193,7 +222,7 @@ func Check(rulesMap map[string][]Rule) ([]UnsatisfiedRule, error) {
193
222
}
194
223
195
224
// Entrypoint for the difflint command.
196
- func Do (r io.Reader , include , exclude []string , extMapPath string ) error {
225
+ func Do (r io.Reader , include , exclude []string , extMapPath string ) ( UnsatisfiedRules , error ) {
197
226
// Parse options.
198
227
extMap := NewExtMap (extMapPath )
199
228
@@ -207,38 +236,29 @@ func Do(r io.Reader, include, exclude []string, extMapPath string) error {
207
236
FileExtMap : extMap .FileExtMap ,
208
237
})
209
238
if err != nil {
210
- return errors .Wrap (err , "failed to lint hunks" )
239
+ return nil , errors .Wrap (err , "failed to lint hunks" )
211
240
}
212
241
213
242
// If there are no unsatisfied rules, return nil.
214
243
if len (result .UnsatisfiedRules ) == 0 {
215
- return nil
244
+ return nil , nil
216
245
}
217
246
218
247
// Print the unsatisfied rules.
248
+ var included bool
219
249
for _ , rule := range result .UnsatisfiedRules {
220
250
// Skip if the rule is not intended to be included in the output.
221
- if ok , err := Include (rule .Hunk .File , include , exclude ); err != nil {
222
- return errors .Wrap (err , "failed to check if file is included" )
223
- } else if ! ok {
224
- continue
251
+ included , err = Include (rule .Hunk .File , include , exclude )
252
+ if err != nil {
253
+ return nil , errors .Wrap (err , "failed to check if file is included" )
225
254
}
226
255
227
- // Print the unsatisfied rule.
228
- log .Printf ("Rule (%s:%d,%s:%d) unsatisfied" , rule .Rule .Hunk .File , rule .Rule .Hunk .Range .Start , rule .Rule .Hunk .File , rule .Rule .Hunk .Range .End )
229
-
230
- // Print the unsatisfied target keys.
231
- for i , target := range rule .Targets {
232
- if _ , ok := rule .UnsatisfiedTargets [i ]; ! ok {
233
- continue
234
- }
235
-
236
- key := TargetKey (rule .Hunk .File , target )
237
- log .Printf (" %s" , key )
256
+ if ! included {
257
+ continue
238
258
}
239
259
}
240
260
241
- return nil
261
+ return result . UnsatisfiedRules , nil
242
262
}
243
263
244
264
// ParseHunks parses the input diff and returns the extracted file paths along
0 commit comments