Skip to content

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

feat(core): create rule for the possessive version of "you"

Elijah Potter9a8ec67f068e
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index d30137ea8..1afb337ad 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -28,6 +28,7 @@ mod oxford_comma;
mod pattern_linter;
mod phrase_corrections;
mod plural_conjugate;
+mod possessive_your;
mod pronoun_contraction;
mod proper_noun_capitalization_linters;
mod repeated_words;
@@ -71,6 +72,7 @@ pub use phrase_corrections::{
ThatChallenged, TurnItOff,
};
pub use plural_conjugate::PluralConjugate;
+pub use possessive_your::PossessiveYour;
pub use pronoun_contraction::PronounContraction;
pub use proper_noun_capitalization_linters::{
AmazonNames, Americas, AppleNames, AzureNames, ChineseCommunistParty, GoogleNames, Holidays,
diff --git a/harper-core/src/linting/possessive_your.rs b/harper-core/src/linting/possessive_your.rs
new file mode 100644
index 000000000..27b17e7b5
--- /dev/null
+++ b/harper-core/src/linting/possessive_your.rs
@@ -0,0 +1,65 @@
+use crate::{
+ patterns::{Pattern, SequencePattern},
+ Token,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct PossessiveYour {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for PossessiveYour {
+ fn default() -> Self {
+ let pattern = SequencePattern::aco("you").then_whitespace().then(Box::new(
+ |tok: &Token, _source: &[char]| tok.kind.is_noun() && !tok.kind.is_verb(),
+ ));
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for PossessiveYour {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
+ let span = matched_tokens.first().unwrap().span;
+ let orig_chars = span.get_content(source);
+
+ Lint {
+ span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![
+ Suggestion::replace_with_match_case("your".chars().collect(), orig_chars),
+ Suggestion::replace_with_match_case("you're an".chars().collect(), orig_chars),
+ ],
+ message: "The possesive version of this word is more common in this context."
+ .to_owned(),
+ ..Default::default()
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "The possessive version of `you` is more common before nouns."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::PossessiveYour;
+
+ #[test]
+ fn your_comments() {
+ assert_suggestion_result(
+ "You comments may end up in the documentation.",
+ PossessiveYour::default(),
+ "Your comments may end up in the documentation.",
+ );
+ }
+}