Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(lint): add `LookingForwardTo` rule (#1567)
thuan141255a475eface6
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex ea09c2779..0cd9d0424 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -54,6 +54,7 @@ use super::less_worse::LessWorse;use super::lets_confusion::LetsConfusion;use super::likewise::Likewise;use super::long_sentences::LongSentences;+use super::looking_forward_to::LookingForwardTo;use super::merge_words::MergeWords;use super::missing_preposition::MissingPreposition;use super::modal_of::ModalOf;@@ -428,6 +429,7 @@ impl LintGroup {insert_struct_rule!(LetsConfusion, true);insert_expr_rule!(Likewise, true);insert_struct_rule!(LongSentences, true);+ insert_expr_rule!(LookingForwardTo, true);insert_struct_rule!(MergeWords, true);insert_expr_rule!(ModalOf, true);insert_expr_rule!(MostNumber, true);diff --git a/harper-core/src/linting/looking_forward_to.rs b/harper-core/src/linting/looking_forward_to.rsnew file mode 100644index 000000000..25943f601--- /dev/null+++ b/harper-core/src/linting/looking_forward_to.rs@@ -0,0 +1,137 @@+use hashbrown::HashSet;++use crate::{+ Token,+ expr::{Expr, FixedPhrase, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion},+};++pub struct LookingForwardTo {+ expr: Box<dyn Expr>,+}++impl Default for LookingForwardTo {+ fn default() -> Self {+ let looking_forward_to = FixedPhrase::from_phrase("looking forward to");++ let pattern = SequenceExpr::default()+ .then(looking_forward_to)+ .t_ws()+ // TODO: update the use the verb with progressive tense function later+ .then_verb();++ Self {+ expr: Box::new(pattern),+ }+ }+}++impl ExprLinter for LookingForwardTo {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], src: &[char]) -> Option<Lint> {+ let span = matched_tokens.last()?.span;+ let verb = matched_tokens.last()?.span.get_content_string(src);+ if verb.ends_with("ing") {+ return None;+ }++ // TODO: create a util function to handle the appending of -ing+ // to verbs, taking into account exceptions and irregular forms.+ let exception_word: HashSet<&str> = [+ // Verbs ending in -ee+ "see",+ "flee",+ "agree",+ "knee",+ "guarantee",+ // Verbs ending in -oe+ "hoe",+ "toe",+ // Verbs ending in -ye or to avoid confusion+ "dye",+ "eye",+ // Irregular/spelling clarification+ "singe",+ "tinge",+ ]+ .iter()+ .cloned()+ .collect();++ let gerund_form: String =+ if verb.to_lowercase().ends_with('e') && !exception_word.contains(verb.as_str()) {+ verb.trim_end_matches('e').to_string() + "ing"+ } else {+ format!("{verb}ing")+ };++ println!("gerund_form: -{gerund_form}- -- verb: -{verb}-");+ Some(Lint {+ span,+ lint_kind: LintKind::WordChoice,+ message: format!(+ "The verb `{verb}` must be in the gerund form (verb + -ing) after 'looking forward to'.",+ ),+ suggestions: vec![Suggestion::replace_with_match_case(+ gerund_form.chars().collect(),+ span.get_content(src),+ )],+ ..Default::default()+ })+ }++ fn description(&self) -> &'static str {+ "This rule identifies instances where the phrase `looking forward to` is followed by a base form verb instead of the required gerund (verb + `-ing` form)."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ use super::LookingForwardTo;++ #[test]+ fn not_lint_with_correct_verb() {+ assert_suggestion_result(+ "She was looking forward to see the grandchildren again.",+ LookingForwardTo::default(),+ "She was looking forward to seeing the grandchildren again.",+ );+ // assert_lint_count(+ // "She was looking forward to seeing the grandchildren again.",+ // LookingForwardTo::default(),+ // 0,+ // );+ }++ #[test]+ fn lint_with_incorrect_verb() {+ assert_suggestion_result(+ "She was looking forward to see the grandchildren again.",+ LookingForwardTo::default(),+ "She was looking forward to seeing the grandchildren again.",+ );+ }++ #[test]+ fn lint_with_incorrect_verb_ending_in_e() {+ assert_suggestion_result(+ "She was looking forward to make the grandchildren happy.",+ LookingForwardTo::default(),+ "She was looking forward to making the grandchildren happy.",+ );+ }++ #[test]+ fn not_lint_with_non_verb() {+ assert_lint_count(+ "She was looking forward to the grandchildren's visit.",+ LookingForwardTo::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex f19382b5c..99dbc7066 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -56,6 +56,7 @@ mod lint;mod lint_group;mod lint_kind;mod long_sentences;+mod looking_forward_to;mod map_phrase_linter;mod map_phrase_set_linter;mod merge_linters;@@ -167,6 +168,7 @@ pub use lint::Lint;pub use lint_group::{LintGroup, LintGroupConfig};pub use lint_kind::LintKind;pub use long_sentences::LongSentences;+pub use looking_forward_to::LookingForwardTo;pub use map_phrase_linter::MapPhraseLinter;pub use map_phrase_set_linter::MapPhraseSetLinter;pub use merge_words::MergeWords;