Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
Rules (#1128)
Elijah Potter9fc86f645256
diff --git a/harper-core/affixes.json b/harper-core/affixes.jsonindex 640a20286..aa49da828 100644--- a/harper-core/affixes.json+++ b/harper-core/affixes.json@@ -417,7 +417,9 @@}],"target_metadata": {- "verb": {},+ "verb": {+ "tense": "Present"+ },"noun": null,"adjective": null},diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 1c232ea2d..72fe35175 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -52872,3 +52872,4 @@ year-over-year/5# And they will be reviewed# Word added using the `just addnoun` command will be added here+Yoast/M2diff --git a/harper-core/src/linting/how_to.rs b/harper-core/src/linting/how_to.rsnew file mode 100644index 000000000..ef2e4a2ff--- /dev/null+++ b/harper-core/src/linting/how_to.rs@@ -0,0 +1,235 @@+use crate::{+ Token, TokenStringExt,+ linting::{Lint, LintKind, PatternLinter, Suggestion},+ patterns::{All, InflectionOfBe, Invert, OwnedPatternExt, Pattern, SequencePattern},+};++pub struct HowTo {+ pattern: Box<dyn Pattern>,+}++impl Default for HowTo {+ fn default() -> Self {+ let mut pattern = All::default();++ let pos_pattern = SequencePattern::default()+ .t_aco("how")+ .then_whitespace()+ .then_verb();+ pattern.add(Box::new(pos_pattern));++ let exceptions = SequencePattern::default()+ .then_anything()+ .then_anything()+ .then(+ InflectionOfBe::new().or(Box::new(|tok: &Token, _: &[char]| {+ tok.kind.is_auxiliary_verb()+ || tok.kind.is_adjective()+ || tok.kind.is_present_tense_verb()+ })),+ );++ pattern.add(Box::new(Invert::new(exceptions)));++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for HowTo {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {+ let span = toks[0..2].span()?;+ let fix: Vec<char> = "to ".chars().collect();++ Some(Lint {+ span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::InsertAfter(fix)],+ message: "Insert `to` after `how` (e.g., `how to clone`).".into(),+ priority: 63,+ })+ }++ fn description(&self) -> &str {+ "Detects the omission of `to` in constructions like `how clone / how install` and suggests `how to …`."+ }+}++#[cfg(test)]+mod tests {+ use super::HowTo;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn flags_missing_to() {+ assert_suggestion_result(+ "Here's how clone the repository.",+ HowTo::default(),+ "Here's how to clone the repository.",+ );+ }++ #[test]+ fn ignores_correct_phrase() {+ assert_lint_count("Here's how to clone the repository.", HowTo::default(), 0);+ }++ #[test]+ fn flags_other_verbs() {+ assert_suggestion_result(+ "Learn how install Rust.",+ HowTo::default(),+ "Learn how to install Rust.",+ );+ }++ #[test]+ fn ros_package_install() {+ assert_suggestion_result(+ "Can someone explain how install this ROS package on Humble?",+ HowTo::default(),+ "Can someone explain how to install this ROS package on Humble?",+ );+ }++ #[test]+ fn extract_and_install_app() {+ assert_suggestion_result(+ "Here’s a quick guide on how install an app you’ve extracted from a tarball.",+ HowTo::default(),+ "Here’s a quick guide on how to install an app you’ve extracted from a tarball.",+ );+ }++ #[test]+ fn dll_files() {+ assert_suggestion_result(+ "This video shows how fix missing DLL files on Windows.",+ HowTo::default(),+ "This video shows how to fix missing DLL files on Windows.",+ );+ }++ #[test]+ fn dofus_on_ubuntu() {+ assert_suggestion_result(+ "Full tutorial on how install Dofus under Ubuntu.",+ HowTo::default(),+ "Full tutorial on how to install Dofus under Ubuntu.",+ );+ }++ #[test]+ fn tar_gz_install() {+ assert_suggestion_result(+ "Find out how install software shipped as a .tar.gz archive.",+ HowTo::default(),+ "Find out how to install software shipped as a .tar.gz archive.",+ );+ }++ #[test]+ fn thrift_libraries() {+ assert_suggestion_result(+ "Anyone know how install the Thrift libraries from source?",+ HowTo::default(),+ "Anyone know how to install the Thrift libraries from source?",+ );+ }++ #[test]+ fn windows_adk() {+ assert_suggestion_result(+ "Lost the Windows ADK again—remind me how install it?",+ HowTo::default(),+ "Lost the Windows ADK again—remind me how to install it?",+ );+ }++ #[test]+ fn accounting_errors() {+ assert_suggestion_result(+ "Eight common accounting errors and how fix them.",+ HowTo::default(),+ "Eight common accounting errors and how to fix them.",+ );+ }++ #[test]+ fn sentence_fragments() {+ assert_suggestion_result(+ "Here’s what sentence fragments are and how fix them.",+ HowTo::default(),+ "Here’s what sentence fragments are and how to fix them.",+ );+ }++ #[test]+ fn zipper_slider() {+ assert_suggestion_result(+ "Quick demo on how fix a broken zipper slider.",+ HowTo::default(),+ "Quick demo on how to fix a broken zipper slider.",+ );+ }++ #[test]+ fn door_lock() {+ assert_suggestion_result(+ "Tips on how fix a door that won’t lock.",+ HowTo::default(),+ "Tips on how to fix a door that won’t lock.",+ );+ }++ #[test]+ fn already_correct_install() {+ assert_lint_count(+ "See how to install the package with apt.",+ HowTo::default(),+ 0,+ );+ }++ #[test]+ fn already_correct_fix() {+ assert_lint_count(+ "He showed me how to fix the zipper in ten minutes.",+ HowTo::default(),+ 0,+ );+ }++ #[test]+ fn how_are_you() {+ assert_lint_count("How are you?", HowTo::default(), 0);+ }++ #[test]+ fn how_calm_you_are() {+ assert_lint_count("I like how calm you are.", HowTo::default(), 0);+ }++ #[test]+ fn how_will_you_make_up() {+ assert_lint_count(+ "How will you make up for your mistakes?",+ HowTo::default(),+ 0,+ );+ }++ #[test]+ fn storytelling_clause() {+ assert_lint_count(+ "I will tell about how leaving my husband led to my dog winning a Nobel Prize.",+ HowTo::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 309c9f372..15d62052e 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -31,6 +31,7 @@ use super::for_noun::ForNoun;use super::hedging::Hedging;use super::hereby::Hereby;use super::hop_hope::HopHope;+use super::how_to::HowTo;use super::hyphenate_number_day::HyphenateNumberDay;use super::inflected_verb_after_to::InflectedVerbAfterTo;use super::left_right_hand::LeftRightHand;@@ -345,6 +346,7 @@ impl LintGroup {insert_struct_rule!(LetsConfusion, true);insert_pattern_rule!(DespiteOf, true);insert_pattern_rule!(ChockFull, true);+ insert_struct_rule!(HowTo, true);insert_struct_rule!(OfCourse, true);insert_struct_rule!(FirstAidKit, true);insert_struct_rule!(PronounKnew, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 0e0d4381d..3e956225c 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -25,6 +25,7 @@ mod for_noun;mod hedging;mod hereby;mod hop_hope;+mod how_to;mod hyphenate_number_day;mod inflected_verb_after_to;mod it_is;@@ -95,6 +96,7 @@ pub use for_noun::ForNoun;pub use hedging::Hedging;pub use hereby::Hereby;pub use hop_hope::HopHope;+pub use how_to::HowTo;pub use hyphenate_number_day::HyphenateNumberDay;pub use inflected_verb_after_to::InflectedVerbAfterTo;pub use left_right_hand::LeftRightHand;diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rsindex 49de22ec6..1a37da545 100644--- a/harper-core/src/linting/phrase_corrections.rs+++ b/harper-core/src/linting/phrase_corrections.rs@@ -1129,6 +1129,18 @@ pub fn lint_group() -> LintGroup {"A more vivid adjective would better convey intense hunger.","Encourages vivid writing by suggesting `starving` instead of weaker expressions like `very hungry.`"),+ "ThereIsAny" => (+ ["there any"],+ ["there is any"],+ "Insert `is` for correct grammar.",+ "Replaces `there any` with `there is any`."+ ),+ "NotIn" => (+ ["no in"],+ ["not in"],+ "Use `not in` for correct grammar.",+ "Replaces `no in` with `not in`."+ ),"LastDitch" => (["last ditch", "last ditched", "last-ditched"],["last-ditch"],diff --git a/harper-core/src/patterns/inflection_of_be.rs b/harper-core/src/patterns/inflection_of_be.rsnew file mode 100644index 000000000..282d51160--- /dev/null+++ b/harper-core/src/patterns/inflection_of_be.rs@@ -0,0 +1,32 @@+use std::num::NonZero;++use super::Pattern;+use crate::Token;+use crate::patterns::WordSet;++/// Matches any inflection of the verb “be”:+/// `am`, `is`, `are`, `was`, `were`, `be`, `been`, `being`.+pub struct InflectionOfBe {+ /// If using a `WordSet` proves expensive, we'll switch to something else.+ inner: WordSet,+}++impl Default for InflectionOfBe {+ fn default() -> Self {+ Self::new()+ }+}++impl InflectionOfBe {+ pub fn new() -> Self {+ Self {+ inner: WordSet::new(&["be", "am", "is", "are", "was", "were", "been", "being"]),+ }+ }+}++impl Pattern for InflectionOfBe {+ fn matches(&self, tokens: &[Token], source: &[char]) -> Option<NonZero<usize>> {+ self.inner.matches(tokens, source)+ }+}diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 2750c6c36..bc75e251e 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -16,6 +16,7 @@ mod either_pattern;mod exact_phrase;mod implies_quantity;mod indefinite_article;+mod inflection_of_be;mod invert;mod naive_pattern_group;mod nominal_phrase;@@ -37,6 +38,7 @@ pub use either_pattern::EitherPattern;pub use exact_phrase::ExactPhrase;pub use implies_quantity::ImpliesQuantity;pub use indefinite_article::IndefiniteArticle;+pub use inflection_of_be::InflectionOfBe;pub use invert::Invert;pub use naive_pattern_group::NaivePatternGroup;pub use nominal_phrase::NominalPhrase;diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex bb08b7c1b..abfd2817d 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -71,6 +71,7 @@ impl SequencePattern {gen_then_from_is!(possessive_nominal);gen_then_from_is!(plural_nominal);gen_then_from_is!(verb);+ gen_then_from_is!(auxiliary_verb);gen_then_from_is!(linking_verb);gen_then_from_is!(pronoun);gen_then_from_is!(punctuation);diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rsindex 2e1ab766f..5d75e547b 100644--- a/harper-core/src/token_kind.rs+++ b/harper-core/src/token_kind.rs@@ -1,7 +1,10 @@use is_macro::Is;use serde::{Deserialize, Serialize};-use crate::{ConjunctionData, NounData, Number, PronounData, Punctuation, Quote, WordMetadata};+use crate::{+ ConjunctionData, NounData, Number, PronounData, Punctuation, Quote, Tense, VerbData,+ WordMetadata,+};#[derive(Debug, Is, Clone, Serialize, Deserialize, Default, PartialOrd, Hash, Eq, PartialEq)]#[serde(tag = "kind", content = "value")]@@ -191,6 +194,19 @@ impl TokenKind {)}+ pub fn is_present_tense_verb(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(Some(WordMetadata {+ verb: Some(VerbData {+ tense: Some(Tense::Present),+ ..+ }),+ ..+ }))+ )+ }+pub fn is_adverb(&self) -> bool {matches!(self,diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rsindex c8182784a..b1400f550 100644--- a/harper-core/src/word_metadata.rs+++ b/harper-core/src/word_metadata.rs@@ -124,6 +124,16 @@ impl WordMetadata {adverb has);+ pub fn is_present_tense_verb(&self) -> bool {+ matches!(+ self.verb,+ Some(VerbData {+ tense: Some(Tense::Present),+ ..+ })+ )+ }+/// Checks if the word is definitely nominalpro.pub fn is_nominal(&self) -> bool {self.noun.is_some() || self.pronoun.is_some()@@ -216,8 +226,8 @@ impl WordMetadata {// TODO future shares a form with present/infinitive#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Is, Hash)]pub enum Tense {- // Past,- // Present,+ Past,+ Present,// Future,}