Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<img>` 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"`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down Expand Up @@ -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`.
Expand Down
13 changes: 13 additions & 0 deletions crates/sourceorigin-standard/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Listed>,
/// 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.
Expand Down Expand Up @@ -231,6 +234,7 @@ struct File {
comments: Comments,
anyhow_crates: Vec<Spanned<String>>,
cargo_in_ci: Vec<Spanned<String>>,
wording: BTreeMap<Spanned<String>, String>,
}

/// A deviation as written: its reason alone, for everywhere, or a table
Expand Down Expand Up @@ -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(),
})
}

Expand Down
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -257,6 +258,10 @@ rules! {
/// Every image in a Markdown file, outside code blocks, has alt text:
/// `![what it shows](…)`, or an `<img>` 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.
Expand Down
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/wording.rs
Original file line number Diff line number Diff line change
@@ -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}"
);
}
52 changes: 52 additions & 0 deletions crates/sourceorigin-standard/src/rules/wording.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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
}
}
Loading