Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: add fixes for bugs #578 and #724
hippietrail115716b0a4d2
diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rsindex aa745e389..c300bdb49 100644--- a/harper-core/src/linting/multiple_sequential_pronouns.rs+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs@@ -2,12 +2,16 @@ use super::Suggestion;use super::pattern_linter::PatternLinter;use crate::linting::LintKind;use crate::patterns::{Pattern, SequencePattern, WordSet};-use crate::{Lint, Lrc, Token, TokenStringExt};+use crate::{CharStringExt, Lint, Lrc, Token, TokenStringExt};+use std::collections::HashSet;/// Linter that checks if multiple pronouns are being used right after each/// other. This is a common mistake to make during the revision process.pub struct MultipleSequentialPronouns {pattern: Box<dyn Pattern>,+ subject_pronouns: HashSet<&'static str>,+ object_pronouns: HashSet<&'static str>,+ possessive_adjectives: HashSet<&'static str>,}impl MultipleSequentialPronouns {@@ -25,6 +29,22 @@ impl MultipleSequentialPronouns {"our", "your", "their", // possessive adjectives, plural]));+ // TODO: temporary sets of pronouns - remove when WordMetadata has this info+ let subject_pronouns = HashSet::from([+ "I", "you", "he", "she", "it", // subject case, singular+ "we", "you", "they", // subject case, plural+ ]);++ let object_pronouns = HashSet::from([+ "me", "you", "him", "her", "it", // object case, singular+ "us", "you", "them", // object case, plural+ ]);++ let possessive_adjectives = HashSet::from([+ "my", "your", "his", "her", "its", // possessive adjectives, singular+ "our", "your", "their", // possessive adjectives, plural+ ]);+Self {pattern: Box::new(SequencePattern::default()@@ -35,8 +55,23 @@ impl MultipleSequentialPronouns {.then(pronouns.clone()),),),+ subject_pronouns,+ object_pronouns,+ possessive_adjectives,}}++ fn is_subject_pronoun(&self, word: &str) -> bool {+ self.subject_pronouns.contains(word)+ }++ fn is_object_pronoun(&self, word: &str) -> bool {+ self.object_pronouns.contains(word)+ }++ fn is_possessive_adjective(&self, word: &str) -> bool {+ self.possessive_adjectives.contains(word)+ }}impl PatternLinter for MultipleSequentialPronouns {@@ -48,6 +83,17 @@ impl PatternLinter for MultipleSequentialPronouns {let mut suggestions = Vec::new();if matched_tokens.len() == 3 {+ let first_word = matched_tokens[0].span.get_content(source).to_string();+ let second_word = matched_tokens[2].span.get_content(source).to_string();+ // Bug 578: "I can lend you my car" - if 1st is object and second is possessive adjective, don't lint+ if self.is_object_pronoun(&first_word) && self.is_possessive_adjective(&second_word) {+ return None;+ }+ // Bug 724: "One told me they were able to begin reading" - if 1st is object ans second is subject, don't lint+ if self.is_object_pronoun(&first_word) && self.is_subject_pronoun(&second_word) {+ return None;+ }+suggestions.push(Suggestion::ReplaceWith(matched_tokens[0].span.get_content(source).to_vec(),));@@ -111,7 +157,7 @@ mod tests {#[test]fn detects_multiple_pronouns_at_end() {assert_lint_count(- "...little bit about I want to do to me you.",+ "...I need to explain this to you them.",MultipleSequentialPronouns::new(),1,)@@ -121,4 +167,22 @@ mod tests {fn comma_separated() {assert_lint_count("To prove it, we...", MultipleSequentialPronouns::new(), 0)}++ #[test]+ fn dont_flag_578() {+ assert_lint_count(+ "I can lend you my car.",+ MultipleSequentialPronouns::new(),+ 0,+ )+ }++ #[test]+ fn dont_flag_724() {+ assert_lint_count(+ "One told me they were able to begin reading.",+ MultipleSequentialPronouns::new(),+ 0,+ )+ }}