Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions buildifier/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Config struct {
Mode string `json:"mode,omitempty"`
// DiffMode is an alias for
DiffMode bool `json:"diffMode,omitempty"`
// Lint determines the lint mode: off, warn, or fix (default off)
// Lint determines the lint mode: off, warn, suggest, or fix (default off)
Lint string `json:"lint,omitempty"`
// Warnings is a comma-separated list of warning identifiers used in the lint mode or "all"
Warnings string `json:"warnings,omitempty"`
Expand Down Expand Up @@ -201,7 +201,7 @@ func (c *Config) Validate(args []string) error {
return err
}

if err := ValidateModes(&c.Mode, &c.Lint, &c.DiffMode); err != nil {
if err := ValidateModes(&c.Mode, &c.Lint, &c.DiffMode, &c.Format); err != nil {
return err
}

Expand Down
10 changes: 7 additions & 3 deletions buildifier/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func isRecognizedMode(validModes []string, mode string) bool {
}

// ValidateModes validates flags --mode, --lint, and -d
func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) error {
func ValidateModes(mode, lint *string, dflag *bool, format *string) error {
if *dflag {
if *mode != "" {
return fmt.Errorf("cannot specify both -d and -mode flags")
Expand All @@ -70,7 +70,6 @@ func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) e

// Check mode.
validModes := []string{"check", "diff", "fix", "print_if_changed"}
validModes = append(validModes, additionalModes...)

if *mode == "" {
*mode = "fix"
Expand All @@ -91,8 +90,13 @@ func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) e
return fmt.Errorf("--lint=fix is only compatible with --mode=fix")
}

case "suggest":
if *format != "json" {
return fmt.Errorf("--lint=suggest is only compatible with --format=json")
}

default:
return fmt.Errorf("unrecognized lint mode %s; valid modes are warn and fix", *lint)
return fmt.Errorf("unrecognized lint mode %s; valid modes are warn, suggest, and fix", *lint)
}

return nil
Expand Down
36 changes: 28 additions & 8 deletions buildifier/utils/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package utils
import (
"encoding/json"
"fmt"
"strings"

"github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/buildtools/warn"
"strings"
)

// Diagnostics contains diagnostic information returned by formatter and linter
Expand Down Expand Up @@ -74,20 +75,27 @@ type FileDiagnostics struct {
}

type warning struct {
Start position `json:"start"`
End position `json:"end"`
Category string `json:"category"`
Actionable bool `json:"actionable"`
AutoFixable bool `json:"autoFixable"`
Message string `json:"message"`
URL string `json:"url"`
Start position `json:"start"`
End position `json:"end"`
Category string `json:"category"`
Actionable bool `json:"actionable"`
AutoFixable bool `json:"autoFixable"`
Message string `json:"message"`
URL string `json:"url"`
Replacement *replacement `json:"replacement,omitempty"` // Optional replacement
}

type position struct {
Line int `json:"line"`
Column int `json:"column"`
}

type replacement struct {
Start int `json:"start"` // Start offset in bytes
End int `json:"end"` // End offset in bytes, may be equal to Start for insertions
Content string `json:"content"` // Replacement content
}

// NewDiagnostics returns a new Diagnostics object
func NewDiagnostics(fileDiagnostics ...*FileDiagnostics) *Diagnostics {
diagnostics := &Diagnostics{
Expand Down Expand Up @@ -121,6 +129,7 @@ func NewFileDiagnostics(filename string, warnings []*warn.Finding) *FileDiagnost
AutoFixable: w.AutoFixable,
Message: w.Message,
URL: w.URL,
Replacement: makeReplacement(w.Replacement),
})
}

Expand All @@ -147,3 +156,14 @@ func makePosition(p build.Position) position {
Column: p.LineRune,
}
}

func makeReplacement(r *warn.Replacement) *replacement {
if r == nil {
return nil
}
return &replacement{
Start: r.Start,
End: r.End,
Content: r.Content,
}
}
2 changes: 2 additions & 0 deletions buildifier/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func Lint(f *build.File, lint string, warningsList *[]string, verbose bool) []*w
switch lint {
case "warn":
return warn.FileWarnings(f, *warningsList, nil, warn.ModeWarn, fileReader)
case "suggest":
return warn.FileWarnings(f, *warningsList, nil, warn.ModeSuggest, fileReader)
case "fix":
warn.FixWarnings(f, *warningsList, verbose, fileReader)
}
Expand Down