Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: implement false positive "the great might of"
hippietrail62bed769bbb6
diff --git a/harper-core/src/linting/modal_of.rs b/harper-core/src/linting/modal_of.rsindex 15d7bc844..10e4cf029 100644--- a/harper-core/src/linting/modal_of.rs+++ b/harper-core/src/linting/modal_of.rs@@ -1,6 +1,6 @@use crate::{Lrc, Token, TokenStringExt,- patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},+ patterns::{EitherPattern, Pattern, SequencePattern, WordSet},};use super::{Lint, LintKind, PatternLinter, Suggestion};@@ -34,12 +34,35 @@ impl Default for ModalOf {.then_exact_word("course"),);+ let modal_of_course = Lrc::new(+ SequencePattern::default()+ .then(modal_of.clone())+ .then(ws_course.clone()),+ );++ let anyword_might_of = Lrc::new(+ SequencePattern::default()+ .then_any_word()+ .then_whitespace()+ .then_exact_word("might")+ .then_whitespace()+ .then_exact_word("of"),+ );++ let anyword_might_of_course = Lrc::new(+ SequencePattern::default()+ .then(anyword_might_of.clone())+ .then(ws_course.clone()),+ );+Self {pattern: Box::new(- SequencePattern::default()- .then(modal_of.clone())- .then(ws_course.clone())- .or(Box::new(modal_of.clone())),+ EitherPattern::new(vec![+ Box::new(anyword_might_of_course),+ Box::new(modal_of_course),+ Box::new(anyword_might_of),+ Box::new(modal_of),+ ])),}}@@ -51,25 +74,45 @@ impl PatternLinter for ModalOf {}fn match_to_lint(&self, matched_toks: &[Token], source_chars: &[char]) -> Option<Lint> {- if matched_toks.len() != 3 {- return None;- }-- let span_modal_of = matched_toks[0..3].span().unwrap();- let span_modal = matched_toks[0].span;-- let modal_have = format!("{} have", span_modal.get_content_string(source_chars))+ let modal_index = match matched_toks.len() {+ // Without context, always an error from the start+ 3 => 0,+ 5 => {+ // False positives: modal _ of _ course / adj. _ might _ of / art. _ might _ of+ let w3_text = matched_toks.last().unwrap().span.get_content(source_chars).iter().collect::<String>();+ if w3_text.as_str() != "of" {+ return None;+ }+ let w1_kind = matched_toks.first().unwrap().kind;+ // the might of something, great might of something+ if w1_kind.is_adjective() || w1_kind.is_article() {+ return None;+ }+ // not a false positive, skip context before+ 2+ }+ // False positive: <word> _ might _ of _ course+ 7 => return None,+ _ => unreachable!(),+ };++ let span_modal_of = matched_toks[modal_index..modal_index + 3].span().unwrap();++ let modal_have = format!("{} have", matched_toks[modal_index].span.get_content_string(source_chars)).chars().collect();- let modal_ws_of = span_modal_of.get_content(source_chars);- Some(Lint {+ return Some(Lint {span: span_modal_of,lint_kind: LintKind::WordChoice,- suggestions: vec![Suggestion::replace_with_match_case(modal_have, modal_ws_of)],+ suggestions: vec![Suggestion::replace_with_match_case(+ modal_have,+ span_modal_of.get_content(source_chars),+ )],message: "Use `have` rather than `of` here.".to_string(),priority: 126,- })+ });+}fn description(&self) -> &'static str {@@ -100,10 +143,20 @@ mod tests {}#[test]- fn test_false_positive() {+ fn test_false_positive_of_course() {assert_lint_count("should of course", ModalOf::default(), 0);}+ #[test]+ fn test_false_positive_the_might_of() {+ assert_lint_count("the might of", ModalOf::default(), 0);+ }++ #[test]+ fn test_false_positive_great_might_of() {+ assert_lint_count("great might of", ModalOf::default(), 0);+ }+#[test]fn test_false_positive_capital_negative() {assert_lint_count("Wouldn't of course", ModalOf::default(), 0);@@ -227,4 +280,13 @@ mod tests {0,);}++ #[test]+ fn doesnt_catch_to_take_on_the_full_might_of_nato() {+ assert_lint_count(+ "To take on the full might of NATO.",+ ModalOf::default(),+ 0,+ );+ }}