Skip to content

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

feat: best of all times→best of all time (#1715)

Andrew Dunbar647df0ff651a
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 02ec57e72..af0c0d522 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -2,7 +2,7 @@ use paste::paste;
use crate::{
CharStringExt, Span, Token, TokenKind,
- expr::{FirstMatchOf, LongestMatchOf},
+ expr::{FirstMatchOf, FixedPhrase, LongestMatchOf},
patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word, WordSet},
};
@@ -94,6 +94,11 @@ impl SequenceExpr {
Self::any_capitalization_of(word)
}
+ /// Match the first of multiple expressions.
+ pub fn any_of(self, exprs: Vec<Box<dyn Expr>>) -> Self {
+ self.then_any_of(exprs)
+ }
+
/// Match any word from the given set of words, case-insensitive.
pub fn word_set(words: &'static [&'static str]) -> Self {
Self::default().then_word_set(words)
@@ -211,6 +216,11 @@ impl SequenceExpr {
self.then(Word::new_exact(word))
}
+ /// Match a fixed phrase.
+ pub fn then_fixed_phrase(self, phrase: &'static str) -> Self {
+ self.then(FixedPhrase::from_phrase(phrase))
+ }
+
/// Match any word except the ones in `words`.
pub fn then_word_except(self, words: &'static [&'static str]) -> Self {
self.then(move |tok: &Token, src: &[char]| {
@@ -330,6 +340,7 @@ impl SequenceExpr {
gen_then_from_is!(adjective);
gen_then_from_is!(positive_adjective);
gen_then_from_is!(comparative_adjective);
+ gen_then_from_is!(superlative_adjective);
// Adverbs
diff --git a/harper-core/src/linting/best_of_all_time.rs b/harper-core/src/linting/best_of_all_time.rs
new file mode 100644
index 000000000..f872d26d3
--- /dev/null
+++ b/harper-core/src/linting/best_of_all_time.rs
@@ -0,0 +1,151 @@
+use crate::Token;
+use crate::expr::{Expr, SequenceExpr};
+use crate::patterns::WhitespacePattern;
+
+use super::{ExprLinter, Lint, LintKind, Suggestion};
+
+pub struct BestOfAllTime {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for BestOfAllTime {
+ fn default() -> Self {
+ // Best, Biggest
+ let inflection_superlative = SequenceExpr::default().then_superlative_adjective();
+ // Most interesting
+ let most_superlative = SequenceExpr::default()
+ .t_aco("most")
+ .t_ws()
+ .then_positive_adjective();
+ // Some resources call this an 'absolute adjective', some consider it a superlative.
+ let fave = SequenceExpr::word_set(&["favorite", "favourite"]);
+
+ // We can't use the noun phrase Expr because it allows determiners before the nouns and "best the thing" wouldn't be right
+ let expr = SequenceExpr::default()
+ .then_any_of(vec![
+ Box::new(inflection_superlative),
+ Box::new(most_superlative),
+ Box::new(fave),
+ ])
+ .then_one_or_more(SequenceExpr::default().then(WhitespacePattern).then_noun())
+ .then_fixed_phrase(" of all times");
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for BestOfAllTime {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let times_span = toks.last()?.span;
+
+ if let Some((_, time_singular)) = times_span.get_content(src).split_last() {
+ return Some(Lint {
+ span: times_span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::ReplaceWith(time_singular.to_vec())],
+ message: "This expression uses singular `time`".to_string(),
+ ..Default::default()
+ });
+ }
+
+ None
+ }
+
+ fn description(&self) -> &'static str {
+ "Checks for nonstandard `of all times` in superlatives instead of singular `time`"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ use super::BestOfAllTime;
+
+ #[test]
+ fn dont_flag_list_of_all_times() {
+ assert_lint_count(
+ "Provides a formatted list of all times that SDO was non-nominal",
+ BestOfAllTime::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn fix_after_best() {
+ assert_suggestion_result(
+ "And also in the best IDE of all times Visual Studio",
+ BestOfAllTime::default(),
+ "And also in the best IDE of all time Visual Studio",
+ );
+ }
+
+ #[test]
+ fn fix_after_greatest() {
+ assert_suggestion_result(
+ "This app shows you why Sachin Tendulkar is the greatest cricket of all times, by using interactive stories.",
+ BestOfAllTime::default(),
+ "This app shows you why Sachin Tendulkar is the greatest cricket of all time, by using interactive stories.",
+ );
+ }
+
+ #[test]
+ fn fix_after_biggest() {
+ assert_suggestion_result(
+ "THIS IS THE BIGGEST QUESTIONS OF ALL TIMES...",
+ BestOfAllTime::default(),
+ "THIS IS THE BIGGEST QUESTIONS OF ALL TIME...",
+ );
+ }
+
+ #[test]
+ fn fix_after_most_influential() {
+ assert_suggestion_result(
+ "It is an open source project that aggregates multiple lists of \"the best/most influential games of all times\"",
+ BestOfAllTime::default(),
+ "It is an open source project that aggregates multiple lists of \"the best/most influential games of all time\"",
+ );
+ }
+
+ #[test]
+ fn dont_flag_sum_of_all_times() {
+ assert_lint_count(
+ "The original TotalTime seems not be the sum of all times",
+ BestOfAllTime::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_history_stacks_of_all_times() {
+ assert_lint_count(
+ "Didn't this imply all history stacks of all times, which itself implied all those saved.",
+ BestOfAllTime::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn fix_after_favorite() {
+ assert_suggestion_result(
+ "Red Dead Redemption 2 is my nr 1 favorite game of all times",
+ BestOfAllTime::default(),
+ "Red Dead Redemption 2 is my nr 1 favorite game of all time",
+ );
+ }
+
+ #[test]
+ fn fix_after_favourite() {
+ assert_suggestion_result(
+ "Just made this website to show you my favourite movies of all times.",
+ BestOfAllTime::default(),
+ "Just made this website to show you my favourite movies of all time.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/its_contraction.rs b/harper-core/src/linting/its_contraction.rs
index 1eb1762e8..6fab27093 100644
--- a/harper-core/src/linting/its_contraction.rs
+++ b/harper-core/src/linting/its_contraction.rs
@@ -100,8 +100,6 @@ impl ItsContraction {
#[cfg(test)]
mod tests {
- use std::default;
-
use super::ItsContraction;
use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index e70f80fe4..eefbf80c8 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -22,6 +22,7 @@ use super::another_think_coming::AnotherThinkComing;
use super::ask_no_preposition::AskNoPreposition;
use super::avoid_curses::AvoidCurses;
use super::back_in_the_day::BackInTheDay;
+use super::best_of_all_time::BestOfAllTime;
use super::boring_words::BoringWords;
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
use super::chock_full::ChockFull;
@@ -408,6 +409,7 @@ impl LintGroup {
insert_expr_rule!(AskNoPreposition, true);
insert_struct_rule!(AvoidCurses, true);
insert_expr_rule!(BackInTheDay, true);
+ insert_expr_rule!(BestOfAllTime, true);
insert_expr_rule!(BoringWords, false);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_expr_rule!(ChockFull, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 3b830e0de..c16f42379 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -13,6 +13,7 @@ mod another_think_coming;
mod ask_no_preposition;
mod avoid_curses;
mod back_in_the_day;
+mod best_of_all_time;
mod boring_words;
mod capitalize_personal_pronouns;
mod chock_full;
@@ -144,6 +145,7 @@ pub use another_think_coming::AnotherThinkComing;
pub use ask_no_preposition::AskNoPreposition;
pub use avoid_curses::AvoidCurses;
pub use back_in_the_day::BackInTheDay;
+pub use best_of_all_time::BestOfAllTime;
pub use boring_words::BoringWords;
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
pub use chock_full::ChockFull;