github.com/errata-ai/vale/v3@v3.4.2/internal/core/alert.go (about)

     1  package core
     2  
     3  // AlertLevels holds the possible values for "level" in an external rule.
     4  var AlertLevels = []string{"suggestion", "warning", "error"}
     5  
     6  // LevelToInt allows us to easily compare levels in lint.go.
     7  var LevelToInt = map[string]int{
     8  	"suggestion": 0,
     9  	"warning":    1,
    10  	"error":      2,
    11  }
    12  
    13  // An Action represents a possible solution to an Alert.
    14  type Action struct {
    15  	Name   string   // the name of the action -- e.g, 'replace'
    16  	Params []string // a slice of parameters for the given action
    17  }
    18  
    19  // An Alert represents a potential error in prose.
    20  type Alert struct {
    21  	Action      Action   // a possible solution
    22  	Span        []int    // the [begin, end] location within a line
    23  	Offset      []string `json:"-"` // tokens to ignore before this match
    24  	Check       string   // the name of the check
    25  	Description string   // why `Message` is meaningful
    26  	Link        string   // reference material
    27  	Message     string   // the output message
    28  	Severity    string   // 'suggestion', 'warning', or 'error'
    29  	Match       string   // the actual matched text
    30  	Line        int      // the source line
    31  	Limit       int      `json:"-"` // the max times to report
    32  	Hide        bool     `json:"-"` // should we hide this alert?
    33  }
    34  
    35  // FormatAlert ensures that all required fields have data.
    36  func FormatAlert(a *Alert, limit int, level, name string) {
    37  	if a.Severity == "" {
    38  		a.Severity = level
    39  	}
    40  	if a.Check == "" {
    41  		a.Check = name
    42  	}
    43  	a.Limit = limit
    44  	a.Message = WhitespaceToSpace(a.Message)
    45  }
    46  
    47  // ByPosition sorts Alerts by line and column.
    48  type ByPosition []Alert
    49  
    50  func (a ByPosition) Len() int      { return len(a) }
    51  func (a ByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    52  func (a ByPosition) Less(i, j int) bool {
    53  	ai, aj := a[i], a[j]
    54  
    55  	if ai.Line != aj.Line {
    56  		return ai.Line < aj.Line
    57  	}
    58  	return ai.Span[0] < aj.Span[0]
    59  }
    60  
    61  // ByName sorts Files by their path.
    62  type ByName []*File
    63  
    64  func (a ByName) Len() int      { return len(a) }
    65  func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    66  func (a ByName) Less(i, j int) bool {
    67  	ai, aj := a[i], a[j]
    68  	return ai.Path < aj.Path
    69  }