From 93e53f018f604485adfcd07d9dd9536453403931 Mon Sep 17 00:00:00 2001 From: chad-loder <26261238+chad-loder@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:52:25 -0700 Subject: [PATCH] fix(lint): take the tree-sitter grammar from a [formats] mapping when the extension has none --- internal/lint/code.go | 18 ++++++++++++++++- internal/lint/code_test.go | 36 ++++++++++++++++++++++++++++++++++ internal/lint/fragment.go | 2 +- testdata/e2e/config-flags.yaml | 29 +++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/internal/lint/code.go b/internal/lint/code.go index 07ae7260..1d914da1 100755 --- a/internal/lint/code.go +++ b/internal/lint/code.go @@ -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) diff --git a/internal/lint/code_test.go b/internal/lint/code_test.go index a70d513a..9c96ab4d 100644 --- a/internal/lint/code_test.go +++ b/internal/lint/code_test.go @@ -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) + } + }) + } +} diff --git a/internal/lint/fragment.go b/internal/lint/fragment.go index 20bddc82..fb0663c8 100644 --- a/internal/lint/fragment.go +++ b/internal/lint/fragment.go @@ -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 } diff --git a/testdata/e2e/config-flags.yaml b/testdata/e2e/config-flags.yaml index 9b604ab9..b648a381 100644 --- a/testdata/e2e/config-flags.yaml +++ b/testdata/e2e/config-flags.yaml @@ -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'.