Skip to content

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

feat: Implement #405 - the "5am in the morning" redundancy (#1414)

Andrew Dunbarcef9ce9042d8
diff --git a/harper-core/src/linting/am_in_the_morning.rs b/harper-core/src/linting/am_in_the_morning.rs
new file mode 100644
index 000000000..df65f736a
--- /dev/null
+++ b/harper-core/src/linting/am_in_the_morning.rs
@@ -0,0 +1,283 @@
+use crate::{
+ Lrc, Span, Token, TokenStringExt,
+ expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+pub struct AmInTheMorning {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for AmInTheMorning {
+ fn default() -> Self {
+ let am = WordSet::new(&["am", "a.m."]);
+ let pm = WordSet::new(&["pm", "p.m."]);
+
+ let maybe_ws_am = LongestMatchOf::new(vec![
+ Box::new(SequenceExpr::default().then(am.clone())),
+ Box::new(SequenceExpr::default().then_whitespace().then(am)),
+ ]);
+ let maybe_ws_pm = LongestMatchOf::new(vec![
+ Box::new(SequenceExpr::default().then(pm.clone())),
+ Box::new(SequenceExpr::default().then_whitespace().then(pm)),
+ ]);
+
+ let ws_in_periods = SequenceExpr::default()
+ .then(FixedPhrase::from_phrase(" in the "))
+ .then(WordSet::new(&["morning", "afternoon", "evening", "night"]));
+
+ let ws_at_periods = FixedPhrase::from_phrase(" at night");
+
+ let expr = SequenceExpr::default()
+ .then(Lrc::new(LongestMatchOf::new(vec![
+ Box::new(maybe_ws_am),
+ Box::new(maybe_ws_pm),
+ ])))
+ .then(Lrc::new(LongestMatchOf::new(vec![
+ Box::new(ws_in_periods),
+ Box::new(ws_at_periods),
+ ])));
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for AmInTheMorning {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let all_after_number_span = toks[0..].span()?;
+ let am_pm_idx = if toks[0].kind.is_whitespace() { 1 } else { 0 };
+
+ let maybe_ws_am_pm_span = Span::new(toks[0].span.start, toks[am_pm_idx].span.end);
+ let sugg_am_pm_only =
+ Suggestion::ReplaceWith(maybe_ws_am_pm_span.get_content(src).to_vec());
+
+ let ws_prep_period = Span::new(toks[am_pm_idx + 1].span.start, all_after_number_span.end);
+ let sugg_prep_period_only =
+ Suggestion::ReplaceWith(ws_prep_period.get_content(src).to_vec());
+
+ Some(Lint {
+ span: all_after_number_span,
+ lint_kind: LintKind::Redundancy,
+ suggestions: vec![sugg_am_pm_only, sugg_prep_period_only],
+ message: "The time period is redundant because it repeats what's already specified by the AM/PM indicator.".to_owned(), priority: 50,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Finds redundant am/pm indicators used together with time periods such as 'in the morning' or 'at night'."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::AmInTheMorning;
+ use crate::linting::tests::{
+ assert_lint_count, assert_nth_suggestion_result, assert_suggestion_result,
+ };
+
+ #[test]
+ fn flag_at_4am_in_the_morning() {
+ assert_lint_count("At 4am in the morning", AmInTheMorning::default(), 1);
+ }
+
+ #[test]
+ fn fix_at_4am_in_the_morning() {
+ assert_suggestion_result("At 4am in the morning", AmInTheMorning::default(), "At 4am");
+ }
+
+ #[test]
+ fn flag_at_4_am_in_the_morning() {
+ assert_lint_count("At 4 am in the morning", AmInTheMorning::default(), 1);
+ }
+
+ #[test]
+ fn fix_at_4_am_in_the_morning() {
+ assert_suggestion_result(
+ "At 4 am in the morning",
+ AmInTheMorning::default(),
+ "At 4 am",
+ );
+ }
+
+ #[test]
+ fn flag_at_4am_in_the_morning_caps() {
+ assert_lint_count("At 4AM in the morning", AmInTheMorning::default(), 1);
+ }
+
+ #[test]
+ fn fix_at_4am_in_the_morning_caps() {
+ assert_suggestion_result("At 4AM in the morning", AmInTheMorning::default(), "At 4AM");
+ }
+
+ #[test]
+ fn flag_at_4_am_in_the_morning_caps() {
+ assert_lint_count("At 4 AM in the morning", AmInTheMorning::default(), 1);
+ }
+
+ #[test]
+ fn fix_at_4_am_in_the_morning_caps() {
+ assert_suggestion_result(
+ "At 4 AM in the morning",
+ AmInTheMorning::default(),
+ "At 4 AM",
+ );
+ }
+
+ #[test]
+ fn at_4_a_dot_m_dot_in_the_morning() {
+ assert_lint_count("At 4 a.m. in the morning", AmInTheMorning::default(), 1)
+ }
+
+ #[test]
+ fn fix_at_4_a_dot_m_dot_in_the_morning() {
+ assert_suggestion_result(
+ "At 4 a.m. in the morning",
+ AmInTheMorning::default(),
+ "At 4 a.m.",
+ );
+ }
+
+ // real-world examples
+
+ #[test]
+ fn fix_real_world_1_am_in_the_morning() {
+ assert_suggestion_result(
+ "I wrote this whole program as a joke, at 1 AM in the morning. Nothing else to say.",
+ AmInTheMorning::default(),
+ "I wrote this whole program as a joke, at 1 AM. Nothing else to say.",
+ );
+ assert_nth_suggestion_result(
+ "I wrote this whole program as a joke, at 1 AM in the morning. Nothing else to say.",
+ AmInTheMorning::default(),
+ "I wrote this whole program as a joke, at 1 in the morning. Nothing else to say.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_3am_in_the_morning() {
+ assert_suggestion_result(
+ "Luckily I was at home, but it was not fun at 3am in the morning.",
+ AmInTheMorning::default(),
+ "Luckily I was at home, but it was not fun at 3am.",
+ );
+ assert_nth_suggestion_result(
+ "Luckily I was at home, but it was not fun at 3am in the morning.",
+ AmInTheMorning::default(),
+ "Luckily I was at home, but it was not fun at 3 in the morning.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_3am_at_night() {
+ assert_suggestion_result(
+ "If I want to run my script or some cron job at 3am at night, it seems to be not possible after macOS is in sleep mode.",
+ AmInTheMorning::default(),
+ "If I want to run my script or some cron job at 3am, it seems to be not possible after macOS is in sleep mode.",
+ );
+ assert_nth_suggestion_result(
+ "If I want to run my script or some cron job at 3am at night, it seems to be not possible after macOS is in sleep mode.",
+ AmInTheMorning::default(),
+ "If I want to run my script or some cron job at 3 at night, it seems to be not possible after macOS is in sleep mode.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_9pm_at_night() {
+ assert_suggestion_result(
+ "The servers stop at 9PM at night and starts again at 9AM.",
+ AmInTheMorning::default(),
+ "The servers stop at 9PM and starts again at 9AM.",
+ );
+ assert_nth_suggestion_result(
+ "The servers stop at 9PM at night and starts again at 9AM.",
+ AmInTheMorning::default(),
+ "The servers stop at 9 at night and starts again at 9AM.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_3_30_am_in_the_morning() {
+ assert_suggestion_result(
+ "Hello I can't believe my neighbor had the nerve to knock on my door at 3:30 AM in the morning.",
+ AmInTheMorning::default(),
+ "Hello I can't believe my neighbor had the nerve to knock on my door at 3:30 AM.",
+ );
+ assert_nth_suggestion_result(
+ "Hello I can't believe my neighbor had the nerve to knock on my door at 3:30 AM in the morning.",
+ AmInTheMorning::default(),
+ "Hello I can't believe my neighbor had the nerve to knock on my door at 3:30 in the morning.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_5_pm_in_the_afternoon_caps_dots() {
+ assert_suggestion_result(
+ "Style issues get a blue marker: It's 5 P.M. in the afternoon.",
+ AmInTheMorning::default(),
+ "Style issues get a blue marker: It's 5 P.M..",
+ );
+ assert_nth_suggestion_result(
+ "Style issues get a blue marker: It's 5 P.M. in the afternoon.",
+ AmInTheMorning::default(),
+ "Style issues get a blue marker: It's 5 in the afternoon.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_5_pm_in_the_afternoon_caps() {
+ assert_suggestion_result(
+ "Its a impressively versatile tool if youd like to tell a colleague from over sea's about at 5 PM in the afternoon on Monday, 27 May 2007.",
+ AmInTheMorning::default(),
+ "Its a impressively versatile tool if youd like to tell a colleague from over sea's about at 5 PM on Monday, 27 May 2007.",
+ );
+ assert_nth_suggestion_result(
+ "Its a impressively versatile tool if youd like to tell a colleague from over sea's about at 5 PM in the afternoon on Monday, 27 May 2007.",
+ AmInTheMorning::default(),
+ "Its a impressively versatile tool if youd like to tell a colleague from over sea's about at 5 in the afternoon on Monday, 27 May 2007.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_6_pm_in_the_evening() {
+ assert_suggestion_result(
+ "I am in China and it is six pm in the evening.",
+ AmInTheMorning::default(),
+ "I am in China and it is six pm.",
+ );
+ assert_nth_suggestion_result(
+ "I am in China and it is six pm in the evening.",
+ AmInTheMorning::default(),
+ "I am in China and it is six in the evening.",
+ 1,
+ );
+ }
+
+ #[test]
+ fn fix_real_world_4_am_in_the_morning() {
+ assert_suggestion_result(
+ "On the second application, we normally have the 503 between 1am and 4 am in the morning, almost every day.",
+ AmInTheMorning::default(),
+ "On the second application, we normally have the 503 between 1am and 4 am, almost every day.",
+ );
+ assert_nth_suggestion_result(
+ "On the second application, we normally have the 503 between 1am and 4 am in the morning, almost every day.",
+ AmInTheMorning::default(),
+ "On the second application, we normally have the 503 between 1am and 4 in the morning, almost every day.",
+ 1,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 2edffe9e3..f828d4073 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -13,6 +13,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::a_part::APart;
use super::adjective_of_a::AdjectiveOfA;
+use super::am_in_the_morning::AmInTheMorning;
use super::amounts_for::AmountsFor;
use super::an_a::AnA;
use super::ask_no_preposition::AskNoPreposition;
@@ -359,6 +360,7 @@ impl LintGroup {
// Add all the more complex rules to the group.
insert_pattern_rule!(APart, true);
insert_struct_rule!(AdjectiveOfA, true);
+ insert_struct_rule!(AmInTheMorning, true);
insert_pattern_rule!(AmountsFor, true);
insert_struct_rule!(AnA, true);
insert_pattern_rule!(AskNoPreposition, true);
diff --git a/harper-core/src/linting/lint_kind.rs b/harper-core/src/linting/lint_kind.rs
index 96e41033e..16e2cf41b 100644
--- a/harper-core/src/linting/lint_kind.rs
+++ b/harper-core/src/linting/lint_kind.rs
@@ -8,32 +8,34 @@ use serde::{Deserialize, Serialize};
/// the existing categories.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Is, Default, Hash, PartialEq, Eq)]
pub enum LintKind {
- /// This should only be used by linters doing spellcheck on individual words.
- Spelling,
Capitalization,
- Style,
- Formatting,
- Repetition,
Enhancement,
- Readability,
- WordChoice,
- Punctuation,
+ Formatting,
#[default]
Miscellaneous,
+ Punctuation,
+ Readability,
+ Redundancy,
+ Repetition,
+ /// This should only be used by linters doing spellcheck on individual words.
+ Spelling,
+ Style,
+ WordChoice,
}
impl LintKind {
pub fn new_from_str(s: &str) -> Option<Self> {
Some(match s {
- "Spelling" => LintKind::Spelling,
"Capitalization" => LintKind::Capitalization,
+ "Enhancement" => LintKind::Enhancement,
"Formatting" => LintKind::Formatting,
- "Repetition" => LintKind::Repetition,
- "Readability" => LintKind::Readability,
"Miscellaneous" => LintKind::Miscellaneous,
- "Enhancement" => LintKind::Enhancement,
- "Word Choice" => LintKind::WordChoice,
+ "Readability" => LintKind::Readability,
+ "Redundancy" => LintKind::Redundancy,
+ "Repetition" => LintKind::Repetition,
+ "Spelling" => LintKind::Spelling,
"Style" => LintKind::Style,
+ "Word Choice" => LintKind::WordChoice,
_ => return None,
})
}
@@ -41,16 +43,17 @@ impl LintKind {
/// Produce a string representation, which can be used as keys in a map or CSS variables.
pub fn to_string_key(&self) -> String {
match self {
- LintKind::Spelling => "Spelling",
LintKind::Capitalization => "Capitalization",
+ LintKind::Enhancement => "Enhancement",
LintKind::Formatting => "Formatting",
- LintKind::Repetition => "Repetition",
- LintKind::Readability => "Readability",
LintKind::Miscellaneous => "Miscellaneous",
- LintKind::Enhancement => "Enhancement",
- LintKind::WordChoice => "WordChoice",
- LintKind::Style => "Style",
LintKind::Punctuation => "Punctuation",
+ LintKind::Readability => "Readability",
+ LintKind::Redundancy => "Redundancy",
+ LintKind::Repetition => "Repetition",
+ LintKind::Spelling => "Spelling",
+ LintKind::Style => "Style",
+ LintKind::WordChoice => "WordChoice",
}
.to_owned()
}
@@ -59,16 +62,17 @@ impl LintKind {
impl Display for LintKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
- LintKind::Spelling => "Spelling",
LintKind::Capitalization => "Capitalization",
+ LintKind::Enhancement => "Enhancement",
LintKind::Formatting => "Formatting",
- LintKind::Repetition => "Repetition",
- LintKind::Readability => "Readability",
LintKind::Miscellaneous => "Miscellaneous",
- LintKind::Enhancement => "Enhancement",
- LintKind::WordChoice => "Word Choice",
- LintKind::Style => "Style",
LintKind::Punctuation => "Punctuation",
+ LintKind::Readability => "Readability",
+ LintKind::Redundancy => "Redundancy",
+ LintKind::Repetition => "Repetition",
+ LintKind::Spelling => "Spelling",
+ LintKind::Style => "Style",
+ LintKind::WordChoice => "Word Choice",
};
write!(f, "{}", s)
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 4ce545663..b3bbb9b29 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -4,6 +4,7 @@
mod a_part;
mod adjective_of_a;
+mod am_in_the_morning;
mod amounts_for;
mod an_a;
mod ask_no_preposition;
@@ -99,6 +100,7 @@ mod wordpress_dotcom;
pub use a_part::APart;
pub use adjective_of_a::AdjectiveOfA;
+pub use am_in_the_morning::AmInTheMorning;
pub use amounts_for::AmountsFor;
pub use an_a::AnA;
pub use ask_no_preposition::AskNoPreposition;