Skip to content

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

refactor(core): move `fro` into own rule

Elijah Potter08bd25884868
diff --git a/harper-core/src/linting/for_noun.rs b/harper-core/src/linting/for_noun.rs
new file mode 100644
index 000000000..dbfb4eb12
--- /dev/null
+++ b/harper-core/src/linting/for_noun.rs
@@ -0,0 +1,77 @@
+use crate::{
+ Token,
+ patterns::{AnyCapitalization, NominalPhrase, OwnedPatternExt, Pattern, SequencePattern},
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct ForNoun {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for ForNoun {
+ fn default() -> Self {
+ let pattern = SequencePattern::aco("fro")
+ .then_whitespace()
+ .then(NominalPhrase.or(Box::new(AnyCapitalization::of("sure"))));
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for ForNoun {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let span = matched_tokens.first()?.span;
+ let problem_chars = span.get_content(source);
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "for",
+ problem_chars,
+ )],
+ message: "`For` is more common in this context.".to_owned(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects the archaic or mistaken `fro` to `for` when followed by a noun."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::ForNoun;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_fro_basic_correction() {
+ assert_suggestion_result(
+ "I got a text fro Sarah.",
+ ForNoun::default(),
+ "I got a text for Sarah.",
+ );
+ }
+
+ #[test]
+ fn allows_for_clean() {
+ assert_lint_count("I got a text for Sarah.", ForNoun::default(), 0);
+ }
+
+ #[test]
+ fn corrects_fro_sure() {
+ assert_suggestion_result(
+ "He was away fro sure!",
+ ForNoun::default(),
+ "He was away for sure!",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 1313fc45e..aebe103b3 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -24,6 +24,7 @@ use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::expand_time_shorthands::ExpandTimeShorthands;
+use super::for_noun::ForNoun;
use super::hedging::Hedging;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
@@ -341,6 +342,7 @@ impl LintGroup {
insert_pattern_rule!(Hedging, true);
insert_pattern_rule!(ExpandTimeShorthands, true);
insert_pattern_rule!(ModalOf, true);
+ insert_pattern_rule!(ForNoun, true);
out.add("SpellCheck", Box::new(SpellCheck::new(dictionary, dialect)));
out.config.set_rule_enabled("SpellCheck", true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 54494d268..4de5af0bf 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -18,6 +18,7 @@ mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
mod expand_time_shorthands;
+mod for_noun;
mod hedging;
mod hereby;
mod hop_hope;
@@ -80,6 +81,7 @@ pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use expand_time_shorthands::ExpandTimeShorthands;
+pub use for_noun::ForNoun;
pub use hedging::Hedging;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 32ae629ba..66690ca5c 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -799,18 +799,6 @@ pub fn lint_group() -> LintGroup {
"Add `a` to form `in quite a while`, clarifying the duration.",
"Corrects `in quite awhile` => `in quite a while` by inserting the missing article."
),
- "AwayFrom" => (
- ["away fro"],
- ["away from"],
- "Use `away from` to indicate distance or separation.",
- "Corrects a common slip of `away fro` to the standard `away from`."
- ),
- "ForOrFrom" => (
- ["fro"],
- ["for", "from"],
- "Did you mean `for` or `from`? Check context.",
- "Offers corrections for `fro`, ensuring modern usage rather than the archaic or idiomatic form."
- ),
"HumanBeings" => (
["human's beings", "humans beings"],
["human beings"],
@@ -1521,24 +1509,6 @@ mod tests {
);
}
- #[test]
- fn test_away_from() {
- assert_suggestion_result(
- "Keep it away fro the children.",
- lint_group(),
- "Keep it away from the children.",
- );
- }
-
- #[test]
- fn test_for_or_from() {
- assert_suggestion_result(
- "I got a text fro Sarah.",
- lint_group(),
- "I got a text for Sarah.",
- );
- }
-
#[test]
fn test_human_beings() {
assert_suggestion_result(