Skip to content

Commit

Permalink
chore: get check passing new CI config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Aug 28, 2023
1 parent 2d1dc3b commit 3595145
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 76 deletions.
10 changes: 5 additions & 5 deletions internal/check/capitalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ func NewCapitalization(cfg *core.Config, generic baseCheck, path string) (Capita
} else if f, ok := varToFunc[rule.Match]; ok {
rule.Check = f
} else {
re, err := regexp2.CompileStd(rule.Match)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
re2, errc := regexp2.CompileStd(rule.Match)
if errc != nil {
return rule, core.NewE201FromPosition(errc.Error(), path, 1)
}
rule.Check = func(s string, r *regexp2.Regexp) bool {
return re.MatchStringStd(s) || isMatch(r, s)
return re2.MatchStringStd(s) || isMatch(r, s)
}
}

return rule, nil
}

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

txt := blk.Text
Expand Down
6 changes: 3 additions & 3 deletions internal/check/conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ 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, err := makeAlert(c.Definition, loc, txt)
if err != nil {
return alerts, err
a, erra := makeAlert(c.Definition, loc, txt)
if erra != nil {
return alerts, erra
}
alerts = append(alerts, a)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/check/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewConsistency(cfg *core.Config, generic baseCheck, path string) (Consisten
var chkRE string

rule := Consistency{}
name := generic["name"].(string)
name, _ := generic["name"].(string)

err := mapstructure.WeakDecode(generic, &rule)
if err != nil {
Expand Down Expand Up @@ -63,9 +63,9 @@ func NewConsistency(cfg *core.Config, generic baseCheck, path string) (Consisten
chkRE = fmt.Sprintf("(?P<%s>%s)|(?P<%s>%s)", subs[0], v1, subs[1], v2)
chkRE = fmt.Sprintf(regex, chkRE)

re, err := regexp2.CompileStd(chkRE)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
re, errc := regexp2.CompileStd(chkRE)
if errc != nil {
return rule, core.NewE201FromPosition(errc.Error(), path, 1)
}

rule.Extends = name
Expand Down
20 changes: 10 additions & 10 deletions internal/check/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ const (
ignoreCase = `(?i)`
wordTemplate = `(?m)\b(?:%s)\b`
nonwordTemplate = `(?m)(?:%s)`
tokenTemplate = `^(?:%s)$`
tokenTemplate = `^(?:%s)$` //nolint:gosec
)

type baseCheck map[string]interface{}

func buildRule(cfg *core.Config, generic baseCheck) (Rule, error) {
path, ok := generic["path"].(string)
if !ok {
msg := fmt.Errorf("'%v' is not valid .", generic)
msg := fmt.Errorf("'%v' is not valid", generic)
return Existence{}, core.NewE100("buildRule: path", msg)
}

Expand Down Expand Up @@ -202,13 +202,13 @@ func parse(file []byte, path string) (map[string]interface{}, error) {
r := regexp.MustCompile(`yaml: line (\d+): (.+)`)
if r.MatchString(err.Error()) {
groups := r.FindStringSubmatch(err.Error())
i, err := strconv.Atoi(groups[1])
if err != nil {
return generic, core.NewE100("addCheck/Atoi", err)
i, erri := strconv.Atoi(groups[1])
if erri != nil {
return generic, core.NewE100("addCheck/Atoi", erri)
}
return generic, core.NewE201FromPosition(groups[2], path, i)
}
} else if err := validateDefinition(generic, path); err != nil {
} else if err = validateDefinition(generic, path); err != nil {
return generic, err
}

Expand All @@ -222,7 +222,7 @@ func validateDefinition(generic map[string]interface{}, path string) error {
path,
1)
} else if !core.StringInSlice(point.(string), extensionPoints) {
key := point.(string)
key, _ := point.(string)
return core.NewE201FromTarget(
fmt.Sprintf("'extends' key must be one of %v.", extensionPoints),
key,
Expand Down Expand Up @@ -279,7 +279,7 @@ func makeRegexp(
noCase bool,
word func() bool,
callback func() string,
append bool,
shouldAppend bool,
) string {
regex := ""

Expand All @@ -293,7 +293,7 @@ func makeRegexp(
regex += nonwordTemplate
}

if append {
if shouldAppend {
regex += callback()
} else {
regex = callback() + regex
Expand Down Expand Up @@ -341,7 +341,7 @@ func updateExceptions(previous []string, current map[string]struct{}) (*regexp2.
return regexp2.CompileStd(regex)
}

return nil, nil
return &regexp2.Regexp{}, nil
}

func decodeRule(input interface{}, output interface{}) error {
Expand Down
8 changes: 4 additions & 4 deletions internal/check/existence.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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, file *core.File) ([]core.Alert, error) {
func (e Existence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
alerts := []core.Alert{}

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

observed := strings.TrimSpace(converted)
if !isMatch(e.exceptRe, observed) {
a, err := makeAlert(e.Definition, loc, blk.Text)
if err != nil {
return alerts, err
a, erra := makeAlert(e.Definition, loc, blk.Text)
if erra != nil {
return alerts, erra
}
alerts = append(alerts, a)
}
Expand Down
1 change: 0 additions & 1 deletion internal/check/existence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestExistence(t *testing.T) {
if len(alerts) != 1 {
t.Errorf("expected one alert, not %v", alerts)
}

}

func FuzzExistenceInit(f *testing.F) {
Expand Down
2 changes: 1 addition & 1 deletion internal/check/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func filter(mgr *Manager) (map[string]Rule, error) {

filtered := map[string]Rule{}
for _, entry := range output.([]interface{}) {
rule := entry.(Definition)
rule, _ := entry.(Definition)

name := rule.Name
if strings.Count(name, ".") > 1 {
Expand Down
4 changes: 2 additions & 2 deletions internal/check/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (mgr *Manager) addCheck(file []byte, chkName, path string) error {

if level, ok := mgr.Config.RuleToLevel[chkName]; ok {
generic["level"] = level
} else if _, ok := generic["level"]; !ok {
} else if _, ok = generic["level"]; !ok {
generic["level"] = "warning"
}
if scope, ok := generic["scope"]; scope == nil || !ok {
Expand Down Expand Up @@ -276,6 +276,6 @@ func (mgr *Manager) loadVocabRules() {
}

func (mgr *Manager) hasStyle(name string) bool {
styles := append(mgr.styles, defaultStyles...)
styles := append(mgr.styles, defaultStyles...) //nolint:gocritic
return core.StringInSlice(name, styles)
}
3 changes: 1 addition & 2 deletions internal/check/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"testing"
)

var checktests = []struct {
/*var checktests = []struct {
check string
msg string
}{
{"NoExtends.yml", "YAML.NoExtends: missing extension point!"},
{"NoMsg.yml", "YAML.NoMsg: missing message!"},
}
/*
func TestAddCheck(t *testing.T) {
cfg, err := config.New()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/check/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Metric struct {
}

// NewMetric creates a new `metric`-based rule.
func NewMetric(cfg *core.Config, generic baseCheck, path string) (Metric, error) {
func NewMetric(_ *core.Config, generic baseCheck, path string) (Metric, error) {
rule := Metric{}

err := decodeRule(generic, &rule)
Expand All @@ -47,13 +47,13 @@ func NewMetric(cfg *core.Config, generic baseCheck, path string) (Metric, error)
}

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

parameters, err := f.ComputeMetrics()
if err != nil {
return alerts, nil
return alerts, err
}

for _, k := range variables {
Expand Down
4 changes: 2 additions & 2 deletions internal/check/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Occurrence struct {
}

// NewOccurrence creates a new `occurrence`-based rule.
func NewOccurrence(cfg *core.Config, generic baseCheck, path string) (Occurrence, error) {
func NewOccurrence(_ *core.Config, generic baseCheck, path string) (Occurrence, error) {
rule := Occurrence{}

err := decodeRule(generic, &rule)
Expand Down Expand Up @@ -47,7 +47,7 @@ func NewOccurrence(cfg *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, f *core.File) ([]core.Alert, error) {
func (o Occurrence) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
var a core.Alert
var err error
var alerts []core.Alert
Expand Down
6 changes: 3 additions & 3 deletions internal/check/readability.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Readability struct {
}

// NewReadability creates a new `readability`-based rule.
func NewReadability(cfg *core.Config, generic baseCheck, path string) (Readability, error) {
func NewReadability(_ *core.Config, generic baseCheck, path string) (Readability, error) {
rule := Readability{}

err := decodeRule(generic, &rule)
Expand All @@ -40,7 +40,7 @@ func NewReadability(cfg *core.Config, generic baseCheck, path string) (Readabili
}

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

Expand All @@ -62,7 +62,7 @@ func (o Readability) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
grade += doc.AutomatedReadability()
}

grade = grade / float64(len(o.Metrics))
grade /= float64(len(o.Metrics))
if grade > o.Grade {
a := core.Alert{Check: o.Name, Severity: o.Level,
Span: []int{1, 1}, Link: o.Link}
Expand Down
10 changes: 5 additions & 5 deletions internal/check/repetition.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Repetition struct {
}

// NewRepetition creates a new `repetition`-based rule.
func NewRepetition(cfg *core.Config, generic baseCheck, path string) (Repetition, error) {
func NewRepetition(_ *core.Config, generic baseCheck, path string) (Repetition, error) {
rule := Repetition{}

err := decodeRule(generic, &rule)
Expand Down Expand Up @@ -50,7 +50,7 @@ func NewRepetition(cfg *core.Config, generic baseCheck, path string) (Repetition
// Run executes the the `repetition`-based rule.
//
// The rule looks for repeated matches of its regex -- such as "this this".
func (o Repetition) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
func (o Repetition) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
var curr, prev string
var hit bool
var ploc []int
Expand Down Expand Up @@ -87,9 +87,9 @@ func (o Repetition) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
if !strings.Contains(converted, "\n") {
floc := []int{ploc[0], loc[1]}

a, err := makeAlert(o.Definition, floc, txt)
if err != nil {
return alerts, err
a, erra := makeAlert(o.Definition, floc, txt)
if erra != nil {
return alerts, erra
}

a.Message, a.Description = formatMessages(o.Message,
Expand Down
8 changes: 4 additions & 4 deletions internal/check/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Script struct {
}

// NewScript creates a new `script`-based rule.
func NewScript(cfg *core.Config, generic baseCheck, path string) (Script, error) {
func NewScript(_ *core.Config, generic baseCheck, path string) (Script, error) {
rule := Script{}

err := decodeRule(generic, &rule)
Expand All @@ -31,7 +31,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, f *core.File) ([]core.Alert, error) {
func (s Script) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
var alerts []core.Alert

script := tengo.NewScript([]byte(s.Script))
Expand All @@ -51,7 +51,7 @@ func (s Script) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
return alerts, core.NewE201FromTarget(err.Error(), "script", s.path)
}

if err := compiled.Run(); err != nil {
if err = compiled.Run(); err != nil {
return alerts, core.NewE201FromTarget(err.Error(), "script", s.path)
}

Expand All @@ -74,7 +74,7 @@ func (s Script) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
func toLocation(a []interface{}) [][]int {
locs := [][]int{}
for _, i := range a {
m := i.(map[string]interface{})
m, _ := i.(map[string]interface{})
locs = append(locs, []int{
int(m["begin"].(int64)),
int(m["end"].(int64)),
Expand Down
15 changes: 7 additions & 8 deletions internal/check/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ func NewSequence(cfg *core.Config, generic baseCheck, path string) (Sequence, er
false)
regex = fmt.Sprintf(regex, token.Pattern)

re, err := regexp2.CompileStd(regex)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
re, errc := regexp2.CompileStd(regex)
if errc != nil {
return rule, core.NewE201FromPosition(errc.Error(), path, 1)
}
rule.Tokens[i].re = re
}

}

rule.Definition.Scope = []string{"sentence"}
Expand Down Expand Up @@ -236,16 +235,16 @@ func (s Sequence) Run(blk nlp.Block, f *core.File) ([]core.Alert, error) {
// We're looking for our "anchor" ...
for _, loc := range tok.re.FindAllStringIndex(txt, -1) {
// These are all possible violations in `txt`:
steps, index := sequenceMatches(idx, s, tok, words)
s.history = append(s.history, index)
steps, _ := sequenceMatches(idx, s, tok, words)
// s.history = append(s.history, index)

if len(steps) > 0 {
seq := stepsToString(steps)
idx := strings.Index(txt, seq)
ssp := strings.Index(txt, seq)

a := core.Alert{
Check: s.Name, Severity: s.Level, Link: s.Link,
Span: []int{idx, idx + len(seq)}, Hide: false,
Span: []int{ssp, ssp + len(seq)}, Hide: false,
Match: seq, Action: s.Action}

a.Message, a.Description = formatMessages(s.Message,
Expand Down
Loading

0 comments on commit 3595145

Please sign in to comment.