Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add rule for redundant modal verbs (#1536)
Elijah Potter975e2b471e84
diff --git a/harper-core/src/linting/double_modal.rs b/harper-core/src/linting/double_modal.rsnew file mode 100644index 000000000..0de18528a--- /dev/null+++ b/harper-core/src/linting/double_modal.rs@@ -0,0 +1,119 @@+use crate::expr::Expr;+use crate::expr::SequenceExpr;+use crate::patterns::ModalVerb;+use crate::{Token, TokenStringExt};++use super::Suggestion;+use super::{ExprLinter, Lint, LintKind};++pub struct DoubleModal {+ expr: Box<dyn Expr>,+}++impl Default for DoubleModal {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .then(ModalVerb::default())+ .t_ws()+ .then(ModalVerb::default());++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for DoubleModal {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let first_chars = matched_tokens.first()?.span.get_content(source);+ let second_chars = matched_tokens.last()?.span.get_content(source);++ Some(Lint {+ span: matched_tokens.span()?,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![+ Suggestion::ReplaceWith(first_chars.into()),+ Suggestion::ReplaceWith(second_chars.into()),+ ],+ message: "Two modal verbs in a row are rarely grammatical; remove one.".to_string(),+ priority: 31,+ })+ }++ fn description(&self) -> &'static str {+ "Two modal verbs in a row are rarely grammatical; remove one of them."+ }+}++#[cfg(test)]+mod tests {+ use super::DoubleModal;+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};++ #[test]+ fn detects_might_could() {+ assert_lint_count(+ "They might could finish the project by Friday.",+ DoubleModal::default(),+ 1,+ );+ }++ #[test]+ fn detects_should_ought() {+ assert_lint_count("You should ought to apologize.", DoubleModal::default(), 1);+ }++ #[test]+ fn allows_single_modal() {+ assert_lint_count("She must leave early.", DoubleModal::default(), 0);+ }++ #[test]+ fn detects_two_double_modals() {+ assert_lint_count(+ "He may can join us, and you might could too.",+ DoubleModal::default(),+ 2,+ );+ }++ #[test]+ fn suggests_removing_second_modal_keeps_first() {+ assert_suggestion_result(+ "They might could finish the project by Friday.",+ DoubleModal::default(),+ "They might finish the project by Friday.",+ );+ }++ #[test]+ fn suggests_removing_second_modal_keeps_first_variant_order() {+ assert_suggestion_result(+ "You could might want to double-check that.",+ DoubleModal::default(),+ "You could want to double-check that.",+ );+ }++ #[test]+ fn suggests_removing_second_modal_keeps_first_capitalised() {+ assert_suggestion_result(+ "We Must Should be consistent.",+ DoubleModal::default(),+ "We Must be consistent.",+ );+ }++ #[test]+ fn allows_will_need() {+ assert_no_lints(+ "You will need administrator or editor level access",+ DoubleModal::default(),+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 8d3bb9ea3..aad228563 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -31,6 +31,7 @@ use super::correct_number_suffix::CorrectNumberSuffix;use super::despite_of::DespiteOf;use super::discourse_markers::DiscourseMarkers;use super::dot_initialisms::DotInitialisms;+use super::double_modal::DoubleModal;use super::ellipsis_length::EllipsisLength;use super::else_possessive::ElsePossessive;use super::everyday::Everyday;@@ -389,6 +390,7 @@ impl LintGroup {insert_expr_rule!(BoringWords, false);insert_struct_rule!(CapitalizePersonalPronouns, true);insert_expr_rule!(ChockFull, true);+ insert_expr_rule!(DoubleModal, true);insert_expr_rule!(MissingPreposition, true);insert_struct_rule!(DiscourseMarkers, true);insert_expr_rule!(WayTooAdjective, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 2fcc15696..58a7cf208 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -25,6 +25,7 @@ mod dashes;mod despite_of;mod discourse_markers;mod dot_initialisms;+mod double_modal;mod ellipsis_length;mod else_possessive;mod everyday;@@ -140,6 +141,7 @@ pub use dashes::Dashes;pub use despite_of::DespiteOf;pub use discourse_markers::DiscourseMarkers;pub use dot_initialisms::DotInitialisms;+pub use double_modal::DoubleModal;pub use ellipsis_length::EllipsisLength;pub use everyday::Everyday;pub use expand_time_shorthands::ExpandTimeShorthands;diff --git a/harper-core/src/linting/modal_of.rs b/harper-core/src/linting/modal_of.rsindex 44e9f955c..8936b8297 100644--- a/harper-core/src/linting/modal_of.rs+++ b/harper-core/src/linting/modal_of.rs@@ -1,7 +1,8 @@use crate::expr::Expr;use crate::expr::LongestMatchOf;use crate::expr::SequenceExpr;-use crate::{Lrc, Token, TokenStringExt, patterns::WordSet};+use crate::patterns::ModalVerb;+use crate::{Lrc, Token, TokenStringExt};use super::{ExprLinter, Lint, LintKind, Suggestion};@@ -15,15 +16,10 @@ impl Default for ModalOf {// Note 2. "had of" has trickier false positives and is less common anyway// "The only other report we've had of this kind of problem ..."// "The code I had of this used to work fine ..."- let modals = ["could", "might", "must", "should", "would"];- let mut words = WordSet::new(&modals);- modals.iter().for_each(|word| {- words.add(&format!("{word}n't"));- });let modal_of = Lrc::new(SequenceExpr::default()- .then(words)+ .then(ModalVerb::default()).then_whitespace().t_aco("of"),);diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rsindex 9316430f0..a7f9e1ed2 100644--- a/harper-core/src/linting/phrase_corrections/mod.rs+++ b/harper-core/src/linting/phrase_corrections/mod.rs@@ -964,6 +964,12 @@ pub fn lint_group() -> LintGroup {"Prefer the standard term `wrought iron`.","`Wrought iron` is low-carbon, malleable iron used for decorative work; variants like `rod iron` or `rot iron` are phonetic misspellings that may confuse readers."),+ "FromTheGetGo" => (+ ["from the get go"],+ ["from the get-go"],+ "Use the hyphenated form: `from the get-go`.",+ "Ensures `from the get-go` is correctly hyphenated, preserving the idiom’s meaning of ‘from the very beginning’."+ ),});group.set_all_rules_to(Some(true));diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 8281739c1..4198f8481 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -12,6 +12,7 @@ mod implies_quantity;mod indefinite_article;mod inflection_of_be;mod invert;+mod modal_verb;mod nominal_phrase;mod upos_set;mod whitespace_pattern;@@ -24,6 +25,7 @@ pub use implies_quantity::ImpliesQuantity;pub use indefinite_article::IndefiniteArticle;pub use inflection_of_be::InflectionOfBe;pub use invert::Invert;+pub use modal_verb::ModalVerb;pub use nominal_phrase::NominalPhrase;pub use upos_set::UPOSSet;pub use whitespace_pattern::WhitespacePattern;diff --git a/harper-core/src/patterns/modal_verb.rs b/harper-core/src/patterns/modal_verb.rsnew file mode 100644index 000000000..4ccb71c4e--- /dev/null+++ b/harper-core/src/patterns/modal_verb.rs@@ -0,0 +1,26 @@+use super::{Pattern, WordSet};++pub struct ModalVerb {+ inner: WordSet,+}++impl Default for ModalVerb {+ fn default() -> Self {+ let modals = [+ "can", "could", "may", "might", "must", "shall", "should", "will", "would", "ought",+ "dare",+ ];+ let mut words = WordSet::new(&modals);+ modals.iter().for_each(|word| {+ words.add(&format!("{word}n't"));+ });++ Self { inner: words }+ }+}++impl Pattern for ModalVerb {+ fn matches(&self, tokens: &[crate::Token], source: &[char]) -> Option<usize> {+ self.inner.matches(tokens, source)+ }+}