Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): added `Whereas` rule
Elijah Potter883640013167
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex d26cf0c6b..ce9608014 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -37,6 +37,7 @@ use super::terminating_conjunctions::TerminatingConjunctions;use super::that_which::ThatWhich;use super::unclosed_quotes::UnclosedQuotes;use super::use_genitive::UseGenitive;+use super::whereas::Whereas;use super::wrong_quotes::WrongQuotes;use super::{CurrencyPlacement, Lint, Linter, NoOxfordComma, OxfordComma};use crate::{Dictionary, Document};@@ -160,6 +161,7 @@ macro_rules! create_lint_group_config {}create_lint_group_config!(+ Whereas => true,SpelledNumbers => false,AnA => true,SentenceCapitalization => true,@@ -219,7 +221,7 @@ create_lint_group_config!(SneakingSuspicion => true,SupposeTo => true,SpecialAttention => true,- ThanOthers => true+ ThanOthers => true,);impl<T: Dictionary + Default> Default for LintGroup<T> {diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 46ee2ed4a..d42d6d454 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -167,3 +167,5 @@ mod tests {assert_lint_count(&transformed_str, linter, 0);}}+pub use whereas::Whereas;+mod whereas;diff --git a/harper-core/src/linting/whereas.rs b/harper-core/src/linting/whereas.rsnew file mode 100644index 000000000..651efd5f9--- /dev/null+++ b/harper-core/src/linting/whereas.rs@@ -0,0 +1,65 @@+use crate::{+ patterns::{Pattern, SequencePattern},+ Token, TokenStringExt,+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++pub struct Whereas {+ pattern: Box<dyn Pattern>,+}++impl Default for Whereas {+ fn default() -> Self {+ let pattern = SequencePattern::default()+ .t_aco("where")+ .then_whitespace()+ .t_aco("as");++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for Whereas {+ 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 orig_chars = span.get_content(source);++ Lint {+ span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ vec!['w', 'h', 'e', 'r', 'e', 'a', 's'],+ orig_chars,+ )],+ message: "`Whereas` is commonly mistaken for `where as`.".to_owned(),+ ..Default::default()+ }+ }++ fn description(&self) -> &'static str {+ "The Whereas rule is designed to identify instances where the phrase `where as` is used in text and suggests replacing it with the single word `whereas`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::Whereas;++ #[test]+ fn where_as() {+ assert_suggestion_result(+ "Dogs love playing fetch, where as cats are more independent creatures.",+ Whereas::default(),+ "Dogs love playing fetch, whereas cats are more independent creatures.",+ );+ }+}diff --git a/justfile b/justfileindex 924762344..66a59edda 100644--- a/justfile+++ b/justfile@@ -290,3 +290,15 @@ fuzz:exit $?fidone++registerlinter module name:+ #! /bin/bash++ D="{{justfile_directory()}}/harper-core/src/linting"++ echo "pub use {{module}}::{{name}};" >> "$D/mod.rs"+ echo "use super::{{module}}::{{name}};" >> "$D/lint_group.rs"+ echo "mod {{module}};" >> "$D/mod.rs"++ sed -i "/create_lint_group_config\!/a {{name}} => true," "$D/lint_group.rs"+ just formatdiff --git a/rustfmt.toml b/rustfmt.tomlindex 4420db53b..fea0452b4 100644--- a/rustfmt.toml+++ b/rustfmt.toml@@ -1,2 +1,3 @@newline_style = "Unix"use_field_init_shorthand = true+reorder_imports = true