Skip to content

Commit

Permalink
Merge pull request #5238 from daghack/print-lint-violations-refactor
Browse files Browse the repository at this point in the history
frontend: refactor lint rule check printing functionality
  • Loading branch information
tonistiigi authored Aug 15, 2024
2 parents 5e729c3 + 41d3195 commit bff1d81
Showing 1 changed file with 61 additions and 37 deletions.
98 changes: 61 additions & 37 deletions frontend/subrequests/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ type Warning struct {
Location pb.Location `json:"location,omitempty"`
}

func (w *Warning) PrintTo(wr io.Writer, sources []*pb.SourceInfo) error {
fmt.Fprintf(wr, "\nWARNING: %s", w.RuleName)
if w.URL != "" {
fmt.Fprintf(wr, " - %s", w.URL)
}
fmt.Fprintf(wr, "\n%s\n", w.Detail)

if w.Location.SourceIndex < 0 {
return nil
}
sourceInfo := sources[w.Location.SourceIndex]
source := errdefs.Source{
Info: sourceInfo,
Ranges: w.Location.Ranges,
}
return source.Print(wr)
}

type BuildError struct {
Message string `json:"message"`
Location pb.Location `json:"location"`
Expand Down Expand Up @@ -117,28 +135,7 @@ func (results *LintResults) ToResult() (*client.Result, error) {
return res, nil
}

func (results *LintResults) validateWarnings() error {
for _, warning := range results.Warnings {
if int(warning.Location.SourceIndex) >= len(results.Sources) {
return errors.Errorf("sourceIndex is out of range")
}
if warning.Location.SourceIndex > 0 {
warningSource := results.Sources[warning.Location.SourceIndex]
if warningSource == nil {
return errors.Errorf("sourceIndex points to nil source")
}
}
}
return nil
}

func PrintLintViolations(dt []byte, w io.Writer) error {
var results LintResults

if err := json.Unmarshal(dt, &results); err != nil {
return err
}

func (results *LintResults) PrintTo(w io.Writer) error {
if err := results.validateWarnings(); err != nil {
return err
}
Expand Down Expand Up @@ -169,21 +166,7 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
})

for _, warning := range results.Warnings {
fmt.Fprintf(w, "\nWARNING: %s", warning.RuleName)
if warning.URL != "" {
fmt.Fprintf(w, " - %s", warning.URL)
}
fmt.Fprintf(w, "\n%s\n", warning.Detail)

if warning.Location.SourceIndex < 0 {
continue
}
sourceInfo := results.Sources[warning.Location.SourceIndex]
source := errdefs.Source{
Info: sourceInfo,
Ranges: warning.Location.Ranges,
}
err := source.Print(w)
err := warning.PrintTo(w, results.Sources)
if err != nil {
return err
}
Expand All @@ -192,6 +175,47 @@ func PrintLintViolations(dt []byte, w io.Writer) error {
return nil
}

func (results *LintResults) PrintErrorTo(w io.Writer) {
// This prints out the error in LintResults to the writer in a format that
// is consistent with the warnings for easier downstream consumption.
if results.Error == nil {
return
}

fmt.Fprintln(w, results.Error.Message)
sourceInfo := results.Sources[results.Error.Location.SourceIndex]
source := errdefs.Source{
Info: sourceInfo,
Ranges: results.Error.Location.Ranges,
}
source.Print(w)
}

func (results *LintResults) validateWarnings() error {
for _, warning := range results.Warnings {
if int(warning.Location.SourceIndex) >= len(results.Sources) {
return errors.Errorf("sourceIndex is out of range")
}
if warning.Location.SourceIndex > 0 {
warningSource := results.Sources[warning.Location.SourceIndex]
if warningSource == nil {
return errors.Errorf("sourceIndex points to nil source")
}
}
}
return nil
}

func PrintLintViolations(dt []byte, w io.Writer) error {
var results LintResults

if err := json.Unmarshal(dt, &results); err != nil {
return err
}

return results.PrintTo(w)
}

func sourceInfoEqual(a, b *pb.SourceInfo) bool {
if a.Filename != b.Filename || a.Language != b.Language {
return false
Expand Down

0 comments on commit bff1d81

Please sign in to comment.