Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: long time ago→a long time ago (#3395)

Andrew Dunbar99f8687cac89
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index 320c3b82a..853a0a97d 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -4913,6 +4913,13 @@
"state": true,
"label": "In Hindsight"
}
+ },
+ {
+ "Bool": {
+ "name": "LongTimeAgo",
+ "state": true,
+ "label": "Long Time Ago"
+ }
}
]
}
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index 58bc676d9..c58d47f76 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -119,6 +119,7 @@ use super::let_to_do::LetToDo;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
use super::long_sentences::LongSentences;
+use super::long_time_ago::LongTimeAgo;
use super::look_down_ones_nose::LookDownOnesNose;
use super::looking_forward_to::LookingForwardTo;
use super::mass_nouns::MassNouns;
@@ -667,6 +668,7 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_expr_rule!(Likewise, true);
insert_struct_rule!(LongSentences, true);
+ insert_expr_rule!(LongTimeAgo, true);
insert_expr_rule!(LookDownOnesNose, true);
insert_expr_rule!(LookingForwardTo, true);
insert_struct_rule_with_dict!(MassNouns, true);
diff --git a/harper-core/src/linting/long_time_ago.rs b/harper-core/src/linting/long_time_ago.rs
new file mode 100644
index 000000000..b255fc06a
--- /dev/null
+++ b/harper-core/src/linting/long_time_ago.rs
@@ -0,0 +1,97 @@
+use crate::{
+ Lint, Token, TokenStringExt,
+ expr::{AnchorStart, Expr, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+};
+
+pub struct LongTimeAgo {
+ expr: SequenceExpr,
+}
+
+impl Default for LongTimeAgo {
+ fn default() -> Self {
+ Self {
+ expr: SequenceExpr::any_of(vec![
+ Box::new(AnchorStart),
+ Box::new(SequenceExpr::default().then_word_except(&["a"]).t_ws()),
+ ])
+ .t_aco("long")
+ .t_ws()
+ .t_aco("time")
+ .t_ws()
+ .t_aco("ago"),
+ }
+ }
+}
+
+impl ExprLinter for LongTimeAgo {
+ type Unit = Chunk;
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let span = match matched_tokens.len() {
+ 5 => matched_tokens.span()?,
+ 7 => matched_tokens[2..].span()?,
+ _ => return None,
+ };
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Usage,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "a long time ago",
+ span.get_content(source),
+ )],
+ message: "The correct phrase is `a long time ago`.".to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "Corrects the missing article `a` in the phrase `long time ago`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::LongTimeAgo;
+
+ #[test]
+ fn fix_long_time_ago_mid_sentence() {
+ assert_suggestion_result(
+ "Simple URL redirect handler written long time ago in PHP - yaroslav-ilin/shortener.",
+ LongTimeAgo::default(),
+ "Simple URL redirect handler written a long time ago in PHP - yaroslav-ilin/shortener.",
+ );
+ }
+
+ #[test]
+ fn fix_long_time_ago_start_of_sentence() {
+ assert_suggestion_result(
+ "Long time ago, I wrote this code.",
+ LongTimeAgo::default(),
+ "A long time ago, I wrote this code.",
+ );
+ }
+
+ #[test]
+ fn dont_flag_a_long_time_ago_mid_sentence() {
+ assert_no_lints(
+ "Created a long time ago and now finally saved in this gist as a backup.",
+ LongTimeAgo::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_a_long_time_ago_start_of_sentence() {
+ assert_no_lints(
+ "a long time ago i had random site redirector on my we site that would send visitors to... - sites.txt.",
+ LongTimeAgo::default(),
+ );
+ }
+}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 8a1b58d0f..d61f93bcc 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -124,6 +124,7 @@ mod lint;
mod lint_group;
mod lint_kind;
mod long_sentences;
+mod long_time_ago;
mod look_down_ones_nose;
mod looking_forward_to;
mod map_phrase_linter;