Skip to content
Merged
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
18 changes: 17 additions & 1 deletion internal/lint/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,24 @@ func (l *Linter) skipsComment(scope string) bool {
core.StringInSlice(scope, ignored)
}

func (l *Linter) lintCode(f *core.File) error {
// languageFor returns the tree-sitter grammar for a file. When the file's own
// extension has none, a `[formats]` association (`mjs = js`) supplies it, so a
// mapped file is parsed like the format it is mapped to.
func (l *Linter) languageFor(f *core.File) (*code.Language, error) {
lang, err := code.GetLanguageFromExt(f.RealExt)
if err == nil {
return lang, nil
}
if mapped, ok := l.Manager.Config.Formats[strings.TrimPrefix(f.RealExt, ".")]; ok {
if lang, mappedErr := code.GetLanguageFromExt("." + mapped); mappedErr == nil {
return lang, nil
}
}
return nil, err
}

func (l *Linter) lintCode(f *core.File) error {
lang, err := l.languageFor(f)
if err != nil {
// No tree-sitter grammar available for this file type.
return l.lintCodeOld(f)
Expand Down
36 changes: 36 additions & 0 deletions internal/lint/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,39 @@ func TestSkipsComment(t *testing.T) {
})
}
}

// A `[formats]` association supplies the grammar for an extension that has
// none of its own; without one, such a file is linted by the regex fallback,
// which has no JavaScript entry and so reads no comments at all.
func TestLanguageForMappedExtension(t *testing.T) {
cfg, err := core.NewConfig(&core.CLIFlags{})
if err != nil {
t.Fatal(err)
}
linter, err := NewLinter(cfg)
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
formats map[string]string
ext string
want bool
}{
{"own grammar", nil, ".js", true},
{"unmapped, no grammar", nil, ".mjs", false},
{"mapped to a grammar", map[string]string{"mjs": "js"}, ".mjs", true},
{"mapped to a format without one", map[string]string{"mjs": "md"}, ".mjs", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
linter.Manager.Config.Formats = tt.formats
lang, err := linter.languageFor(&core.File{RealExt: tt.ext})
if got := err == nil && lang != nil; got != tt.want {
t.Errorf("languageFor(%q) with %v found a grammar = %v, want %v",
tt.ext, tt.formats, got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/lint/fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func commentPadding(comment code.Comment, line int, source string, lang *code.La
}

func (l *Linter) lintFragments(f *core.File) error {
lang, err := code.GetLanguageFromExt(f.RealExt)
lang, err := l.languageFor(f)
if err != nil {
return err
}
Expand Down
29 changes: 29 additions & 0 deletions testdata/e2e/config-flags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,32 @@ cases:
exit: 1
want: |
COMMIT_EDITMSG:1:30:Test.Term:Use 'flag' instead of 'option'.

- name: formats-code-grammar
about: a `[formats]` key that maps an extension to a code format reads its
comments with that format's grammar; `.mjs` has none of its own and was
read by the regex fallback, which found no comments at all
files:
.vale.ini: |
StylesPath = styles
MinAlertLevel = suggestion

[formats]
mjs = js

[*.mjs]
BasedOnStyles = Test
styles/Test/Term.yml: |
extends: existence
message: "Use 'flag' instead of '%s'."
level: error
tokens:
- option
test.mjs: |
// Read the option first.
const a = 1; /* and this option */
args: test.mjs
exit: 1
want: |
test.mjs:1:13:Test.Term:Use 'flag' instead of 'option'.
test.mjs:2:26:Test.Term:Use 'flag' instead of 'option'.
Loading