Skip to content

Commit

Permalink
Error on <br> tags in source files (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbaldry authored Dec 22, 2024
1 parent e76150e commit 21a4780
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 16 deletions.
7 changes: 7 additions & 0 deletions vale/Grafana/Paragraphs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends: script
scope: raw
level: error
link: https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-br-element
message: |
br elements must be used only for line breaks that are actually part of the content, as in poems or addresses.
script: Paragraphs.tengo
36 changes: 36 additions & 0 deletions vale/config/scripts/Paragraphs.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
DEBUG := false

fmt := import("fmt")
text := import("text")

matches := []
ruleActive := true

cursor := 0
for line in text.split(scope, "\n") {
if DEBUG {
fmt.println(text.pad_left(text.itoa(cursor), 3, " "), ": ", text.quote(line), " ", len(line))
}

if text.re_find(`<!-- vale Grafana.Paragraphs = NO -->`, line, -1) {
ruleActive = false
}

if text.re_find(`<!-- vale Grafana.Paragraphs = YES -->`, line, -1) {
ruleActive = true
}

if ruleActive {
for match in text.re_find(`<br */?>`, line, -1) {
matches = append(matches, {begin: cursor + match[0].begin, end: cursor + match[0].end})
}
}

cursor += len(line) + 1
}

if DEBUG {
for match in matches {
fmt.println(match)
}
}
87 changes: 87 additions & 0 deletions vale/config/scripts/Paragraphs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package test

import (
"context"
"os"
"regexp"
"testing"

"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParagraphs(t *testing.T) {
t.Run("don't use", func(t *testing.T) {
src := `Don't use:
- <br>
- <br />
- <br/>
`
data, err := os.ReadFile("Paragraphs.tengo")
require.NoError(t, err)

data = regexp.MustCompile(`DEBUG := false`).ReplaceAll(data, []byte(`DEBUG := true`))

script := tengo.NewScript(data)
script.SetImports(stdlib.GetModuleMap("text", "fmt", "math"))

err = script.Add("scope", src)
require.NoError(t, err)

compiled, err := script.RunContext(context.Background())
require.NoError(t, err)

want := []map[string]int{
{"begin": 14, "end": 18},
{"begin": 21, "end": 27},
{"begin": 30, "end": 35},
}

assert.Equal(t, want, mustParseMatches(compiled.Get("matches").Array()))
})

t.Run("with exclusions", func(t *testing.T) {
src := `
Test exclusions:
<!-- vale Grafana.Paragraphs = NO -->
- <br>
- <br />
- <br/>
<!-- vale Grafana.Paragraphs = YES -->
Don't use:
- <br>
- <br />
- <br/>
`

data, err := os.ReadFile("Paragraphs.tengo")
require.NoError(t, err)

data = regexp.MustCompile(`DEBUG := false`).ReplaceAll(data, []byte(`DEBUG := true`))

script := tengo.NewScript(data)
script.SetImports(stdlib.GetModuleMap("text", "fmt", "math"))

err = script.Add("scope", src)
require.NoError(t, err)

compiled, err := script.RunContext(context.Background())
require.NoError(t, err)

want := []map[string]int{
{"begin": 137, "end": 141},
{"begin": 144, "end": 150},
{"begin": 153, "end": 158},
}

assert.Equal(t, want, mustParseMatches(compiled.Get("matches").Array()))
})
}
16 changes: 0 additions & 16 deletions vale/config/scripts/SmartQuotes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ import (
"github.com/stretchr/testify/require"
)

func mustParseMatches(a []interface{}) []map[string]int {
matches := []map[string]int{}

for _, i := range a {
m, _ := i.(map[string]any)
match := map[string]int{
"begin": int(m["begin"].(int64)),
"end": int(m["end"].(int64)),
}

matches = append(matches, match)
}

return matches
}

func TestSmartQuotes(t *testing.T) {
t.Run("don't use", func(t *testing.T) {
src := `Don't use:
Expand Down
17 changes: 17 additions & 0 deletions vale/config/scripts/tengo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test

func mustParseMatches(a []interface{}) []map[string]int {
matches := []map[string]int{}

for _, i := range a {
m, _ := i.(map[string]any)
match := map[string]int{
"begin": int(m["begin"].(int64)),
"end": int(m["end"].(int64)),
}

matches = append(matches, match)
}

return matches
}

0 comments on commit 21a4780

Please sign in to comment.