Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): edge-case in `ShouldContract` rule (#1714)
Elijah Potter902340936cd1
diff --git a/harper-core/src/linting/pronoun_contraction/should_contract.rs b/harper-core/src/linting/pronoun_contraction/should_contract.rsindex 1c8bb2d96..62b799841 100644--- a/harper-core/src/linting/pronoun_contraction/should_contract.rs+++ b/harper-core/src/linting/pronoun_contraction/should_contract.rs@@ -1,7 +1,11 @@+use std::sync::Arc;+use crate::TokenKind;+use crate::expr::AnchorStart;use crate::expr::Expr;+use crate::expr::OwnedExprExt;use crate::expr::SequenceExpr;-use crate::{CharStringExt, Token, patterns::WordSet};+use crate::{Token, patterns::WordSet};use crate::Lint;use crate::linting::{ExprLinter, LintKind, Suggestion};@@ -17,31 +21,42 @@ pub struct ShouldContract {impl Default for ShouldContract {fn default() -> Self {+ let cap = Arc::new(+ SequenceExpr::default()+ .then(WordSet::new(&["your", "were"]))+ .then_whitespace()+ .then_kind_is_but_is_not(+ TokenKind::is_non_quantifier_determiner,+ TokenKind::is_pronoun,+ )+ .then_whitespace()+ .then_adjective(),+ );++ let start = SequenceExpr::default().then(AnchorStart).then(cap.clone());+ let mid = SequenceExpr::default()+ .then_unless(WordSet::new(&["what"]))+ .t_ws()+ .then(cap);+Self {- expr: Box::new(- SequenceExpr::default()- .then(WordSet::new(&["your", "were"]))- .then_whitespace()- .then_kind_is_but_is_not(- TokenKind::is_non_quantifier_determiner,- TokenKind::is_pronoun,- )- .then_whitespace()- .then_adjective(),- ),+ expr: Box::new(start.or(mid)),}}}impl ShouldContract {- fn mistake_to_correct(mistake: &str) -> impl Iterator<Item = Vec<char>> {- match mistake.to_lowercase().as_str() {+ fn mistake_to_correct(mistake: &str) -> Option<Vec<Vec<char>>> {+ let words = match mistake.to_lowercase().as_str() {"your" => vec!["you're", "you are"],"were" => vec!["we're", "we are"],- _ => panic!("The pattern in this linter should make a fall-through impossible."),+ _ => return None,}.into_iter().map(|v| v.chars().collect())+ .collect();++ Some(words)}}@@ -51,13 +66,30 @@ impl ExprLinter for ShouldContract {}fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {- let mistake = matched_tokens[0].span.get_content(source);+ // Locate the mistake+ let possible_mistakes = [matched_tokens[0].span, matched_tokens[1].span];++ let mut correct = None;+ let mut span = None;++ for p_mist in possible_mistakes {+ let mistake = p_mist.get_content_string(source);+ let correct_cand = Self::mistake_to_correct(&mistake);+ if correct_cand.is_some() {+ correct = correct_cand;+ span = Some(p_mist);+ }+ }++ let correct = correct?;+ let span = span?;Some(Lint {- span: matched_tokens[0].span,+ span,lint_kind: LintKind::WordChoice,- suggestions: Self::mistake_to_correct(&mistake.to_lower().to_string())- .map(|v| Suggestion::replace_with_match_case(v, mistake))+ suggestions: correct+ .into_iter()+ .map(|v| Suggestion::replace_with_match_case(v, span.get_content(source))).collect(),message: "Use the contraction or separate the words instead.".to_string(),priority: 31,@@ -126,4 +158,9 @@ mod tests {assert_no_lints("Were any other toys fun?", ShouldContract::default());assert_no_lints("You were his closest friend.", ShouldContract::default());}++ #[test]+ fn allows_issue_1673() {+ assert_no_lints("What were the action items?", ShouldContract::default());+ }}diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex 65c860727..07870442b 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -1048,17 +1048,6 @@ Suggest:-Lint: WordChoice (31 priority)-Message: |- 731 | nerves of her body were continually smouldering. She smiled slowly and, walking- 732 | through her husband as if he were a ghost, shook hands with Tom, looking him- | ^~~~ Use the contraction or separate the words instead.-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: Spelling (63 priority)Message: |756 | “Terrible place, isn’t it,” said Tom, exchanging a frown with Doctor Eckleburg.@@ -1326,17 +1315,6 @@ Suggest:-Lint: WordChoice (31 priority)-Message: |- 1077 | janitor and sent him for some celebrated sandwiches, which were a complete- | ^~~~ Use the contraction or separate the words instead.- 1078 | supper in themselves. I wanted to get out and walk eastward toward the park-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: Readability (127 priority)Message: |1078 | supper in themselves. I wanted to get out and walk eastward toward the park@@ -3721,16 +3699,6 @@ Suggest:-Lint: WordChoice (31 priority)-Message: |- 2447 | “When I said you were a particular friend of Tom’s, he started to abandon the- | ^~~~ Use the contraction or separate the words instead.-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: Spelling (63 priority)Message: |2455 | hard, limited person, who dealt in universal scepticism, and who leaned back@@ -4251,16 +4219,6 @@ Suggest:-Lint: WordChoice (31 priority)-Message: |- 3057 | an outlet for his imagination; they were a satisfactory hint of the unreality of- | ^~~~ Use the contraction or separate the words instead.-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: WordChoice (63 priority)Message: |3073 | tried to separate him from his money. The none too savory ramifications by which@@ -4400,16 +4358,6 @@ Message: |-Lint: WordChoice (31 priority)-Message: |- 3123 | They were a party of three on horseback—Tom and a man named Sloane and a pretty- | ^~~~ Use the contraction or separate the words instead.-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: Repetition (127 priority)Message: |3135 | uneasy anyhow until he had given them something, realizing in a vague way that@@ -4465,17 +4413,6 @@ Message: |-Lint: WordChoice (31 priority)-Message: |- 3235 | in my memory from Gatsby’s other parties that summer. There were the same- | ^~~~ Use the contraction or separate the words instead.- 3236 | people, or at least the same sort of people, the same profusion of champagne,-Suggest:- - Replace with: “we're”- - Replace with: “we are”---Lint: Readability (127 priority)Message: |3238 | air, a pervading harshness that hadn’t been there before. Or perhaps I had