Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: correct wrong variants of welcome/greet with open arms (#4241)
Andrew Dunbar3c1cd106917a
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex f7734cf4d..862d90fc7 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -5733,6 +5733,13 @@"label": "Wish Could"}},+ {+ "Bool": {+ "name": "WithOpenArms",+ "state": true,+ "label": "With Open Arms"+ }+ },{"Bool": {"name": "WorthToDo",diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 0e0da0759..aa4097dc9 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -314,6 +314,7 @@ use super::widely_accepted::WidelyAccepted;use super::will_non_lemma::WillNonLemma;use super::win_prize::WinPrize;use super::wish_could::WishCould;+use super::with_open_arms::WithOpenArms;use super::wordpress_dotcom::WordPressDotcom;use super::worth_to_do::WorthToDo;use super::would_never_have::WouldNeverHave;@@ -910,6 +911,7 @@ impl LintGroup {insert_expr_rule_with_dict!(WillNonLemma);insert_expr_rule!(WinPrize);insert_expr_rule!(WishCould);+ insert_expr_rule!(WithOpenArms);insert_struct_rule!(WordPressDotcom);insert_expr_rule_with_dict!(WorthToDo);insert_expr_rule!(WouldNeverHave);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex a478d445c..f6a75b31f 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -332,6 +332,7 @@ mod widely_accepted;mod will_non_lemma;mod win_prize;mod wish_could;+mod with_open_arms;mod wordpress_dotcom;mod worth_to_do;mod would_never_have;diff --git a/harper-core/src/linting/with_open_arms.rs b/harper-core/src/linting/with_open_arms.rsnew file mode 100644index 000000000..697f35fa5--- /dev/null+++ b/harper-core/src/linting/with_open_arms.rs@@ -0,0 +1,174 @@+use crate::{+ CharStringExt, Lint, Token, TokenStringExt,+ expr::{All, Expr, OwnedExprExt, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, debug::format_lint_match, expr_linter::Chunk},+ patterns::{InflectionOfBe, WordSet},+};++pub struct WithOpenArms {+ expr: All,+}++impl Default for WithOpenArms {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::any_of([+ Box::new(InflectionOfBe::default()) as Box<dyn Expr>,+ Box::new(WordSet::new(&["get", "gets", "getting", "got", "gotten"])),+ ])+ .t_ws()+ .t_set(&["greet", "greeted", "welcome", "welcomed"])+ .t_ws()+ .t_aco("with")+ .t_ws()+ .t_set(&["open", "opened"])+ .t_ws()+ .t_set(&["arm", "arms"])+ .but_not(+ SequenceExpr::word_set(&["greeted", "welcomed"])+ .t_any()+ .t_any()+ .t_any()+ .t_aco("open")+ .t_any()+ .t_aco("arms"),+ ),+ }+ }+}++impl ExprLinter for WithOpenArms {+ type Unit = Chunk;++ fn match_to_lint_with_context(+ &self,+ toks: &[Token],+ src: &[char],+ ctx: Option<(&[Token], &[Token])>,+ ) -> Option<Lint> {+ eprintln!("🚨 {}", format_lint_match(toks, ctx, src));+ let [verb, ws_with_ws @ .., open, ws4, arms] = &toks[2..] else {+ return None;+ };++ let (verb_ch, open_ch, arms_ch) = (verb.get_ch(src), open.get_ch(src), arms.get_ch(src));++ let not_passive = !verb_ch.ends_with_ignore_ascii_case_chars(&['d']);+ let open_past = open_ch.ends_with_ignore_ascii_case_chars(&['d']);+ let arms_singular = !arms_ch.ends_with_ignore_ascii_case_chars(&['s']);++ let verb: Vec<char> = if not_passive {+ if verb_ch.starts_with_ignore_ascii_case_str("g") {+ let mut greet = verb_ch.to_vec();+ let t_to_e_delta = ('t' as i32) - ('e' as i32);++ if let [.., t] = greet.as_slice() {+ let e = ((*t as i32) - t_to_e_delta) as u8 as char;+ let d = ((e as i32) - 1) as u8 as char;+ greet.extend([e, d])+ }+ greet+ } else {+ let mut welcome = verb_ch.to_vec();+ if let [.., e] = welcome.as_slice() {+ let d = ((*e as i32) - 1) as u8 as char;+ welcome.push(d);+ }+ welcome+ }+ } else {+ verb_ch.to_vec()+ };++ let open: Vec<char> = if open_past {+ open_ch[..open_ch.len() - 2].to_vec()+ } else {+ open_ch.to_vec()+ };++ let arms: Vec<char> = if arms_singular {+ let mut arm = arms_ch.to_vec();+ if let [.., m] = arm.as_slice() {+ let m_to_s_delta = ('s' as i32) - ('m' as i32);+ let s = ((*m as i32) + m_to_s_delta) as u8 as char;+ arm.push(s);+ }+ arm+ } else {+ arms_ch.to_vec()+ };++ let correction: Vec<char> = verb+ .into_iter()+ .chain(ws_with_ws.get_ch(src)?.iter().copied())+ .chain(open)+ .chain(ws4.get_ch(src).iter().copied())+ .chain(arms)+ .collect();++ let message = if not_passive {+ "The idiom `with open arms` requires the passive mood, so the verb must be in the past tense."+ } else {+ "The correct form of the idiom is `with open arms`."+ }.to_owned();++ Some(Lint {+ span: toks[2..].span()?,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::ReplaceWith(correction)],+ message,+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects wrong variants of the idiom `welcome/greet with open arms`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::WithOpenArms;++ #[test]+ fn fix_welcome() {+ assert_suggestion_result(+ "Contributions to this project are welcome with open arms!",+ WithOpenArms::default(),+ "Contributions to this project are welcomed with open arms!",+ );+ }++ #[test]+ fn fix_opened() {+ assert_suggestion_result(+ "This PR will be welcomed with opened arms <3.",+ WithOpenArms::default(),+ "This PR will be welcomed with open arms <3.",+ );+ }++ #[test]+ fn fix_arm() {+ assert_suggestion_result(+ "there are no maxis and Vitalk is welcomed with open arm in to building on top of Bitcoin",+ WithOpenArms::default(),+ "there are no maxis and Vitalk is welcomed with open arms in to building on top of Bitcoin",+ )+ }++ #[test]+ fn fix_greet_and_arm() {+ assert_suggestion_result(+ "I'm excited to be greet with open arm and the same eagerness and affection that has always awaited me",+ WithOpenArms::default(),+ "I'm excited to be greeted with open arms and the same eagerness and affection that has always awaited me",+ );+ }+}