Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: a friend of me→friend of mine (#1679)
Andrew Dunbar71f9a1bc4a9a
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex e1f0bc8e9..02ec57e72 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -317,6 +317,7 @@ impl SequenceExpr {gen_then_from_is!(third_person_pronoun);gen_then_from_is!(third_person_singular_pronoun);gen_then_from_is!(third_person_plural_pronoun);+ gen_then_from_is!(object_pronoun);// Verbsdiff --git a/harper-core/src/linting/friend_of_me.rs b/harper-core/src/linting/friend_of_me.rsnew file mode 100644index 000000000..ce9a99c7d--- /dev/null+++ b/harper-core/src/linting/friend_of_me.rs@@ -0,0 +1,130 @@+use crate::{+ Token,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion},+};++pub struct FriendOfMe {+ expr: Box<dyn Expr>,+}++impl Default for FriendOfMe {+ fn default() -> Self {+ Self {+ expr: Box::new(+ SequenceExpr::word_set(&["friend", "friends", "enemy", "enemies"])+ .then_whitespace()+ .t_aco("of")+ .t_ws()+ .then_object_pronoun(),+ ),+ }+ }+}++impl ExprLinter for FriendOfMe {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let obj_pron_tok = toks.last()?;+ let obj_pron_str = obj_pron_tok.span.get_content_string(src);++ let poss_pron_str = match obj_pron_str.as_str() {+ "me" => "mine",+ "you" => "yours",+ "him" => "his",+ // "her" is also a possessive determiner, which leads to many false positives+ "her" => return None,+ "it" => return None,+ "us" => "ours",+ "them" => "theirs",+ _ => return None,+ };++ Some(Lint {+ span: obj_pron_tok.span,+ lint_kind: LintKind::Grammar,+ suggestions: vec![Suggestion::replace_with_match_case_str(+ poss_pron_str,+ obj_pron_tok.span.get_content(src),+ )],+ message: format!("Use `{poss_pron_str}` instead of `{obj_pron_str}`."),+ priority: 31,+ })+ }++ fn description(&self) -> &'static str {+ "Corrects wrong pronoun usage in constructions like `a friend of me`."+ }+}++#[cfg(test)]+mod tests {+ use super::FriendOfMe;+ use crate::linting::tests::assert_suggestion_result;++ #[test]+ fn corrects_friend_of_me() {+ assert_suggestion_result(+ "Last year a friend of me died unexpectedly (not a close friend).",+ FriendOfMe::default(),+ "Last year a friend of mine died unexpectedly (not a close friend).",+ );+ }++ #[test]+ fn corrects_friend_of_you() {+ assert_suggestion_result(+ "imagine a friend of you wants to disturb your call, and send you a session-terminate with a wrong SID",+ FriendOfMe::default(),+ "imagine a friend of yours wants to disturb your call, and send you a session-terminate with a wrong SID",+ );+ }++ #[test]+ fn corrects_friend_of_us() {+ assert_suggestion_result(+ "You have denounced a friend of us! You have denounced an enemy of us.",+ FriendOfMe::default(),+ "You have denounced a friend of ours! You have denounced an enemy of ours.",+ );+ }++ #[test]+ fn corrects_friends_of_them() {+ assert_suggestion_result(+ "guest has friend and they see which friends of them are comming",+ FriendOfMe::default(),+ "guest has friend and they see which friends of theirs are comming",+ );+ }++ #[test]+ fn corrects_friend_of_him() {+ assert_suggestion_result(+ "Ah, got it, i thought you may was a friend of him",+ FriendOfMe::default(),+ "Ah, got it, i thought you may was a friend of his",+ );+ }++ #[test]+ fn corrects_friends_of_me() {+ assert_suggestion_result(+ "guest has friend and they see which friends of me are comming",+ FriendOfMe::default(),+ "guest has friend and they see which friends of mine are comming",+ );+ }++ #[test]+ fn corrects_friends_of_us() {+ assert_suggestion_result(+ "This project was created for friends of us.",+ FriendOfMe::default(),+ "This project was created for friends of ours.",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 811847862..2cf336ce3 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -42,6 +42,7 @@ use super::few_units_of_time_ago::FewUnitsOfTimeAgo;use super::filler_words::FillerWords;use super::first_aid_kit::FirstAidKit;use super::for_noun::ForNoun;+use super::friend_of_me::FriendOfMe;use super::have_pronoun::HavePronoun;use super::have_take_a_look::HaveTakeALook;use super::hedging::Hedging;@@ -426,6 +427,7 @@ impl LintGroup {insert_expr_rule!(FillerWords, true);insert_struct_rule!(FirstAidKit, true);insert_struct_rule!(ForNoun, true);+ insert_expr_rule!(FriendOfMe, true);insert_expr_rule!(HavePronoun, true);insert_expr_rule!(Hedging, true);insert_expr_rule!(Hereby, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 5d95ca6b5..ef6c858a8 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -36,6 +36,7 @@ mod few_units_of_time_ago;mod filler_words;mod first_aid_kit;mod for_noun;+mod friend_of_me;mod have_pronoun;mod have_take_a_look;mod hedging;@@ -161,6 +162,7 @@ pub use expr_linter::ExprLinter;pub use few_units_of_time_ago::FewUnitsOfTimeAgo;pub use filler_words::FillerWords;pub use for_noun::ForNoun;+pub use friend_of_me::FriendOfMe;pub use have_pronoun::HavePronoun;pub use have_take_a_look::HaveTakeALook;pub use hedging::Hedging;