Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: redundant 'my first half of my life' etc. (#4684)
Andrew Dunbar2299fc1e6772
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex bc00a372f..8ab2d5127 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -3137,6 +3137,13 @@"label": "Discourse Markers"}},+ {+ "Bool": {+ "name": "DoublePossessiveNounPhrase",+ "state": true,+ "label": "Double Possessive Noun Phrase"+ }+ },{"Bool": {"name": "EverPronounRelPronoun",diff --git a/harper-core/src/linting/double_possessive_noun_phrase.rs b/harper-core/src/linting/double_possessive_noun_phrase.rsnew file mode 100644index 000000000..ba3567e74--- /dev/null+++ b/harper-core/src/linting/double_possessive_noun_phrase.rs@@ -0,0 +1,136 @@+use crate::{+ CharStringExt, Lint, Token,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct DoublePossessiveNounPhrase {+ expr: SequenceExpr,+}++impl Default for DoublePossessiveNounPhrase {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::default()+ .then_possessive_determiner()+ .t_ws()+ .then_adjective()+ .t_ws()+ .t_set([+ "column", "draft", "edition", "half", "line", "part", "point", "portion",+ "row", "section", "segment", "verse", "year",+ ])+ .t_ws()+ .t_aco("of")+ .t_ws()+ .then_possessive_determiner(),+ }+ }+}++impl ExprLinter for DoublePossessiveNounPhrase {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let (poss1, poss2) = (toks.first()?, toks.last()?);+ if !poss1.get_ch(src).eq_ch_lenient(poss2.get_ch(src)) {+ return None;+ }+ let span = poss1.span;++ Some(Lint {+ span,+ lint_kind: LintKind::Redundancy,+ suggestions: vec![Suggestion::replace_with_match_case(+ vec!['t', 'h', 'e'],+ span.get_content(src),+ )],+ message: "Avoid the redundant possessive by changing the first to `the`.".to_owned(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Detects redundant possessive constructions like `my latter half of my book`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ use super::DoublePossessiveNounPhrase;++ #[test]+ fn fix_my_first_part() {+ assert_suggestion_result(+ "This is my first part of my phrase.",+ DoublePossessiveNounPhrase::default(),+ "This is the first part of my phrase.",+ );+ }++ #[test]+ fn fix_your_second_half() {+ assert_suggestion_result(+ "As you helpfully noted in your second half of your comment, high wealth, deliberately low income",+ DoublePossessiveNounPhrase::default(),+ "As you helpfully noted in the second half of your comment, high wealth, deliberately low income",+ );+ }++ #[test]+ fn fix_his_latter() {+ assert_suggestion_result(+ "In his latter part of his life, he does talk of the thrill",+ DoublePossessiveNounPhrase::default(),+ "In the latter part of his life, he does talk of the thrill",+ );+ }++ #[test]+ fn fix_your_first() {+ assert_suggestion_result(+ "Use this bullet-point template to build out your first draft of your resume.",+ DoublePossessiveNounPhrase::default(),+ "Use this bullet-point template to build out the first draft of your resume.",+ );+ }++ #[test]+ fn fix_your_year() {+ assert_suggestion_result(+ "benefits like 50% off your first year of your subscription",+ DoublePossessiveNounPhrase::default(),+ "benefits like 50% off the first year of your subscription",+ );+ }++ #[test]+ fn dont_flag_inform_of() {+ assert_no_lints(+ "Nash can inform his former clients of his concerns",+ DoublePossessiveNounPhrase::default(),+ );+ }++ #[test]+ fn dont_flag_notify_of() {+ assert_no_lints(+ "it must notify your former spouse of his or her TCC rights",+ DoublePossessiveNounPhrase::default(),+ );+ }++ #[test]+ fn dont_flag_their_of_our() {+ assert_no_lints(+ "Ofgem published their final determination of our business plan.",+ DoublePossessiveNounPhrase::default(),+ );+ }+}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex c0ea9ddd2..ea0786af3 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -91,6 +91,7 @@ use super::do_mistake::DoMistake;use super::dot_initialisms::DotInitialisms;use super::double_click::DoubleClick;use super::double_modal::DoubleModal;+use super::double_possessive_noun_phrase::DoublePossessiveNounPhrase;use super::ellipsis_length::EllipsisLength;use super::else_possessive::ElsePossessive;use super::ever_every::EverEvery;@@ -725,6 +726,7 @@ impl LintGroup {insert_expr_rule!(DotInitialisms);insert_expr_rule!(DoubleClick);insert_expr_rule!(DoubleModal);+ insert_expr_rule!(DoublePossessiveNounPhrase);insert_struct_rule!(EllipsisLength);insert_expr_rule!(ElsePossessive);insert_expr_rule!(EverEvery);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 1f8496b43..fd49f7f64 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -91,6 +91,7 @@ mod do_mistake;mod dot_initialisms;mod double_click;mod double_modal;+mod double_possessive_noun_phrase;mod ellipsis_length;mod else_possessive;mod ever_every;