Skip to content

Commit

Permalink
finds all uncommented ellipses
Browse files Browse the repository at this point in the history
  • Loading branch information
apinnick committed Sep 29, 2024
1 parent 7dd6722 commit 1cd5db2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .vale/fixtures/RedHat/YamlEllipses/testinvalid.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
----
...
...
----
...
----
6 changes: 3 additions & 3 deletions .vale/fixtures/RedHat/YamlEllipses/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[source,yaml]
----
# ...
# ...
# This is an ordinary comment
----

[source,terminal]
----
...
----

20 changes: 12 additions & 8 deletions .vale/styles/RedHat/YamlEllipses.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
extends: existence
extends: script
level: suggestion
message: "Use '# ...' rather than '...' as ellipsis in YAML code blocks."
message: "Use '# ...' instead of '...' in YAML code blocks."
link: https://yaml.org/spec/1.2.2/#22-structures
scope: raw
script: |
Expand All @@ -14,18 +14,22 @@ script: |
scope += "\n"
codeblock_delim_regex := "^-{4,}$"
source_block_regex := "^\\[(source,yaml).*\\]"
ellipses := 0
reg_ellipsis := "^\\s*\\.\\.\\."
comment_ellipsis := "^#"
reg_ellipsis := "^\\s*\\.{3}$"
inside_codeblock := false
for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
if text.re_match(source_block_regex, line){
//ignore content in codeblocks
if text.re_match(codeblock_delim_regex, line) && inside_codeblock == false {
inside_codeblock = true
} else if text.re_match(codeblock_delim_regex, line) && inside_codeblock == true {
inside_codeblock = false
}
if !text.re_match(comment_ellipsis, line) && text.re_match(reg_ellipsis, line) && inside_codeblock == true {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
} else if text.re_match(reg_ellipsis, line){
ellipses ++
}
}
18 changes: 12 additions & 6 deletions tengo-rule-scripts/YamlEllipses.tengo
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
scope += "\n"

codeblock_delim_regex := "^-{4,}$"
source_block_regex := "^\\[(yml|yaml).*\\]"
yaml_block_regex := "^\\[(source,yaml).*\\]"
ellipses := 0
reg_ellipsis := "^\\s*\\.\\.\\."
comment_ellipsis := "^# \\.{3}$"
reg_ellipsis := "^\\s*\\.{3}$"
inside_codeblock := false

for line in text.split(scope, "\n") {
// trim trailing whitespace
line = text.trim_space(line)
if text.re_match(source_block_regex, line){
//ignore content in codeblocks
if text.re_match(codeblock_delim_regex, line) && inside_codeblock == false {
inside_codeblock = true
} else if text.re_match(codeblock_delim_regex, line) && inside_codeblock == true {
inside_codeblock = false
}
if !text.re_match(comment_ellipsis, line) && text.re_match(reg_ellipsis, line) && inside_codeblock == true {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
} else if text.re_match(reg_ellipsis, line){
ellipses ++
}
}

fmt.println(ellipses)
fmt.println(matches)

0 comments on commit 1cd5db2

Please sign in to comment.