Skip to content

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

feat(core): add rules and fix description

Elijah Potter1d8ba9172694
diff --git a/harper-core/src/linting/first_aid_kit.rs b/harper-core/src/linting/first_aid_kit.rs
new file mode 100644
index 000000000..cf81bd797
--- /dev/null
+++ b/harper-core/src/linting/first_aid_kit.rs
@@ -0,0 +1,99 @@
+use crate::{
+ Token,
+ linting::{Lint, LintKind, PatternLinter, Suggestion},
+ patterns::{SequencePattern, WordSet},
+};
+
+pub struct FirstAidKit {
+ pattern: Box<dyn crate::patterns::Pattern>,
+}
+
+impl Default for FirstAidKit {
+ fn default() -> Self {
+ let supply_words = WordSet::new(&["aid", "starter", "travel", "tool"]);
+ let pattern = SequencePattern::default()
+ .then(supply_words)
+ .then_whitespace()
+ .then_any_capitalization_of("kid");
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for FirstAidKit {
+ fn pattern(&self) -> &dyn crate::patterns::Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let typo_token = tokens.last()?;
+ let typo_span = typo_token.span;
+ let typo_text = typo_span.get_content(source);
+ Some(Lint {
+ span: typo_span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ "kit".chars().collect(),
+ typo_text,
+ )],
+ message: "Did you mean `kit` (a set of items) instead of “kid”?".to_string(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Detects when “kid” after “aid”, “starter”, “travel”, or “tool” should be “kit” (a set of supplies)."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::FirstAidKit;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_first_aid_kid() {
+ assert_suggestion_result(
+ "A first aid kid is a collection of medical supplies.",
+ FirstAidKit::default(),
+ "A first aid kit is a collection of medical supplies.",
+ );
+ }
+
+ #[test]
+ fn corrects_starter_kid() {
+ assert_suggestion_result(
+ "Check the starter kid before proceeding.",
+ FirstAidKit::default(),
+ "Check the starter kit before proceeding.",
+ );
+ }
+
+ #[test]
+ fn corrects_travel_kid() {
+ assert_suggestion_result(
+ "Pack your travel kid for the trip.",
+ FirstAidKit::default(),
+ "Pack your travel kit for the trip.",
+ );
+ }
+
+ #[test]
+ fn corrects_tool_kid() {
+ assert_suggestion_result(
+ "Don't forget the tool kid for assembly.",
+ FirstAidKit::default(),
+ "Don't forget the tool kit for assembly.",
+ );
+ }
+
+ #[test]
+ fn does_not_flag_kid_in_other_contexts() {
+ assert_lint_count(
+ "The kid ran through the aid station.",
+ FirstAidKit::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 200578c22..38fdd84ff 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -26,6 +26,7 @@ use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::expand_time_shorthands::ExpandTimeShorthands;
+use super::first_aid_kit::FirstAidKit;
use super::for_noun::ForNoun;
use super::hedging::Hedging;
use super::hereby::Hereby;
@@ -341,6 +342,7 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_pattern_rule!(DespiteOf, true);
insert_pattern_rule!(ChockFull, true);
+ insert_struct_rule!(FirstAidKit, true);
insert_struct_rule!(PronounKnew, true);
insert_struct_rule!(TheHowWhy, true);
insert_struct_rule!(WidelyAccepted, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 787b27474..98ac1044a 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -20,6 +20,7 @@ mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
mod expand_time_shorthands;
+mod first_aid_kit;
mod for_noun;
mod hedging;
mod hereby;
@@ -86,6 +87,7 @@ pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use expand_time_shorthands::ExpandTimeShorthands;
+pub use first_aid_kit::FirstAidKit;
pub use for_noun::ForNoun;
pub use hedging::Hedging;
pub use hereby::Hereby;
@@ -114,7 +116,6 @@ pub use pattern_linter::PatternLinter;
pub use pique_interest::PiqueInterest;
pub use possessive_your::PossessiveYour;
pub use pronoun_contraction::PronounContraction;
-pub use pronoun_knew::PronounKnew;
pub use repeated_words::RepeatedWords;
pub use sentence_capitalization::SentenceCapitalization;
pub use somewhat_something::SomewhatSomething;
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 7510d654f..d6205b05d 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -1097,7 +1097,13 @@ pub fn lint_group() -> LintGroup {
["cursing through veins"],
["coursing through veins"],
"In this idiom, blood “courses” (flows) through veins, not “curses”.",
- "In English idioms, “to course” means to flow rapidly—so avoid the eggcorn “cursing through veins.”"
+ "In English idioms, “to course” means to flow rapidly—so avoid the eggcorn `cursing through veins.`"
+ ),
+ "BestRegards" => (
+ ["beat regards"],
+ ["best regards"],
+ "Use `best regards` to convey sincere well wishes in a closing.",
+ "In valedictions, `best` expresses your highest regard—avoid the typo `beat regards`."
),
});