Skip to content

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

fix(core): make the `ItsPossessive` rule more robust (#2769)

Elijah Potter742933ebdb85
diff --git a/harper-core/src/linting/its_possessive.rs b/harper-core/src/linting/its_possessive.rs
index a65f01159..1524bbcee 100644
--- a/harper-core/src/linting/its_possessive.rs
+++ b/harper-core/src/linting/its_possessive.rs
@@ -22,14 +22,14 @@ impl Default for ItsPossessive {
fn default() -> Self {
let mut map = ExprMap::default();
- let adj_term = SequenceExpr::default()
+ let adj_term_mid_sentence = SequenceExpr::default()
.t_ws()
.then(UPOSSet::new(&[UPOS::ADJ]));
let mid_sentence = SequenceExpr::with(UPOSSet::new(&[UPOS::VERB, UPOS::ADP]))
.t_ws()
.t_aco("it's")
- .then_optional(adj_term)
+ .then_optional(adj_term_mid_sentence)
.t_ws()
.then(
UPOSSet::new(&[UPOS::NOUN, UPOS::PROPN]).or(|tok: &Token, _: &[char]| {
@@ -39,10 +39,31 @@ impl Default for ItsPossessive {
map.insert(mid_sentence, 2);
- let start_of_sentence = SequenceExpr::with(AnchorStart)
+ let start_of_sentence_noun = SequenceExpr::with(AnchorStart)
.t_aco("it's")
.t_ws()
- .then(UPOSSet::new(&[UPOS::ADJ, UPOS::NOUN, UPOS::PROPN]))
+ .then(
+ UPOSSet::new(&[UPOS::NOUN, UPOS::PROPN]).or(|tok: &Token, _: &[char]| {
+ tok.kind.as_number().is_some_and(|n| n.suffix.is_some())
+ }),
+ )
+ .then_unless(SequenceExpr::default().t_ws().then(UPOSSet::new(&[
+ UPOS::VERB,
+ UPOS::PART,
+ UPOS::ADP,
+ UPOS::NOUN,
+ UPOS::PRON,
+ UPOS::SCONJ,
+ UPOS::CCONJ,
+ UPOS::ADV,
+ ])));
+
+ map.insert(start_of_sentence_noun, 0);
+
+ let start_of_sentence_adjective = SequenceExpr::with(AnchorStart)
+ .t_aco("it's")
+ .t_ws()
+ .then(UPOSSet::new(&[UPOS::ADJ]))
.t_ws()
.then_unless(UPOSSet::new(&[
UPOS::VERB,
@@ -55,7 +76,30 @@ impl Default for ItsPossessive {
UPOS::ADV,
]));
- map.insert(start_of_sentence, 0);
+ map.insert(start_of_sentence_adjective, 0);
+
+ let start_of_chunk_after_conjunction = SequenceExpr::with(AnchorStart)
+ .then(UPOSSet::new(&[UPOS::CCONJ]))
+ .t_ws()
+ .t_aco("it's")
+ .t_ws()
+ .then(
+ UPOSSet::new(&[UPOS::NOUN, UPOS::PROPN]).or(|tok: &Token, _: &[char]| {
+ tok.kind.as_number().is_some_and(|n| n.suffix.is_some())
+ }),
+ )
+ .then_unless(SequenceExpr::default().t_ws().then(UPOSSet::new(&[
+ UPOS::VERB,
+ UPOS::PART,
+ UPOS::ADP,
+ UPOS::NOUN,
+ UPOS::PRON,
+ UPOS::SCONJ,
+ UPOS::CCONJ,
+ UPOS::ADV,
+ ])));
+
+ map.insert(start_of_chunk_after_conjunction, 2);
let special = SequenceExpr::aco("it's").t_ws().t_aco("various");
@@ -352,4 +396,22 @@ mod tests {
fn dont_flag_issue_1722_its_big_enough() {
assert_no_lints("It's big enough.", ItsPossessive::default());
}
+
+ #[test]
+ fn flags_all_its_possessive_in_list() {
+ assert_lint_count(
+ "Understand it's code, it's values, and it's purpose.",
+ ItsPossessive::default(),
+ 3,
+ );
+ }
+
+ #[test]
+ fn fixes_all_its_possessive_in_list() {
+ assert_suggestion_result(
+ "Understand it's code, it's values, and it's purpose.",
+ ItsPossessive::default(),
+ "Understand its code, its values, and its purpose.",
+ );
+ }
}