Skip to content

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

feat(core): `ItsContraction`

Elijah Potter79e3f808da99
diff --git a/harper-core/src/linting/its_contraction.rs b/harper-core/src/linting/its_contraction.rs
new file mode 100644
index 000000000..8e214c8d8
--- /dev/null
+++ b/harper-core/src/linting/its_contraction.rs
@@ -0,0 +1,99 @@
+use crate::{
+ Token,
+ linting::{Lint, LintKind, PatternLinter, Suggestion},
+ patterns::{Pattern, SequencePattern, WordSet},
+};
+
+pub struct ItsContraction {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for ItsContraction {
+ fn default() -> Self {
+ let its = WordSet::new(&["its"]);
+ let verbs = WordSet::new(&["had", "been", "got"]);
+ let pattern = SequencePattern::default()
+ .then(its)
+ .then_whitespace()
+ .then(verbs);
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for ItsContraction {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], source: &[char]) -> Option<Lint> {
+ let offender = toks.first()?;
+ let offender_chars = offender.span.get_content(source);
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![
+ Suggestion::replace_with_match_case_str("it's", offender_chars),
+ Suggestion::replace_with_match_case_str("it has", offender_chars),
+ ],
+ message: "Use `it's` (short for `it has`) here, not the possessive `its`.".to_owned(),
+ priority: 54,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Detects the possessive `its` before `had`, `been`, or `got` and offers `it's` or `it has`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::ItsContraction;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn fix_had() {
+ assert_suggestion_result(
+ "Its had an enormous effect.",
+ ItsContraction::default(),
+ "It's had an enormous effect.",
+ );
+ }
+
+ #[test]
+ fn fix_been() {
+ assert_suggestion_result(
+ "Its been months since we spoke.",
+ ItsContraction::default(),
+ "It's been months since we spoke.",
+ );
+ }
+
+ #[test]
+ fn fix_got() {
+ assert_suggestion_result(
+ "I think its got nothing to do with us.",
+ ItsContraction::default(),
+ "I think it's got nothing to do with us.",
+ );
+ }
+
+ #[test]
+ fn ignore_correct_contraction() {
+ assert_lint_count(
+ "It's been a long year for everyone.",
+ ItsContraction::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn ignore_possessive() {
+ assert_lint_count(
+ "The company revised its policies last week.",
+ ItsContraction::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 80dc0248c..5470ed84b 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -36,6 +36,7 @@ use super::hop_hope::HopHope;
use super::how_to::HowTo;
use super::hyphenate_number_day::HyphenateNumberDay;
use super::inflected_verb_after_to::InflectedVerbAfterTo;
+use super::its_contraction::ItsContraction;
use super::left_right_hand::LeftRightHand;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
@@ -322,6 +323,7 @@ impl LintGroup {
insert_struct_rule!(ElsePossessive, true);
insert_pattern_rule!(WinPrize, true);
insert_pattern_rule!(AskNoPreposition, true);
+ insert_pattern_rule!(ItsContraction, true);
insert_struct_rule!(CommaFixes, true);
insert_struct_rule!(CompoundNouns, true);
insert_pattern_rule!(Confident, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 560a3ff45..bdd079303 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -32,6 +32,7 @@ mod hyphenate_number_day;
mod inflected_verb_after_to;
mod it_is;
mod it_would_be;
+mod its_contraction;
mod left_right_hand;
mod lets_confusion;
mod likewise;
@@ -108,6 +109,7 @@ pub use hop_hope::HopHope;
pub use how_to::HowTo;
pub use hyphenate_number_day::HyphenateNumberDay;
pub use inflected_verb_after_to::InflectedVerbAfterTo;
+pub use its_contraction::ItsContraction;
pub use left_right_hand::LeftRightHand;
pub use lets_confusion::LetsConfusion;
pub use likewise::Likewise;