Skip to content

Commit

Permalink
refactor: validate scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jul 6, 2023
1 parent 9174696 commit 4711b32
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/check/capitalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func NewCapitalization(cfg *core.Config, generic baseCheck, path string) (Capita
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
Expand Down
5 changes: 5 additions & 0 deletions internal/check/conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func NewConditional(cfg *core.Config, generic baseCheck, path string) (Condition
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
Expand Down
5 changes: 5 additions & 0 deletions internal/check/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func NewConsistency(cfg *core.Config, generic baseCheck, path string) (Consisten
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

regex := makeRegexp(
cfg.WordTemplate,
rule.Ignorecase,
Expand Down
14 changes: 14 additions & 0 deletions internal/check/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,17 @@ func decodeRule(input interface{}, output interface{}) error {

return decoder.Decode(input)
}

func checkScopes(scopes []string, path string) error {
for _, scope := range scopes {
// Negation ...
scope = strings.TrimPrefix(scope, "~")
if !core.StringInSlice(scope, allowedScopes) {
return core.NewE201FromTarget(
fmt.Sprintf("'scope' must be one of %v", allowedScopes),
"scope",
path)
}
}
return nil
}
5 changes: 5 additions & 0 deletions internal/check/existence.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func NewExistence(cfg *core.Config, generic baseCheck, path string) (Existence,
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
Expand Down
5 changes: 5 additions & 0 deletions internal/check/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func NewOccurrence(cfg *core.Config, generic baseCheck, path string) (Occurrence
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

regex := ""
if rule.Ignorecase {
regex += ignoreCase
Expand Down
5 changes: 5 additions & 0 deletions internal/check/repetition.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func NewRepetition(cfg *core.Config, generic baseCheck, path string) (Repetition
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

regex := ""
if rule.Ignorecase {
regex += ignoreCase
Expand Down
22 changes: 22 additions & 0 deletions internal/check/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ import (
"github.com/errata-ai/vale/v2/internal/nlp"
)

var allowedScopes = []string{
"text",
"heading",
"heading.h1",
"heading.h2",
"heading.h3",
"heading.h4",
"heading.h5",
"heading.h6",
"table.header",
"table.cell",
"table.caption",
"figure.caption",
"list",
"paragraph",
"sentence",
"alt",
"blockquote",
"summary",
"raw",
}

// A Selector represents a named section of text.
type Selector struct {
Value []string // e.g., text.comment.line.py
Expand Down
5 changes: 5 additions & 0 deletions internal/check/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func NewSequence(cfg *core.Config, generic baseCheck, path string) (Sequence, er
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}

for i, token := range rule.Tokens {
if !rule.needsTagging && token.Tag != "" {
rule.needsTagging = true
Expand Down
5 changes: 5 additions & 0 deletions internal/check/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func NewSubstitution(cfg *core.Config, generic baseCheck, path string) (Substitu
if err != nil {
return rule, readStructureError(err, path)
}

err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}
tokens := ""

re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
Expand Down

0 comments on commit 4711b32

Please sign in to comment.