Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: redundant "after <duration> later" (#2146)
Andrew Dunbar05338099c626
diff --git a/harper-core/src/expr/duration_expr.rs b/harper-core/src/expr/duration_expr.rsindex 3e0c3fd59..b829c9d46 100644--- a/harper-core/src/expr/duration_expr.rs+++ b/harper-core/src/expr/duration_expr.rs@@ -1,4 +1,4 @@-use crate::patterns::WordSet;+use crate::patterns::{IndefiniteArticle, WordSet};use crate::{Span, Token};use super::{Expr, SequenceExpr, SpelledNumberExpr};@@ -21,6 +21,7 @@ impl Expr for DurationExpr {.then_longest_of(vec![Box::new(SpelledNumberExpr),Box::new(SequenceExpr::default().then_number()),+ Box::new(IndefiniteArticle::default()),]).then_whitespace().then(units);diff --git a/harper-core/src/linting/after_later.rs b/harper-core/src/linting/after_later.rsnew file mode 100644index 000000000..25085da3a--- /dev/null+++ b/harper-core/src/linting/after_later.rs@@ -0,0 +1,155 @@+use crate::Token;+use crate::expr::{DurationExpr, Expr, SequenceExpr};+use crate::linting::{ExprLinter, Lint, LintKind, Suggestion};+use crate::token_string_ext::TokenStringExt;++pub struct AfterLater {+ expr: Box<dyn Expr>,+}++impl Default for AfterLater {+ fn default() -> Self {+ Self {+ expr: Box::new(+ SequenceExpr::aco("after")+ .t_ws()+ .then_optional(+ SequenceExpr::word_set(&[+ "about",+ "almost",+ "approximately",+ "around",+ "circa",+ "exactly",+ "just",+ "maybe",+ "nearly",+ "only",+ "perhaps",+ "precisely",+ "probably",+ "roughly",+ ])+ .t_ws(),+ )+ .then(DurationExpr)+ .t_ws()+ .t_aco("later"),+ ),+ }+ }+}++impl ExprLinter for AfterLater {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let without_after: Vec<char> = toks[2..].span()?.get_content(src).to_vec();+ let without_later: Vec<char> = toks[..toks.len() - 2].span()?.get_content(src).to_vec();++ let template_chars = toks.span()?.get_content(src);++ Some(Lint {+ span: toks.span()?,+ lint_kind: LintKind::Redundancy,+ message: "Don't use `later` following `after [a period of time]`".to_string(),+ suggestions: vec![+ Suggestion::replace_with_match_case(without_after, template_chars),+ Suggestion::replace_with_match_case(without_later, template_chars),+ ],+ ..Default::default()+ })+ }++ fn description(&self) -> &str {+ "Checks for the word `later` following `after [a period of time]`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::{AfterLater, tests::assert_top3_suggestion_result};++ #[test]+ fn after_90_days_later() {+ assert_top3_suggestion_result(+ "Try to rename your organization after 90 days later because of GitHub official documentation it said.",+ AfterLater::default(),+ "Try to rename your organization after 90 days because of GitHub official documentation it said.",+ );+ }++ #[test]+ fn after_about_30_minutes_later() {+ assert_top3_suggestion_result(+ "It plays like 1 minute of the song and then stops, and after about 30 minutes later, the bot disconnects an throws DisTubeError",+ AfterLater::default(),+ "It plays like 1 minute of the song and then stops, and about 30 minutes later, the bot disconnects an throws DisTubeError",+ );+ }++ #[test]+ fn after_14_days_later() {+ assert_top3_suggestion_result(+ "After 14 days later, the cache expired.",+ AfterLater::default(),+ "After 14 days, the cache expired.",+ );+ }++ #[test]+ fn after_exactly_5_minutes_later() {+ assert_top3_suggestion_result(+ "After exactly 5 minutes later, they try again and the cluster is formed then.",+ AfterLater::default(),+ "Exactly 5 minutes later, they try again and the cluster is formed then.",+ );+ }++ #[test]+ fn after_22_years_later_1() {+ assert_top3_suggestion_result(+ "Completed YR campaign for 2nd time after 22 years later.",+ AfterLater::default(),+ "Completed YR campaign for 2nd time after 22 years.",+ );+ }++ #[test]+ fn after_almost_2_years_later() {+ assert_top3_suggestion_result(+ "This buyer contacted me after almost 2 years later.",+ AfterLater::default(),+ "This buyer contacted me almost 2 years later.",+ );+ }++ #[test]+ fn after_2_years_later() {+ assert_top3_suggestion_result(+ "Is Jedi Survivor better now after 2 years later?",+ AfterLater::default(),+ "Is Jedi Survivor better now after 2 years?",+ );+ }++ #[test]+ fn after_a_year_later() {+ assert_top3_suggestion_result(+ "Even after a year later, I don’t know how to get my self-love back.",+ AfterLater::default(),+ "Even a year later, I don’t know how to get my self-love back.",+ );+ }++ #[test]+ fn after_22_years_later_2() {+ assert_top3_suggestion_result(+ "After 22 years later, my top 1 game was Zeroed",+ AfterLater::default(),+ "After 22 years, my top 1 game was Zeroed",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 079c1e455..d65009c34 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -15,6 +15,7 @@ use super::a_part::APart;use super::addicting::Addicting;use super::adjective_double_degree::AdjectiveDoubleDegree;use super::adjective_of_a::AdjectiveOfA;+use super::after_later::AfterLater;use super::all_intents_and_purposes::AllIntentsAndPurposes;use super::am_in_the_morning::AmInTheMorning;use super::amounts_for::AmountsFor;@@ -450,6 +451,7 @@ impl LintGroup {insert_expr_rule!(Addicting, true);insert_expr_rule!(AdjectiveDoubleDegree, true);insert_struct_rule!(AdjectiveOfA, true);+ insert_expr_rule!(AfterLater, true);insert_expr_rule!(AllIntentsAndPurposes, true);insert_struct_rule!(AmInTheMorning, true);insert_expr_rule!(AmountsFor, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex f884dd66f..6bafe41b1 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -6,6 +6,7 @@ mod a_part;mod addicting;mod adjective_double_degree;mod adjective_of_a;+mod after_later;mod all_intents_and_purposes;mod am_in_the_morning;mod amounts_for;@@ -185,6 +186,7 @@ pub use a_part::APart;pub use addicting::Addicting;pub use adjective_double_degree::AdjectiveDoubleDegree;pub use adjective_of_a::AdjectiveOfA;+pub use after_later::AfterLater;pub use all_intents_and_purposes::AllIntentsAndPurposes;pub use am_in_the_morning::AmInTheMorning;pub use amounts_for::AmountsFor;