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
6 changes: 5 additions & 1 deletion internal/check/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ func spelling(alert core.Alert, cfg *core.Config) ([]string, error) {
if len(name) != 2 {
return suggestions, fmt.Errorf("unknown check '%s'", alert.Check)
}
path := filepath.Join(cfg.StylesPath(), name[0], name[1]+".yml")
stem := filepath.Join(cfg.StylesPath(), name[0], name[1])
path, found := core.FindRuleFile(cfg.FS, stem)
if !found {
path = stem + ".yml"
}

mgr, err := NewManager(cfg)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/check/inherit.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ func (mgr *Manager) resolveRule(name string) (string, error) {
return "", fmt.Errorf("'%s' is not a rule name", name)
}

rel := filepath.Join(parts[1:]...) + ".yml"
rel := filepath.Join(parts[1:]...)
for _, p := range mgr.Config.SearchPaths() {
candidate := filepath.Join(p, parts[0], rel)
if mgr.Config.FS.Exists(candidate) {
stem := filepath.Join(p, parts[0], rel)
if candidate, found := core.FindRuleFile(mgr.Config.FS, stem); found {
return candidate, nil
}
}

return "", fmt.Errorf(
"'%s' does not exist on the search path (looked for '%s')",
"'%s' does not exist on the search path (looked for '%s.yml' or '.yaml')",
name, filepath.Join(parts[0], rel))
}

Expand Down
24 changes: 24 additions & 0 deletions internal/check/inherit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ message: "leaf"
t.Errorf("chain flattened wrong: %v", got)
}
}

// A parent may be written as `.yaml`; with both spellings beside each other,
// `.yml` is the rule. See #1217.
func TestResolveRuleExtensions(t *testing.T) {
mgr, sp := managerWithStyles(t, map[string]string{
"Only/Words.yaml": parentRule,
"Both/Words.yml": parentRule,
"Both/Words.yaml": parentRule,
})

cases := []struct{ name, want string }{
{"Only.Words", filepath.Join(sp, "Only", "Words.yaml")},
{"Both.Words", filepath.Join(sp, "Both", "Words.yml")},
}
for _, tt := range cases {
got, err := mgr.resolveRule(tt.name)
if err != nil {
t.Fatalf("resolveRule(%q): %v", tt.name, err)
}
if got != tt.want {
t.Errorf("resolveRule(%q) = %q; want %q", tt.name, got, tt.want)
}
}
}
22 changes: 14 additions & 8 deletions internal/check/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ type Manager struct {
// NewManager creates a new Manager and loads the rule definitions (that is,
// extended checks) specified by configuration.
func NewManager(config *core.Config) (*Manager, error) {
var path string

mgr := Manager{
Config: config,

Expand Down Expand Up @@ -71,14 +69,15 @@ func NewManager(config *core.Config) (*Manager, error) {
if !mgr.hasStyle(parts[0]) {
// If this rule isn't part of an already-loaded style, we load it
// individually. Every segment after the style is a path
// component: `Std.dates.TimeFormat` is `Std/dates/TimeFormat.yml`.
fName := filepath.Join(parts[1:]...) + ".yml"
// component: `Std.dates.TimeFormat` is `Std/dates/TimeFormat.yml`
// (or `.yaml`).
stem := filepath.Join(parts[1:]...)
for _, p := range mgr.Config.SearchPaths() {
path = filepath.Join(p, parts[0], fName)
if !mgr.Config.FS.Exists(path) {
rule, found := core.FindRuleFile(mgr.Config.FS, filepath.Join(p, parts[0], stem))
if !found {
continue
}
if err = mgr.addCheckFile(chk, path); err != nil {
if err = mgr.addCheckFile(chk, rule); err != nil {
return &mgr, err
}
}
Expand Down Expand Up @@ -223,7 +222,7 @@ func (mgr *Manager) addStyle(path string) error {
// underscore prefix keeps a directory inert, the same convention
// the Go toolchain and the test-file walk use.
return filepath.SkipDir
case info.IsDir() || !strings.HasSuffix(info.Name(), ".yml"):
case info.IsDir() || !core.HasRuleExt(info.Name()):
return nil
case core.IsTestFile(info.Name()):
// A rule's cases live beside it, so the style directory holds YAML
Expand All @@ -232,6 +231,13 @@ func (mgr *Manager) addStyle(path string) error {
return nil
}

// `Rule.yml` and `Rule.yaml` would both be `Style.Rule`. Only the
// preferred extension is the rule, whichever the walk reaches first.
stem := strings.TrimSuffix(fp, filepath.Ext(fp))
if preferred, _ := core.FindRuleFile(mgr.Config.FS, stem); preferred != fp {
return nil
}

chkName, nErr := core.CheckName(path, fp)
if nErr != nil {
return nErr
Expand Down
23 changes: 23 additions & 0 deletions internal/core/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package core
import (
"path/filepath"
"regexp"
"slices"
"sort"
"strings"

"github.com/vale-cli/vale/v3/internal/glob"
"github.com/vale-cli/vale/v3/internal/system"
)

// TestFileSuffixes name the YAML that holds a rule's test cases rather than a
Expand All @@ -29,6 +31,27 @@ func IsTestFile(name string) bool {
return false
}

// RuleExtensions are the extensions a rule file may have, in order of
// preference: a rule written as both `Rule.yml` and `Rule.yaml` is read from
// `Rule.yml`, the one Vale loaded before `.yaml` was accepted. See #1217.
var RuleExtensions = []string{".yml", ".yaml"}

// HasRuleExt reports whether a file name ends in one of RuleExtensions.
func HasRuleExt(name string) bool {
return slices.Contains(RuleExtensions, filepath.Ext(name))
}

// FindRuleFile returns the file that defines the rule at stem -- its path
// without an extension -- trying each of RuleExtensions in turn.
func FindRuleFile(fsys system.FS, stem string) (string, bool) {
for _, ext := range RuleExtensions {
if path := stem + ext; fsys.Exists(path) {
return path, true
}
}
return "", false
}

// CommentsByNormedExt determines what parts of a file we should lint -- e.g.,
// we only want to lint ; comments in a Clojure file.
//
Expand Down
67 changes: 67 additions & 0 deletions testdata/e2e/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,73 @@ cases:
styles/Demo/Rule.yml:4:5:Vale.Spelling:Did you really mean 'typpo'?
styles/Demo/Rule.yml:5:11:Vale.Spelling:Did you really mean 'typpo'?

- name: rule-yaml-extension
about: "#1217 -- a rule file may end in `.yaml` as well as `.yml`, whether
it loads with its style, is turned on by name, or is extended by a rule"
files:
_vale: |
StylesPath = styles
MinAlertLevel = suggestion

[*]
BasedOnStyles = T
U.Case = YES
styles/T/Hedge.yaml: |
extends: existence
message: "Avoid '%s'."
level: warning
tokens:
- very
styles/T/Coined.yml: |
extends: Base.Words
message: "'%s' is not a word."
styles/U/Case.yaml: |
extends: substitution
message: "Use '%s' instead of '%s'."
level: warning
ignorecase: false
swap:
javascript: JavaScript
styles/Base/Words.yaml: |
extends: existence
message: "'%s'"
level: warning
tokens:
- agendize
args: test.md
exit: 0
want: |
test.md:1:11:T.Hedge:Avoid 'very'.
test.md:1:66:U.Case:Use 'JavaScript' instead of 'javascript'.
test.md:1:77:T.Coined:'agendize' is not a word.

- name: rule-yml-and-yaml
about: "#1217 -- with both `Hedge.yml` and `Hedge.yaml`, `T.Hedge` is read
from `.yml`, the file Vale loaded before `.yaml` was accepted"
files:
_vale: |
StylesPath = styles
MinAlertLevel = suggestion

[*]
BasedOnStyles = T
styles/T/Hedge.yml: |
extends: existence
message: "From .yml: '%s'."
level: warning
tokens:
- very
styles/T/Hedge.yaml: |
extends: existence
message: "From .yaml: '%s'."
level: warning
tokens:
- very
args: test.md
exit: 0
want: |
test.md:1:11:T.Hedge:From .yml: 'very'.

- name: vocab-per-section
about: a section's Vocab adds its terms for that section's files only, with
Terms and Avoid rules of its own; the top-level Vocab is unchanged
Expand Down
Loading