use super::Finding;
const ALWAYS: [&str; 14] = [
"SOKF_FORMAT_31",
"SOKF_FORMAT_60",
"SOKF_FORMAT_65",
"SOKF_FORMAT_72",
"SOKF_FORMAT_77",
"SOKF_FORMAT_78",
"SOKF_FORMAT_79",
"SOKF_FORMAT_80",
"SOKF_FORMAT_96",
"SOKF_SCHEMA_26",
"SOKF_SCHEMA_51",
"TLMD_137",
"TLMD_138",
"TLMD_165",
];
pub(crate) fn fixable(f: &Finding) -> bool {
match f.criterion.as_str() {
"SOKF_FORMAT_85" => !f.message.contains("cannot be regenerated"),
"SOKF_FORMAT_73" => f.message.contains("uses the heading text"),
"SOKF_FORMAT_88" => f.message.contains("refers to an asset by path"),
"SOKF_FORMAT_97" => f.message.starts_with("the index is stale"),
"TLMD_109" => f.message.contains("its repair"),
key => ALWAYS.contains(&key),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{RepoPath, Severity};
fn finding(criterion: &str, message: &str) -> Finding {
Finding::new(
RepoPath::parse("/k/a.md").unwrap(),
Some(1),
Severity::Error,
criterion,
message,
)
}
#[test]
fn a_finding_is_fixable_by_its_key_and_variant() {
assert!(fixable(&finding(
"SOKF_FORMAT_85",
"`sokf:include a` is stale"
)));
assert!(!fixable(&finding(
"SOKF_FORMAT_85",
"`sokf:include a` cannot be regenerated: a contains an include that does not resolve"
)));
assert!(fixable(&finding(
"SOKF_FORMAT_80",
"the definition block is stale"
)));
assert!(fixable(&finding(
"SOKF_FORMAT_97",
"the index is stale: it differs from the generated content"
)));
assert!(!fixable(&finding(
"SOKF_FORMAT_97",
"the file is not a generated index, so `--fix` doesn't overwrite it: move its content into a document"
)));
assert!(fixable(&finding(
"SOKF_FORMAT_88",
"`sokf:include /a.png` refers to an asset by path; include it by its sidecar's id as `sokf:include a`"
)));
assert!(!fixable(&finding(
"SOKF_FORMAT_88",
"`sokf:include /k/b.md` refers to a document by path. Include its body by id as `sokf:include b`, or show its source in a code block"
)));
assert!(fixable(&finding(
"TLMD_109",
"key K has no anchor; its repair writes `<a id=\"k\"></a>` before it"
)));
assert!(!fixable(&finding(
"SOKF_FORMAT_61",
"`[sokf:planer]` resolves to no document"
)));
assert!(!fixable(&finding("TL_74", "`x` fails at column 1")));
assert!(fixable(&finding(
"SOKF_FORMAT_73",
"`#Retry once` uses the heading text. Use the slug `#retry-once` instead"
)));
assert!(!fixable(&finding(
"SOKF_FORMAT_73",
"`#D-1` matches no heading or key in this document"
)));
}
}