Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): create hyphenation rule for `out-of-date`
Elijah Potter83738e3ca79c
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 41e2c44f5..55ca43d0c 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -37,6 +37,7 @@ use super::merge_words::MergeWords;use super::multiple_sequential_pronouns::MultipleSequentialPronouns;use super::nobody::Nobody;use super::number_suffix_capitalization::NumberSuffixCapitalization;+use super::out_of_date::OutOfDate;use super::phrase_corrections::BaitedBreath;use super::phrase_corrections::BareInMind;use super::phrase_corrections::EludedTo;@@ -194,6 +195,7 @@ macro_rules! create_lint_group_config {}create_lint_group_config!(+ OutOfDate => true,Desktop => true,Laptop => true,ThenThan => true,diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex d8514be31..05f2be2cd 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -33,6 +33,7 @@ mod multiple_sequential_pronouns;mod no_oxford_comma;mod nobody;mod number_suffix_capitalization;+mod out_of_date;mod oxford_comma;mod pattern_linter;mod phrase_corrections;@@ -96,6 +97,7 @@ pub use multiple_sequential_pronouns::MultipleSequentialPronouns;pub use no_oxford_comma::NoOxfordComma;pub use nobody::Nobody;pub use number_suffix_capitalization::NumberSuffixCapitalization;+pub use out_of_date::OutOfDate;pub use oxford_comma::OxfordComma;pub use pattern_linter::PatternLinter;pub use phrase_corrections::BaitedBreath;diff --git a/harper-core/src/linting/out_of_date.rs b/harper-core/src/linting/out_of_date.rsnew file mode 100644index 000000000..6f2a4dce7--- /dev/null+++ b/harper-core/src/linting/out_of_date.rs@@ -0,0 +1,83 @@+use crate::{+ patterns::{EitherPattern, ExactPhrase, Pattern},+ Token, TokenStringExt,+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++pub struct OutOfDate {+ pattern: Box<dyn Pattern>,+}++impl Default for OutOfDate {+ fn default() -> Self {+ let pattern = EitherPattern::new(vec![+ Box::new(ExactPhrase::from_phrase("out of date")),+ Box::new(ExactPhrase::from_phrase("out-of date")),+ Box::new(ExactPhrase::from_phrase("out of-date")),+ ]);++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for OutOfDate {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let span = matched_tokens.span().unwrap();+ let problem_text = span.get_content(source);++ Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::replace_with_match_case(+ "out-of-date".chars().collect(),+ problem_text,+ )],+ message: "Did you mean the compound adjective?".to_owned(),+ priority: 31,+ }+ }++ fn description(&self) -> &'static str {+ "Ensures that the phrase `out of date` is written with a hyphen as `out-of-date` when used as a compound adjective."+ }+}++#[cfg(test)]+mod tests {+ use super::OutOfDate;+ use crate::linting::tests::assert_suggestion_result;++ #[test]+ fn corrects_out_of_date() {+ assert_suggestion_result(+ "The software is out of date.",+ OutOfDate::default(),+ "The software is out-of-date.",+ );+ }++ #[test]+ fn corrects_out_of_date_with_variation() {+ assert_suggestion_result(+ "This information is out of-date.",+ OutOfDate::default(),+ "This information is out-of-date.",+ );+ }++ #[test]+ fn allows_correct_usage() {+ assert_suggestion_result(+ "The guidelines are out-of-date.",+ OutOfDate::default(),+ "The guidelines are out-of-date.",+ );+ }+}