Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): rule to condense `miss spell` and friends (#2066)
Elijah Potter6b46a38e7f9e
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex e9f8a2478..140fe1b46 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -6,7 +6,7 @@ use crate::{patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word, WordSet},};-use super::{Expr, Optional, Repeating, Step, UnlessStep};+use super::{Expr, Optional, OwnedExprExt, Repeating, Step, UnlessStep};#[derive(Default)]pub struct SequenceExpr {@@ -175,6 +175,16 @@ impl SequenceExpr {self.then(WhitespacePattern)}+ /// Match against one or more whitespace tokens.+ pub fn then_whitespace_or_hyphen(self) -> Self {+ self.then(WhitespacePattern.or(|tok: &Token, _: &[char]| tok.kind.is_hyphen()))+ }++ /// Shorthand for [`Self::then_whitespace_or_hyphen`].+ pub fn t_ws_h(self) -> Self {+ self.then_whitespace_or_hyphen()+ }+/// Shorthand for [`Self::then_whitespace`].pub fn t_ws(self) -> Self {self.then_whitespace()diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 7eef74754..d60992305 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -73,6 +73,7 @@ use super::looking_forward_to::LookingForwardTo;use super::merge_words::MergeWords;use super::missing_preposition::MissingPreposition;use super::missing_to::MissingTo;+use super::misspell::Misspell;use super::mixed_bag::MixedBag;use super::modal_of::ModalOf;use super::months::Months;@@ -478,6 +479,7 @@ impl LintGroup {insert_struct_rule!(LongSentences, true);insert_expr_rule!(LookingForwardTo, true);insert_struct_rule!(MergeWords, true);+ insert_expr_rule!(Misspell, true);insert_expr_rule!(MissingPreposition, true);insert_expr_rule!(MissingTo, true);insert_expr_rule!(MixedBag, true);diff --git a/harper-core/src/linting/misspell.rs b/harper-core/src/linting/misspell.rsnew file mode 100644index 000000000..bad74ce7f--- /dev/null+++ b/harper-core/src/linting/misspell.rs@@ -0,0 +1,115 @@+use crate::{+ Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion},+ patterns::WordSet,+};++pub struct Misspell {+ expr: Box<dyn Expr>,+}++impl Default for Misspell {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .then(WordSet::new(&["miss"]))+ .t_ws_h()+ .then(WordSet::new(&[+ "spell",+ "spelled",+ "spelling",+ "spells",+ "spellings",+ ]));++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for Misspell {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let span = matched_tokens.span()?;+ let misspell_variant = matched_tokens.last()?;++ let variant_chars = misspell_variant.span.get_content(source);+ let variant_lower = variant_chars+ .iter()+ .map(|c| c.to_ascii_lowercase())+ .collect::<String>();++ let replacement = match variant_lower.as_str() {+ "spell" => "misspell",+ "spelled" => "misspelled",+ "spelling" => "misspelling",+ "spells" => "misspells",+ "spellings" => "misspellings",+ _ => return None,+ };++ let suggestions = vec![Suggestion::replace_with_match_case(+ replacement.chars().collect(),+ span.get_content(source),+ )];++ Some(Lint {+ span,+ lint_kind: LintKind::BoundaryError,+ suggestions,+ message: "Write `misspell` and its inflections as a single word.".to_string(),+ priority: 63,+ })+ }++ fn description(&self) -> &'static str {+ "Ensures `misspell` and its inflected forms are written as a single word."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::Misspell;++ #[test]+ fn base_form() {+ assert_suggestion_result(+ "They often miss spell names in the log.",+ Misspell::default(),+ "They often misspell names in the log.",+ );+ }++ #[test]+ fn past_tense() {+ assert_suggestion_result(+ "She miss spelled the answer on the quiz.",+ Misspell::default(),+ "She misspelled the answer on the quiz.",+ );+ }++ #[test]+ fn past_tense_hyphen() {+ assert_suggestion_result(+ "She miss-spelled the answer on the quiz.",+ Misspell::default(),+ "She misspelled the answer on the quiz.",+ );+ }++ #[test]+ fn gerund_form() {+ assert_suggestion_result(+ "His constant miss spelling frustrated the team.",+ Misspell::default(),+ "His constant misspelling frustrated the team.",+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex c9b55f929..162c668a5 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -79,6 +79,7 @@ mod merge_linters;mod merge_words;mod missing_preposition;mod missing_to;+mod misspell;mod mixed_bag;mod modal_of;mod months;@@ -223,6 +224,7 @@ pub use mass_plurals::MassPlurals;pub use merge_words::MergeWords;pub use missing_preposition::MissingPreposition;pub use missing_to::MissingTo;+pub use misspell::Misspell;pub use mixed_bag::MixedBag;pub use modal_of::ModalOf;pub use months::Months;