Skip to content

Commit bff1d81

Browse files
authored
Merge pull request moby#5238 from daghack/print-lint-violations-refactor
frontend: refactor lint rule check printing functionality
2 parents 5e729c3 + 41d3195 commit bff1d81

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

frontend/subrequests/lint/lint.go

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ type Warning struct {
3939
Location pb.Location `json:"location,omitempty"`
4040
}
4141

42+
func (w *Warning) PrintTo(wr io.Writer, sources []*pb.SourceInfo) error {
43+
fmt.Fprintf(wr, "\nWARNING: %s", w.RuleName)
44+
if w.URL != "" {
45+
fmt.Fprintf(wr, " - %s", w.URL)
46+
}
47+
fmt.Fprintf(wr, "\n%s\n", w.Detail)
48+
49+
if w.Location.SourceIndex < 0 {
50+
return nil
51+
}
52+
sourceInfo := sources[w.Location.SourceIndex]
53+
source := errdefs.Source{
54+
Info: sourceInfo,
55+
Ranges: w.Location.Ranges,
56+
}
57+
return source.Print(wr)
58+
}
59+
4260
type BuildError struct {
4361
Message string `json:"message"`
4462
Location pb.Location `json:"location"`
@@ -117,28 +135,7 @@ func (results *LintResults) ToResult() (*client.Result, error) {
117135
return res, nil
118136
}
119137

120-
func (results *LintResults) validateWarnings() error {
121-
for _, warning := range results.Warnings {
122-
if int(warning.Location.SourceIndex) >= len(results.Sources) {
123-
return errors.Errorf("sourceIndex is out of range")
124-
}
125-
if warning.Location.SourceIndex > 0 {
126-
warningSource := results.Sources[warning.Location.SourceIndex]
127-
if warningSource == nil {
128-
return errors.Errorf("sourceIndex points to nil source")
129-
}
130-
}
131-
}
132-
return nil
133-
}
134-
135-
func PrintLintViolations(dt []byte, w io.Writer) error {
136-
var results LintResults
137-
138-
if err := json.Unmarshal(dt, &results); err != nil {
139-
return err
140-
}
141-
138+
func (results *LintResults) PrintTo(w io.Writer) error {
142139
if err := results.validateWarnings(); err != nil {
143140
return err
144141
}
@@ -169,21 +166,7 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
169166
})
170167

171168
for _, warning := range results.Warnings {
172-
fmt.Fprintf(w, "\nWARNING: %s", warning.RuleName)
173-
if warning.URL != "" {
174-
fmt.Fprintf(w, " - %s", warning.URL)
175-
}
176-
fmt.Fprintf(w, "\n%s\n", warning.Detail)
177-
178-
if warning.Location.SourceIndex < 0 {
179-
continue
180-
}
181-
sourceInfo := results.Sources[warning.Location.SourceIndex]
182-
source := errdefs.Source{
183-
Info: sourceInfo,
184-
Ranges: warning.Location.Ranges,
185-
}
186-
err := source.Print(w)
169+
err := warning.PrintTo(w, results.Sources)
187170
if err != nil {
188171
return err
189172
}
@@ -192,6 +175,47 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
192175
return nil
193176
}
194177

178+
func (results *LintResults) PrintErrorTo(w io.Writer) {
179+
// This prints out the error in LintResults to the writer in a format that
180+
// is consistent with the warnings for easier downstream consumption.
181+
if results.Error == nil {
182+
return
183+
}
184+
185+
fmt.Fprintln(w, results.Error.Message)
186+
sourceInfo := results.Sources[results.Error.Location.SourceIndex]
187+
source := errdefs.Source{
188+
Info: sourceInfo,
189+
Ranges: results.Error.Location.Ranges,
190+
}
191+
source.Print(w)
192+
}
193+
194+
func (results *LintResults) validateWarnings() error {
195+
for _, warning := range results.Warnings {
196+
if int(warning.Location.SourceIndex) >= len(results.Sources) {
197+
return errors.Errorf("sourceIndex is out of range")
198+
}
199+
if warning.Location.SourceIndex > 0 {
200+
warningSource := results.Sources[warning.Location.SourceIndex]
201+
if warningSource == nil {
202+
return errors.Errorf("sourceIndex points to nil source")
203+
}
204+
}
205+
}
206+
return nil
207+
}
208+
209+
func PrintLintViolations(dt []byte, w io.Writer) error {
210+
var results LintResults
211+
212+
if err := json.Unmarshal(dt, &results); err != nil {
213+
return err
214+
}
215+
216+
return results.PrintTo(w)
217+
}
218+
195219
func sourceInfoEqual(a, b *pb.SourceInfo) bool {
196220
if a.Filename != b.Filename || a.Language != b.Language {
197221
return false

0 commit comments

Comments
 (0)