diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 9765aa5..e265c38 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -61,6 +61,7 @@ A repository adopts the standard in four steps: - `pr-template`: `.github/pull_request_template.md` has the headings `## What and why` and `## Testing`. - `docs-summary`: With a `book.toml`, every Markdown page under its `src` is linked from its `SUMMARY.md`, or included in a page with `{{#include …}}`. - `docs-images`: Every image in a Markdown file, outside code blocks, has alt text: `![what it shows](…)`, or an `` with a non-empty `alt`. +- `wording`: No Markdown file, outside code, holds as a word a spelling `standard.toml`'s `[wording]` rejects, such as `"Source Origin" = "SourceOrigin"`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and @@ -102,6 +103,10 @@ doc-lines = 10 block-lines = 6 file-lines = 40 +# Spellings the docs reject, each with the one to write instead. +[wording] +"Source Origin" = "SourceOrigin" + [check-changed] # Crates that build another without depending on it, such as a build # script compiling a crate it embeds: a change to `built` reaches `builder`. diff --git a/crates/sourceorigin-standard/src/config/mod.rs b/crates/sourceorigin-standard/src/config/mod.rs index ae52bd4..42b55c3 100644 --- a/crates/sourceorigin-standard/src/config/mod.rs +++ b/crates/sourceorigin-standard/src/config/mod.rs @@ -81,6 +81,9 @@ pub(crate) struct Config { /// The lines of workflows' `run:` that may run Cargo rather than a /// recipe. pub(crate) cargo_in_ci: Vec, + /// The `[wording]` table: each rejected spelling, with its line, and + /// the one to write instead. + pub(crate) wording: Vec<(Listed, String)>, } /// The `[comments]` table: what comments the rules allow. @@ -231,6 +234,7 @@ struct File { comments: Comments, anyhow_crates: Vec>, cargo_in_ci: Vec>, + wording: BTreeMap, String>, } /// A deviation as written: its reason alone, for everywhere, or a table @@ -361,6 +365,15 @@ impl Config { comments: file.comments, anyhow_crates: listed(file.anyhow_crates), cargo_in_ci: listed(file.cargo_in_ci), + wording: file + .wording + .into_iter() + .map(|(rejected, preferred)| { + let line = line_of(text, rejected.span().start); + let value = rejected.into_inner(); + (Listed { value, line }, preferred) + }) + .collect(), }) } diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 14d076a..0749911 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -43,6 +43,7 @@ mod text_file; mod toml_file; mod toolchain; mod unsafe_modules; +mod wording; mod workflow_permissions; mod workflows; mod workspace_dependencies; @@ -257,6 +258,10 @@ rules! { /// Every image in a Markdown file, outside code blocks, has alt text: /// `![what it shows](…)`, or an `` with a non-empty `alt`. DocsImages = "docs-images" => docs_images, + /// No Markdown file, outside code, holds as a word a spelling + /// `standard.toml`'s `[wording]` rejects, such as + /// `"Source Origin" = "SourceOrigin"`. + Wording = "wording" => wording, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index cb81f6d..aa0e1d2 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -29,6 +29,7 @@ mod scheduled_audit; mod t3_setup; mod toolchain; mod unsafe_modules; +mod wording; mod workflow_permissions; mod workspace_dependencies; mod workspace_lints; diff --git a/crates/sourceorigin-standard/src/rules/tests/wording.rs b/crates/sourceorigin-standard/src/rules/tests/wording.rs new file mode 100644 index 0000000..9081e0b --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/wording.rs @@ -0,0 +1,34 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn the_docs_use_the_names_the_repository_prefers() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write( + "standard.toml", + "[wording]\n\"Source Origin\" = \"SourceOrigin\"\n\"Origin auth\" = \"Origin Auth\"\n", + ) + .write( + "docs/guide.md", + "# Source Origin\n\nSign in with Origin auth, which `Source Origin` names.\n\ + Origin authority is a word of its own.\n\n```\nSource Origin\n```\n", + ); + assert_eq!( + fixture.violations(Rule::Wording), + [ + "docs/guide.md:1: write `SourceOrigin`, not `Source Origin` (wording)", + "docs/guide.md:3: write `Origin Auth`, not `Origin auth` (wording)", + ] + ); + fixture.fix_rule(Rule::Wording); + let guide = + std::fs::read_to_string(fixture.0.root().join("docs/guide.md")).expect("the page is read"); + assert!( + guide.starts_with( + "# SourceOrigin\n\nSign in with Origin Auth, which `Source Origin` names.\n" + ), + "{guide}" + ); +} diff --git a/crates/sourceorigin-standard/src/rules/wording.rs b/crates/sourceorigin-standard/src/rules/wording.rs new file mode 100644 index 0000000..3b96560 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/wording.rs @@ -0,0 +1,52 @@ +//! `wording`: the docs name things as the repository does. + +use super::fix::{Checked, Fix}; +use super::{Finding, Repository}; + +impl Repository { + /// Each line of a Markdown file holding a spelling `[wording]` rejects + /// as a word, outside code, fixed by writing the preferred one there. + pub(super) fn wording(&self) -> Checked { + let holds = |text: &str, word: &str| { + text.match_indices(word).any(|(at, _)| { + let before = text[..at].chars().next_back(); + let after = text[at + word.len()..].chars().next(); + !before.is_some_and(char::is_alphanumeric) + && !after.is_some_and(char::is_alphanumeric) + }) + }; + let mut checked = Checked::default(); + for file in &self.markdown { + for (line, prose) in file.prose_lines() { + let rejected: Vec<_> = self + .config + .wording + .iter() + .filter(|(rejected, _)| holds(&prose, &rejected.value)) + .collect(); + if rejected.is_empty() { + continue; + } + let original = file.text.lines().nth(line - 1).unwrap_or_default(); + let mut fixed: Vec = original.split('`').map(str::to_owned).collect(); + for (rejected, preferred) in rejected { + checked.findings.push(Finding { + path: file.path.clone(), + line, + message: format!("write `{preferred}`, not `{}`", rejected.value), + }); + for outside_code in fixed.iter_mut().step_by(2) { + *outside_code = outside_code.replace(&rejected.value, preferred); + } + } + checked.fixes.push(Fix::line( + file.path.clone(), + format!("write the names `[wording]` prefers on line {line}"), + line, + fixed.join("`"), + )); + } + } + checked + } +}