Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: most amount/number→highest amount/number (#1238)
Andrew Dunbar8ee0667600f6
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 265c5ee71..8a8d87c1f 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -44,6 +44,7 @@ use super::linking_verbs::LinkingVerbs;use super::long_sentences::LongSentences;use super::merge_words::MergeWords;use super::modal_of::ModalOf;+use super::most_number::MostNumber;use super::multiple_sequential_pronouns::MultipleSequentialPronouns;use super::nail_on_the_head::NailOnTheHead;use super::nobody::Nobody;@@ -364,6 +365,7 @@ impl LintGroup {insert_struct_rule!(LongSentences, true);insert_struct_rule!(MergeWords, true);insert_pattern_rule!(ModalOf, true);+ insert_pattern_rule!(MostNumber, true);insert_pattern_rule!(MultipleSequentialPronouns, true);insert_struct_rule!(NoOxfordComma, false);insert_pattern_rule!(Nobody, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex d661b75aa..883364706 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -45,6 +45,7 @@ mod map_phrase_linter;mod merge_linters;mod merge_words;mod modal_of;+mod most_number;mod multiple_sequential_pronouns;mod nail_on_the_head;mod no_oxford_comma;@@ -122,6 +123,7 @@ pub use long_sentences::LongSentences;pub use map_phrase_linter::MapPhraseLinter;pub use merge_words::MergeWords;pub use modal_of::ModalOf;+pub use most_number::MostNumber;pub use multiple_sequential_pronouns::MultipleSequentialPronouns;pub use nail_on_the_head::NailOnTheHead;pub use no_oxford_comma::NoOxfordComma;diff --git a/harper-core/src/linting/most_number.rs b/harper-core/src/linting/most_number.rsnew file mode 100644index 000000000..65ed7f312--- /dev/null+++ b/harper-core/src/linting/most_number.rs@@ -0,0 +1,120 @@+use crate::{+ Token, TokenStringExt,+ patterns::{All, Pattern, SequencePattern, WordSet},+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++pub struct MostNumber {+ pattern: Box<dyn Pattern>,+}++impl Default for MostNumber {+ fn default() -> Self {+ Self {+ pattern: Box::new(All::new(vec![+ // Main pattern+ Box::new(+ SequencePattern::default()+ .t_aco("most")+ .t_ws()+ .then(WordSet::new(&["amount", "number"])),+ ),+ // Context pattern+ Box::new(+ SequencePattern::default()+ .then_anything()+ .then_anything()+ .then_anything()+ .then_anything()+ .t_aco("of"),+ ),+ ])),+ }+ }+}++impl PatternLinter for MostNumber {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], source: &[char]) -> Option<Lint> {+ let most_amt_num_span = toks[0..3].span()?;+ let noun_string = toks[2].span.get_content_string(source);+ let superlatives = if noun_string == "amount" {+ vec!["largest", "greatest"]+ } else {+ vec!["highest", "largest"]+ };+ let suggestions = superlatives+ .into_iter()+ .map(|superlative| {+ Suggestion::replace_with_match_case(+ format!("{} {}", superlative, noun_string).chars().collect(),+ most_amt_num_span.get_content(source),+ )+ })+ .collect();+ Some(Lint {+ span: most_amt_num_span,+ lint_kind: LintKind::Miscellaneous,+ suggestions,+ message: format!(+ "`Most` is not standard before `{}`.",+ toks[2].span.get_content_string(source)+ ),+ priority: 31,+ })+ }++ fn description(&self) -> &str {+ "Corrects `most number` and `most amount`"+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{+ assert_lint_count, assert_suggestion_result, assert_top3_suggestion_result,+ };++ use super::MostNumber;++ #[test]+ fn corrects_most_number() {+ assert_suggestion_result(+ "Find artists that have been on Spotify the most number of times.",+ MostNumber::default(),+ "Find artists that have been on Spotify the highest number of times.",+ );+ }++ #[test]+ #[ignore = "replace_with_match_case currently produces 'GreatEst'"]+ fn corrects_most_amount_title_case() {+ assert_top3_suggestion_result(+ "Area of Container with the Most Amount of Water",+ MostNumber::default(),+ "Area of Container with the Greatest Amount of Water",+ );+ }++ #[test]+ fn corrects_most_amount() {+ assert_top3_suggestion_result(+ "I just wanted to make sure it's good for the most amount of people, not just what I like.",+ MostNumber::default(),+ "I just wanted to make sure it's good for the greatest amount of people, not just what I like.",+ );+ }++ #[test]+ fn dont_correct_most_number_without_context() {+ assert_lint_count(+ "The random non-sequential nature should prevent most number gaming/sniping/lunging.",+ MostNumber::default(),+ 0,+ );+ }+}