Skip to content

Commit

Permalink
fix: ensure applyPatterns respects formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 15, 2024
1 parent 341348c commit 5e502a0
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 13 deletions.
3 changes: 2 additions & 1 deletion internal/core/file.go
Expand Up @@ -30,6 +30,7 @@ type File struct {
Format string // 'code', 'markup' or 'prose'
NormedExt string // the normalized extension (see util/format.go)
Path string // the full path
NormedPath string // the normalized path
Transform string // XLST transform
RealExt string // actual file extension
Checks map[string]bool // syntax-specific checks assigned in .vale
Expand Down Expand Up @@ -120,7 +121,7 @@ func NewFile(src string, config *Config) (*File, error) {
simple: config.Flags.Simple, Transform: transform,
limits: make(map[string]int), Path: src, Metrics: make(map[string]int),
NLP: nlp.Info{Endpoint: config.NLPEndpoint, Lang: lang},
Lookup: lookup,
Lookup: lookup, NormedPath: ReplaceExt(src, config.Formats),
}

return &file, nil
Expand Down
17 changes: 17 additions & 0 deletions internal/core/util.go
Expand Up @@ -302,3 +302,20 @@ func HasAnySuffix(s string, suffixes []string) bool {
}
return false
}

// ReplaceExt replaces the extension of `fp` with `ext` if the extension of
// `fp` is in `formats`.
//
// This is used in places where we need to normalize file extensions (e.g.,
// `foo.mdx` -> `foo.md`) in order to respect format associations.
func ReplaceExt(fp string, formats map[string]string) string {
var ext string

old := filepath.Ext(fp)
if normed, found := formats[strings.Trim(old, ".")]; found {
ext = "." + normed
fp = fp[0:len(fp)-len(old)] + ext
}

return fp
}
2 changes: 1 addition & 1 deletion internal/lint/adoc.go
Expand Up @@ -81,7 +81,7 @@ func (l *Linter) lintADoc(f *core.File) error {
return core.NewE100("lintAdoc", errors.New("asciidoctor not found"))
}

s, err := l.applyPatterns(f.Content, "\n----\n$1\n----\n", "`$1`", f.Path)
s, err := l.applyPatterns(f.Content, "\n----\n$1\n----\n", "`$1`", f.NormedPath)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions internal/lint/lint.go
Expand Up @@ -347,15 +347,8 @@ func (l *Linter) match(s string) bool {
}

func (l *Linter) skip(fp string) bool {
var ext string
fp = filepath.ToSlash(core.ReplaceExt(fp, l.Manager.Config.Formats))

old := filepath.Ext(fp)
if normed, found := l.Manager.Config.Formats[strings.Trim(old, ".")]; found {
ext = "." + normed
fp = fp[0:len(fp)-len(old)] + ext
}

fp = filepath.ToSlash(fp)
if !l.match(fp) {
return true
} else if l.nonGlobal {
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/md.go
Expand Up @@ -33,7 +33,7 @@ var reLinkDef = regexp.MustCompile(`\[(?:[^]]+)\]:`)
func (l Linter) lintMarkdown(f *core.File) error {
var buf bytes.Buffer

s, err := l.applyPatterns(f.Content, "\n```\n$1\n```\n", "`$1`", f.Path)
s, err := l.applyPatterns(f.Content, "\n```\n$1\n```\n", "`$1`", f.NormedPath)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/org.go
Expand Up @@ -34,7 +34,7 @@ func (l Linter) lintOrg(f *core.File) error {
s := reOrgAttribute.ReplaceAllString(f.Content, "\n=$1=\n")
s = reOrgProps.ReplaceAllString(s, orgExample)

s, err := l.applyPatterns(s, orgExample, "=$1=", f.Path)
s, err := l.applyPatterns(s, orgExample, "=$1=", f.NormedPath)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lint/rst.go
Expand Up @@ -136,7 +136,7 @@ func (l *Linter) lintRST(f *core.File) error {
return core.NewE100("lintRST", errors.New("rst2html not found"))
}

s, err := l.applyPatterns(f.Content, "\n::\n\n%s\n", "``$1``", f.Path)
s, err := l.applyPatterns(f.Content, "\n::\n\n%s\n", "``$1``", f.NormedPath)
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/features/patterns.feature
@@ -1,4 +1,15 @@
Feature: IgnorePatterns

Scenario: MDX
When I test patterns for "test.mdx"
Then the output should contain exactly:
"""
test.mdx:1:19:vale.Redundancy:'ACT test' is redundant
test.mdx:7:19:vale.Redundancy:'ACT test' is redundant
test.mdx:17:1:vale.Redundancy:'ACT test' is redundant
"""
And the exit status should be 0

Scenario: Markdown
When I test patterns for "test.md"
Then the output should contain exactly:
Expand Down
3 changes: 3 additions & 0 deletions testdata/fixtures/patterns/_vale
Expand Up @@ -4,6 +4,9 @@ MinAlertLevel = suggestion
IgnoredScopes = code, tt, strong, comment.line
IgnoredClasses = metrics, blurb, alignedsummary

[formats]
mdx = md

[*]
BasedOnStyles = Vale

Expand Down
29 changes: 29 additions & 0 deletions testdata/fixtures/patterns/test.mdx
@@ -0,0 +1,29 @@
This is some text ACT test

{{< file-excerpt "script.py" python >}}
This is some text ACT test
{{< /file-excerpt >}}

This is some text ACT test

This is some text **ACT test**

{{< file-excerpt "_includes/scripts.html" html >}}
<scrip type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></scrip>
{% ACT test %}
{{< /file-excerpt >}}

ACT test

$ACT test$

<https://gitlan.com/config>

Some backtick text like `--config`.

- <https://gitlab.com/gitlab-org/security-products/license-management/-/blob/0b976fcffe0a9b8e80587adb076bcdf279c9331c/config/install.sh#L168-170>
- <https://gitlab.com/gitlab-org/security-products/license-management/-/blob/0b976fcffe0a9b8e80587adb076bcdf279c9331c/config/.bashrc#L49>

<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

0 comments on commit 5e502a0

Please sign in to comment.