Skip to content

Commit

Permalink
Merge pull request #729 from aireilly/add-check-unset-attr
Browse files Browse the repository at this point in the history
Adding unset attribute rule
  • Loading branch information
aireilly authored Sep 13, 2024
2 parents cf45d52 + 2b927b1 commit 6c70712
Show file tree
Hide file tree
Showing 5 changed files with 149 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
17 changes: 17 additions & 0 deletions .vale/fixtures/AsciiDoc/UnsetAttributes/testinvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
:_mod-docs-content-type: PROCEDURE
:context: creating-infrastructure-machinesets
= Testing

ifeval::["{context}" == "creating-infrastructure-machinesets"]
:infra: test
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:
20 changes: 20 additions & 0 deletions .vale/fixtures/AsciiDoc/UnsetAttributes/testvalid.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
:context: creating-infrastructure-machinesets
:_mod-docs-content-type: ASSEMBLY

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

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

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

//This is really just unsetting a second time, but it should not trip the rule
ifeval::["{context}" == "creating-infrastructure"]
//vale-fixture
:type!:
endif::[]
49 changes: 49 additions & 0 deletions .vale/styles/AsciiDoc/UnsetAttributes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
extends: script
level: error
message: "Set attribute directive does not have a corresponding unset attribute directive."
link: https://docs.asciidoctor.org/asciidoc/latest/attributes/unset-attributes/#unset-a-document-attribute-in-the-body
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-_]+:.*$"
context_mod_docs_regex := "^:context|_mod-docs-content-type:.*$"
attr_name_regex := ":[\\w-_]+:"
attr_name := ""
unset_attr_pref := ""
unset_attr_suff := ""
for line in text.split(scope, "\n") {
if text.re_match(attr_regex, line) {
if !text.re_match(context_mod_docs_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
// re_find returns an array holding all matches
attr_name = ((text.re_find(attr_name_regex, line))[0][0])["text"]
unset_attr_pref = `^:!` + text.trim_prefix(attr_name, `:`)
unset_attr_suff = `^` + text.trim_suffix(attr_name, `:`) + `!:`
// 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]
}
}
}
}
}
}
}
58 changes: 58 additions & 0 deletions tengo-rule-scripts/UnsetAttributes.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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-_]+:.*$"
context_mod_docs_regex := "^:context|_mod-docs-content-type:.*$"
attr_name_regex := ":[\\w-_]+:"
attr_name := ""
unset_attr_pref := ""
unset_attr_suff := ""

for line in text.split(scope, "\n") {
if text.re_match(attr_regex, line) {
if !text.re_match(context_mod_docs_regex, line) {
start := text.index(scope, line)
matches = append(matches, {begin: start, end: start + len(line)})
// re_find returns an array holding all matches
attr_name = ((text.re_find(attr_name_regex, line))[0][0])["text"]
unset_attr_pref = `^:!` + text.trim_prefix(attr_name, `:`)
unset_attr_suff = `^` + text.trim_suffix(attr_name, `:`) + `!:`
// 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 6c70712

Please sign in to comment.