Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): `OfCourse` rule
Elijah Potter3840d26ed12c
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 38fdd84ff..87ba77291 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -43,6 +43,7 @@ use super::modal_of::ModalOf;use super::multiple_sequential_pronouns::MultipleSequentialPronouns;use super::nobody::Nobody;use super::number_suffix_capitalization::NumberSuffixCapitalization;+use super::of_course::OfCourse;use super::out_of_date::OutOfDate;use super::oxymorons::Oxymorons;use super::pattern_linter::run_on_chunk;@@ -342,6 +343,7 @@ impl LintGroup {insert_struct_rule!(LetsConfusion, true);insert_pattern_rule!(DespiteOf, true);insert_pattern_rule!(ChockFull, true);+ insert_struct_rule!(OfCourse, true);insert_struct_rule!(FirstAidKit, true);insert_struct_rule!(PronounKnew, true);insert_struct_rule!(TheHowWhy, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 51bc9d046..6dc6d8653 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -45,6 +45,7 @@ mod multiple_sequential_pronouns;mod no_oxford_comma;mod nobody;mod number_suffix_capitalization;+mod of_course;mod out_of_date;mod oxford_comma;mod oxymorons;@@ -110,6 +111,7 @@ pub use multiple_sequential_pronouns::MultipleSequentialPronouns;pub use no_oxford_comma::NoOxfordComma;pub use nobody::Nobody;pub use number_suffix_capitalization::NumberSuffixCapitalization;+pub use of_course::OfCourse;pub use out_of_date::OutOfDate;pub use oxford_comma::OxfordComma;pub use oxymorons::Oxymorons;diff --git a/harper-core/src/linting/of_course.rs b/harper-core/src/linting/of_course.rsnew file mode 100644index 000000000..6f6bacdfc--- /dev/null+++ b/harper-core/src/linting/of_course.rs@@ -0,0 +1,112 @@+use crate::{+ Token,+ linting::{Lint, LintKind, PatternLinter, Suggestion},+ patterns::{SequencePattern, WordSet},+};++pub struct OfCourse {+ pattern: Box<dyn crate::patterns::Pattern>,+}++impl Default for OfCourse {+ fn default() -> Self {+ let wrong_forms = WordSet::new(&["curse", "corse"]);+ let pattern = SequencePattern::default()+ .t_aco("of")+ .then_whitespace()+ .then(wrong_forms);++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for OfCourse {+ fn pattern(&self) -> &dyn crate::patterns::Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched: &[Token], source: &[char]) -> Option<Lint> {+ // Skip if the word before “of” is “kind” or “sort” → “kind of curse” is valid.+ if let Some(of_idx) = matched.first().map(|t| t.span.start) {+ if let Some(prev) = source.get(..of_idx).and_then(|src| {+ // Walk backwards over whitespace to find the preceding word token.+ let mut i = of_idx.saturating_sub(1);+ while i > 0 && src[i].is_whitespace() {+ i -= 1;+ }+ // Return slice ending with that char to build a small string.+ let start = src[..=i]+ .iter()+ .rposition(|c| c.is_whitespace())+ .map(|p| p + 1)+ .unwrap_or(0);+ Some(src[start..=i].iter().collect::<String>())+ }) {+ let lower = prev.to_ascii_lowercase();+ if lower == "kind" || lower == "sort" {+ return None;+ }+ }+ }++ let typo_span = matched.last()?.span;+ let original = typo_span.get_content(source);++ Some(Lint {+ span: typo_span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ "course".chars().collect(),+ original,+ )],+ message: "Did you mean “of **course**” (= clearly) instead of “of curse / corse”?"+ .to_string(),+ priority: 31,+ })+ }++ fn description(&self) -> &str {+ "Corrects the common eggcorn “of curse / corse” to “of course,” ignoring phrases like “kind of curse.”"+ }+}++#[cfg(test)]+mod tests {+ use super::OfCourse;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn flags_of_curse() {+ assert_suggestion_result("Yes, of curse!", OfCourse::default(), "Yes, of course!");+ }++ #[test]+ fn flags_of_corse() {+ assert_suggestion_result(+ "Well, of corse we can.",+ OfCourse::default(),+ "Well, of course we can.",+ );+ }++ #[test]+ fn ignores_kind_of_curse() {+ assert_lint_count("This kind of curse is dangerous.", OfCourse::default(), 0);+ }++ #[test]+ fn ignores_sort_of_curse() {+ assert_lint_count("It's a sort of curse that lingers.", OfCourse::default(), 0);+ }++ #[test]+ fn ignores_curse_of_title() {+ assert_lint_count(+ "The Curse of Strahd is a famous module.",+ OfCourse::default(),+ 0,+ );+ }+}