Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add `PronounKnew` rule
Elijah Potter22d469237eb2
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 8cc8921fb..200578c22 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -48,6 +48,7 @@ use super::pattern_linter::run_on_chunk;use super::pique_interest::PiqueInterest;use super::possessive_your::PossessiveYour;use super::pronoun_contraction::PronounContraction;+use super::pronoun_knew::PronounKnew;use super::proper_noun_capitalization_linters;use super::repeated_words::RepeatedWords;use super::sentence_capitalization::SentenceCapitalization;@@ -340,6 +341,7 @@ impl LintGroup {insert_struct_rule!(LetsConfusion, true);insert_pattern_rule!(DespiteOf, true);insert_pattern_rule!(ChockFull, true);+ insert_struct_rule!(PronounKnew, true);insert_struct_rule!(TheHowWhy, true);insert_struct_rule!(WidelyAccepted, true);insert_pattern_rule!(Confident, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 39269abab..787b27474 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -50,6 +50,7 @@ mod phrase_corrections;mod pique_interest;mod possessive_your;mod pronoun_contraction;+mod pronoun_knew;mod proper_noun_capitalization_linters;mod repeated_words;mod sentence_capitalization;@@ -113,6 +114,7 @@ pub use pattern_linter::PatternLinter;pub use pique_interest::PiqueInterest;pub use possessive_your::PossessiveYour;pub use pronoun_contraction::PronounContraction;+pub use pronoun_knew::PronounKnew;pub use repeated_words::RepeatedWords;pub use sentence_capitalization::SentenceCapitalization;pub use somewhat_something::SomewhatSomething;diff --git a/harper-core/src/linting/pronoun_knew.rs b/harper-core/src/linting/pronoun_knew.rsnew file mode 100644index 000000000..3721368fb--- /dev/null+++ b/harper-core/src/linting/pronoun_knew.rs@@ -0,0 +1,95 @@+use crate::{+ Token,+ linting::{Lint, LintKind, PatternLinter, Suggestion},+ patterns::{EitherPattern, SequencePattern, WordSet},+};++pub struct PronounKnew {+ pattern: Box<dyn crate::patterns::Pattern>,+}++impl Default for PronounKnew {+ fn default() -> Self {+ let pronoun_then_new = SequencePattern::default()+ .then_pronoun()+ .then_whitespace()+ .then_any_capitalization_of("new");++ let pronoun_adverb_then_new = SequencePattern::default()+ .then_pronoun()+ .then_whitespace()+ .then(WordSet::new(&["always", "never", "also", "often"]))+ .then_whitespace()+ .then_any_capitalization_of("new");++ let combined_pattern = EitherPattern::new(vec![+ Box::new(pronoun_then_new),+ Box::new(pronoun_adverb_then_new),+ ]);++ Self {+ pattern: Box::new(combined_pattern),+ }+ }+}++impl PatternLinter for PronounKnew {+ fn pattern(&self) -> &dyn crate::patterns::Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, tokens: &[Token], source: &[char]) -> Option<Lint> {+ let typo_token = tokens.last()?;+ let typo_span = typo_token.span;+ let typo_text = typo_span.get_content(source);++ Some(Lint {+ span: typo_span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ "knew".chars().collect(),+ typo_text,+ )],+ message: "Did you mean “knew” (the past tense of “know”)?".to_string(),+ priority: 31,+ })+ }++ fn description(&self) -> &str {+ "Detects when “new” following a pronoun (optionally with an adverb) is a typo for the past tense “knew.”"+ }+}++#[cfg(test)]+mod tests {+ use super::PronounKnew;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn simple_pronoun_new() {+ assert_suggestion_result(+ "I new you would say that.",+ PronounKnew::default(),+ "I knew you would say that.",+ );+ }++ #[test]+ fn with_adverb() {+ assert_suggestion_result(+ "She often new the answer.",+ PronounKnew::default(),+ "She often knew the answer.",+ );+ }++ #[test]+ fn does_not_flag_without_pronoun() {+ assert_lint_count("The software is new.", PronounKnew::default(), 0);+ }++ #[test]+ fn does_not_flag_other_context() {+ assert_lint_count("They called it \"new\".", PronounKnew::default(), 0);+ }+}