-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error on <br> tags in source files (#927)
- Loading branch information
Showing
5 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |