Skip to content

Commit

Permalink
Adding unset attr rule
Browse files Browse the repository at this point in the history
  • Loading branch information
aireilly committed Mar 6, 2024
1 parent edaab0b commit 7f950ec
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vale/fixtures/AsciiDoc/UnsetAttributes/.vale.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; Vale configuration file to test the `UnsetAttributes` rule
StylesPath = ../../../styles
MinAlertLevel = error
[*.adoc]
AsciiDoc.UnsetAttributes = YES
15 changes: 15 additions & 0 deletions .vale/fixtures/AsciiDoc/UnsetAttributes/testinvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:context: creating-infrastructure-machinesets

ifeval::["{context}" == "creating-infrastructure-machinesets"]
:infra:
endif::[]

Vale reports lots of errors, it should also report an attribute that you forgot to unset

////
Ignore multi-line comments
:!infra:
////

//ignore comments
//:!infra:
19 changes: 19 additions & 0 deletions .vale/fixtures/AsciiDoc/UnsetAttributes/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
:context: creating-infrastructure-machinesets

ifeval::["{context}" == "creating-infrastructure-machinesets"]
//vale-fixture
:infra:
endif::[]

Vale reports lots of errors, it should also report an attribute that you forgot to unset

ifeval::["{context}" == "creating-infrastructure-machinesets"]
//vale-fixture
:!infra:
endif::[]

//This is really just unsetting a second time, but it should not trip the rule
ifeval::["{context}" == "creating-infrastructure"]
//vale-fixture
:infra!:
endif::[]
42 changes: 42 additions & 0 deletions .vale/styles/AsciiDoc/UnsetAttributes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
extends: script
level: error
message: "Set attribute has not been unset in the current file."
link: https://docs.asciidoctor.org/asciidoc/latest/attributes/unset-attributes/#unset-a-document-attribute-in-the-header
scope: raw
script: |
text := import("text")
matches := []
// trim extra whitespace
scope = text.trim_space(scope)
// add a newline, it might be missing
scope += "\n"
// clean out multi-line comments
scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")
attr_regex := "^:[\\w-]+:$"
unset_attr_pref := ""
unset_attr_suff := ""
for line in text.split(scope, "\n") {
if text.re_match(attr_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
unset_attr_pref = `^:!` + text.trim_prefix(line, `:`)
unset_attr_suff = `^` + text.trim_suffix(line, `:`) + `!:`
// loop through lines for every attr found
for line in text.split(scope, "\n") {
if text.re_match(unset_attr_pref, line) {
if len(matches) > 0 {
// remove the most recently added match
matches = matches[:len(matches)-1]
} else if text.re_match(unset_attr_suff, line) {
if len(matches) > 0 {
matches = matches[:len(matches)-1]
}
}
}
}
}
}
51 changes: 51 additions & 0 deletions tengo-rule-scripts/UnsetAttributes.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Tengo Language
$ tengo UnsetAttributes.tengo <asciidoc_file_to_validate>
*/

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

input := os.args()
scope := os.read_file(input[2])
matches := []

// trim extra whitespace
scope = text.trim_space(scope)
// add a newline, it might be missing
scope += "\n"
// clean out multi-line comments
scope = text.re_replace("(?s) *(\n////.*?////\n)", scope, "")

attr_regex := "^:[\\w-]+:$"
unset_attr_pref := ""
unset_attr_suff := ""

for line in text.split(scope, "\n") {
if text.re_match(attr_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
unset_attr_pref = `^:!` + text.trim_prefix(line, `:`)
unset_attr_suff = `^` + text.trim_suffix(line, `:`) + `!:`
// loop through lines for every attr found
for line in text.split(scope, "\n") {
if text.re_match(unset_attr_pref, line) {
if len(matches) > 0 {
// remove the most recently added match
matches = matches[:len(matches)-1]
} else if text.re_match(unset_attr_suff, line) {
if len(matches) > 0 {
matches = matches[:len(matches)-1]
}
}
}
}
}
}

if len(matches) == 0 {
fmt.println("Attributes are unset properly")
} else {
fmt.println(matches)
}

0 comments on commit 7f950ec

Please sign in to comment.