Skip to content

Commit

Permalink
refactor: display action results in message
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jul 20, 2024
1 parent 7f2c55e commit fea611f
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/vale/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func fix(args []string, flags *core.CLIFlags) error {
return err
}

resp, err := lint.ParseAlert(alert, cfg)
resp, err := check.ParseAlert(alert, cfg)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions internal/lint/action.go → internal/check/action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lint
package check

import (
"encoding/json"
Expand All @@ -13,7 +13,6 @@ import (
"github.com/d5/tengo/v2/stdlib"
"github.com/jdkato/twine/strcase"

"github.com/errata-ai/vale/v3/internal/check"
"github.com/errata-ai/vale/v3/internal/core"
)

Expand Down Expand Up @@ -43,7 +42,7 @@ func ParseAlert(s string, cfg *core.Config) (Solution, error) {
return Solution{}, err
}

suggestions, err := processAlert(body, cfg)
suggestions, err := FixAlert(body, cfg)
if err != nil {
resp.Error = err.Error()
}
Expand All @@ -52,7 +51,7 @@ func ParseAlert(s string, cfg *core.Config) (Solution, error) {
return resp, nil
}

func processAlert(alert core.Alert, cfg *core.Config) ([]string, error) {
func FixAlert(alert core.Alert, cfg *core.Config) ([]string, error) {
action := alert.Action.Name
if f, found := fixers[action]; found {
return f(alert, cfg)
Expand Down Expand Up @@ -118,7 +117,7 @@ func spelling(alert core.Alert, cfg *core.Config) ([]string, error) {
name := strings.Split(alert.Check, ".")
path := filepath.Join(cfg.StylesPath(), name[0], name[1]+".yml")

mgr, err := check.NewManager(cfg)
mgr, err := NewManager(cfg)
if err != nil {
return suggestions, err
}
Expand All @@ -130,7 +129,7 @@ func spelling(alert core.Alert, cfg *core.Config) ([]string, error) {
}
}

rule, ok := mgr.Rules()[alert.Check].(check.Spelling)
rule, ok := mgr.Rules()[alert.Check].(Spelling)
if !ok {
return suggestions, errors.New("unknown check")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/check/capitalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewCapitalization(cfg *core.Config, generic baseCheck, path string) (Capita
}

// Run checks the capitalization style of the provided text.
func (c Capitalization) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (c Capitalization) Run(blk nlp.Block, _ *core.File, cfg *core.Config) ([]core.Alert, error) {
alerts := []core.Alert{}

expected, matched := c.Check(blk.Text, c.exceptRe)
Expand All @@ -126,7 +126,7 @@ func (c Capitalization) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
}
pos := []int{0, nlp.StrLen(blk.Text)}

a, err := makeAlert(c.Definition, pos, blk.Text)
a, err := makeAlert(c.Definition, pos, blk.Text, cfg)
if err != nil {
return alerts, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/check/conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewConditional(cfg *core.Config, generic baseCheck, path string) (Condition
}

// Run evaluates the given conditional statement.
func (c Conditional) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
func (c Conditional) Run(blk nlp.Block, f *core.File, cfg *core.Config) ([]core.Alert, error) {
alerts := []core.Alert{}

txt := blk.Text
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c Conditional) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
if !core.StringInSlice(s, f.Sequences) && !isMatch(c.exceptRe, s) {
// If we've found one (e.g., "WHO") and we haven't marked it as
// being defined previously, send an Alert.
a, erra := makeAlert(c.Definition, loc, txt)
a, erra := makeAlert(c.Definition, loc, txt, cfg)
if erra != nil {
return alerts, erra
}
Expand Down
4 changes: 2 additions & 2 deletions internal/check/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewConsistency(cfg *core.Config, generic baseCheck, path string) (Consisten
}

// Run looks for inconsistent use of a user-defined regex.
func (o Consistency) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
func (o Consistency) Run(blk nlp.Block, f *core.File, cfg *core.Config) ([]core.Alert, error) {
alerts := []core.Alert{}

loc := []int{}
Expand All @@ -100,7 +100,7 @@ func (o Consistency) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
if matches != nil && core.AllStringsInSlice(s.subs, f.Sequences) {
o.Name = o.Extends

a, err := makeAlert(o.Definition, loc, txt)
a, err := makeAlert(o.Definition, loc, txt, cfg)
if err != nil {
return alerts, err
}
Expand Down
23 changes: 20 additions & 3 deletions internal/check/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type FilterEnv struct {

// Rule represents in individual writing construct to enforce.
type Rule interface {
Run(blk nlp.Block, file *core.File) ([]core.Alert, error)
Run(blk nlp.Block, file *core.File, cfg *core.Config) ([]core.Alert, error)
Fields() Definition
Pattern() string
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func re2Loc(s string, loc []int) (string, error) {
return string(converted[loc[0]:loc[1]]), nil
}

func makeAlert(chk Definition, loc []int, txt string) (core.Alert, error) {
func makeAlert(chk Definition, loc []int, txt string, cfg *core.Config) (core.Alert, error) {
match, err := re2Loc(txt, loc)
if err != nil {
return core.Alert{}, err
Expand All @@ -191,7 +191,24 @@ func makeAlert(chk Definition, loc []int, txt string) (core.Alert, error) {
a := core.Alert{
Check: chk.Name, Severity: chk.Level, Span: loc, Link: chk.Link,
Match: match, Action: chk.Action}
a.Message, a.Description = formatMessages(chk.Message, chk.Description, match)

if chk.Action.Name != "" {
repl := match

fixed, fixError := FixAlert(a, cfg)
if fixError != nil {
return core.Alert{}, fixError
}

if len(fixed) == 1 {
repl = fixed[0]
} else if len(fixed) > 1 {
repl = core.ToSentence(fixed, "or")
}
a.Message, a.Description = formatMessages(chk.Message, chk.Description, match, repl)
} else {
a.Message, a.Description = formatMessages(chk.Message, chk.Description, match)
}

return a, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/check/existence.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewExistence(cfg *core.Config, generic baseCheck, path string) (Existence,
// This is simplest of the available extension points: it looks for any matches
// of its internal `pattern` (calculated from `NewExistence`) against the
// provided text.
func (e Existence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (e Existence) Run(blk nlp.Block, _ *core.File, cfg *core.Config) ([]core.Alert, error) {
alerts := []core.Alert{}

for _, loc := range e.pattern.FindAllStringIndex(blk.Text, -1) {
Expand All @@ -85,7 +85,7 @@ func (e Existence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {

observed := strings.TrimSpace(converted)
if !isMatch(e.exceptRe, observed) {
a, erra := makeAlert(e.Definition, loc, blk.Text)
a, erra := makeAlert(e.Definition, loc, blk.Text, cfg)
if erra != nil {
return alerts, erra
}
Expand Down
4 changes: 2 additions & 2 deletions internal/check/existence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestExistence(t *testing.T) {
t.Fatal(err)
}

alerts, _ := rule.Run(nlp.NewBlock("", "This is a test.", ""), file)
alerts, _ := rule.Run(nlp.NewBlock("", "This is a test.", ""), file, cfg)
if len(alerts) != 1 {
t.Errorf("expected one alert, not %v", alerts)
}
Expand Down Expand Up @@ -70,6 +70,6 @@ func FuzzExistence(f *testing.F) {

f.Add("hello")
f.Fuzz(func(t *testing.T, s string) {
_, _ = rule.Run(nlp.NewBlock("", s, ""), file)
_, _ = rule.Run(nlp.NewBlock("", s, ""), file, cfg)
})
}
2 changes: 1 addition & 1 deletion internal/check/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewMetric(_ *core.Config, generic baseCheck, path string) (Metric, error) {
}

// Run calculates the readability level of the given text.
func (o Metric) Run(_ nlp.Block, f *core.File) ([]core.Alert, error) {
func (o Metric) Run(_ nlp.Block, f *core.File, _ *core.Config) ([]core.Alert, error) {
alerts := []core.Alert{}
ctx := context.Background()

Expand Down
4 changes: 2 additions & 2 deletions internal/check/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewOccurrence(_ *core.Config, generic baseCheck, path string) (Occurrence,

// Run checks the number of occurrences of a user-defined regex against a
// certain threshold.
func (o Occurrence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (o Occurrence) Run(blk nlp.Block, _ *core.File, cfg *core.Config) ([]core.Alert, error) {
var a core.Alert
var err error
var alerts []core.Alert
Expand Down Expand Up @@ -99,7 +99,7 @@ func (o Occurrence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
return alerts, nil
}

a, err = makeAlert(o.Definition, span, txt)
a, err = makeAlert(o.Definition, span, txt, cfg)
if err != nil {
return alerts, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/check/readability.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewReadability(_ *core.Config, generic baseCheck, path string) (Readability
}

// Run calculates the readability level of the given text.
func (o Readability) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (o Readability) Run(blk nlp.Block, _ *core.File, _ *core.Config) ([]core.Alert, error) {
var grade float64
var alerts []core.Alert

Expand Down
4 changes: 2 additions & 2 deletions internal/check/repetition.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewRepetition(cfg *core.Config, generic baseCheck, path string) (Repetition
// Run executes the `repetition`-based rule.
//
// The rule looks for repeated matches of its regex -- such as "this this".
func (o Repetition) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (o Repetition) Run(blk nlp.Block, _ *core.File, cfg *core.Config) ([]core.Alert, error) {
var curr, prev string
var hit bool
var ploc []int
Expand Down Expand Up @@ -98,7 +98,7 @@ func (o Repetition) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
if !strings.Contains(converted, "\n") && !isMatch(o.exceptRe, converted) {
floc := []int{ploc[0], loc[1]}

a, erra := makeAlert(o.Definition, floc, txt)
a, erra := makeAlert(o.Definition, floc, txt, cfg)
if erra != nil {
return alerts, erra
}
Expand Down
2 changes: 1 addition & 1 deletion internal/check/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewScript(cfg *core.Config, generic baseCheck, path string) (Script, error)
}

// Run executes the given script and returns its Alerts.
func (s Script) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (s Script) Run(blk nlp.Block, _ *core.File, _ *core.Config) ([]core.Alert, error) {
var alerts []core.Alert

script := tengo.NewScript([]byte(s.Script))
Expand Down
2 changes: 1 addition & 1 deletion internal/check/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func stepsToString(steps []string) string {
}

// Run looks for the user-defined sequence of tokens.
func (s Sequence) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
func (s Sequence) Run(blk nlp.Block, f *core.File, _ *core.Config) ([]core.Alert, error) {
var alerts []core.Alert
var offset []string

Expand Down
2 changes: 1 addition & 1 deletion internal/check/spelling.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func NewSpelling(cfg *core.Config, generic baseCheck, path string) (Spelling, er
}

// Run performs spell-checking on the provided text.
func (s Spelling) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (s Spelling) Run(blk nlp.Block, _ *core.File, _ *core.Config) ([]core.Alert, error) {
var alerts []core.Alert

txt := blk.Text
Expand Down
4 changes: 2 additions & 2 deletions internal/check/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewSubstitution(cfg *core.Config, generic baseCheck, path string) (Substitu
// Run executes the `substitution`-based rule.
//
// The rule looks for one pattern and then suggests a replacement.
func (s Substitution) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
func (s Substitution) Run(blk nlp.Block, _ *core.File, cfg *core.Config) ([]core.Alert, error) {
var alerts []core.Alert

txt := blk.Text
Expand Down Expand Up @@ -139,7 +139,7 @@ func (s Substitution) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
s.Message = convertMessage(s.Message)
}

a, aerr := makeAlert(s.Definition, loc, txt)
a, aerr := makeAlert(s.Definition, loc, txt, cfg)
if aerr != nil {
return alerts, aerr
}
Expand Down
6 changes: 3 additions & 3 deletions internal/check/substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestIsDeterministic(t *testing.T) {
t.Fatal(err)
}

actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{})
actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{}, &core.Config{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestRegex(t *testing.T) {
t.Fatal(err)
}

actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{})
actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{}, &core.Config{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestRegexEscapedParens(t *testing.T) {
t.Fatal(err)
}

actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{})
actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{}, &core.Config{})
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (l *Linter) lintBlock(f *core.File, blk nlp.Block, lines, pad int, lookup b

info := chk.Fields()

alerts, err := chk.Run(blk, f)
alerts, err := chk.Run(blk, f, l.Manager.Config)
if err != nil {
return err
}
Expand Down

0 comments on commit fea611f

Please sign in to comment.