|
| 1 | +use crate::{ |
| 2 | + Token, TokenStringExt, |
| 3 | + expr::{Expr, FirstMatchOf, SequenceExpr}, |
| 4 | + linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk}, |
| 5 | +}; |
| 6 | + |
| 7 | +const CONTRACTION_AND_POSSESSIVE_ENDINGS: [&str; 7] = ["d", "ll", "m", "re", "s", "t", "ve"]; |
| 8 | + |
| 9 | +pub struct WrongApostrophe { |
| 10 | + expr: Box<dyn Expr>, |
| 11 | +} |
| 12 | + |
| 13 | +impl Default for WrongApostrophe { |
| 14 | + fn default() -> Self { |
| 15 | + Self { |
| 16 | + expr: Box::new(FirstMatchOf::new(vec![ |
| 17 | + Box::new( |
| 18 | + SequenceExpr::any_word() |
| 19 | + .then_semicolon() |
| 20 | + .then_word_set(&CONTRACTION_AND_POSSESSIVE_ENDINGS), |
| 21 | + ), |
| 22 | + Box::new( |
| 23 | + SequenceExpr::any_word() |
| 24 | + .then_acute() |
| 25 | + .then_word_set(&CONTRACTION_AND_POSSESSIVE_ENDINGS), |
| 26 | + ), |
| 27 | + ])), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl ExprLinter for WrongApostrophe { |
| 33 | + type Unit = Chunk; |
| 34 | + |
| 35 | + fn expr(&self) -> &dyn Expr { |
| 36 | + self.expr.as_ref() |
| 37 | + } |
| 38 | + |
| 39 | + fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> { |
| 40 | + let whole_span = toks.span()?; |
| 41 | + let base = &toks.first()?; |
| 42 | + let ending = &toks.last()?; |
| 43 | + |
| 44 | + let replacement_str = format!( |
| 45 | + "{}'{}", |
| 46 | + base.span.get_content_string(src).to_lowercase(), |
| 47 | + ending.span.get_content_string(src).to_lowercase() |
| 48 | + ); |
| 49 | + |
| 50 | + let mut lettercase_template = base.span.get_content(src).to_vec(); |
| 51 | + lettercase_template.extend_from_slice(ending.span.get_content(src)); |
| 52 | + |
| 53 | + Some(Lint { |
| 54 | + span: whole_span, |
| 55 | + lint_kind: LintKind::Typo, |
| 56 | + suggestions: vec![Suggestion::replace_with_match_case( |
| 57 | + replacement_str.chars().collect(), |
| 58 | + &lettercase_template, |
| 59 | + )], |
| 60 | + message: format!("Did you mean `{replacement_str}`?"), |
| 61 | + priority: 57, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + fn description(&self) -> &str { |
| 66 | + "Corrects semicolons or acute accents typed instead of apostrophes." |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::WrongApostrophe; |
| 73 | + use crate::linting::tests::{assert_lint_count, assert_suggestion_result}; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn fix_dont_with_semicolon_to_apostrophe() { |
| 77 | + assert_suggestion_result( |
| 78 | + "It's better if you don;t type like this.", |
| 79 | + WrongApostrophe::default(), |
| 80 | + "It's better if you don't type like this.", |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn ignore_correct() { |
| 86 | + assert_lint_count("I don't doubt it.", WrongApostrophe::default(), 0); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn fix_title_case() { |
| 91 | + assert_suggestion_result( |
| 92 | + "Don;t type like this.", |
| 93 | + WrongApostrophe::default(), |
| 94 | + "Don't type like this.", |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn fix_all_caps() { |
| 100 | + assert_suggestion_result( |
| 101 | + "DON;T TRY THIS AT HOME.", |
| 102 | + WrongApostrophe::default(), |
| 103 | + "DON'T TRY THIS AT HOME.", |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + #[ignore = "replace_with_match_case has a bug turning `I'll` into `I'LL`"] |
| 109 | + fn fix_ill_and_monkeys() { |
| 110 | + assert_suggestion_result( |
| 111 | + "Well I;ll be a monkey;s uncle!", |
| 112 | + WrongApostrophe::default(), |
| 113 | + "Well I'll be a monkey's uncle!", |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn fix_other_contractions_and_possessives() { |
| 119 | + assert_suggestion_result( |
| 120 | + "Let;s see if we;ve fixed patrakov;s bug. Fun wasn;t it?", |
| 121 | + WrongApostrophe::default(), |
| 122 | + "Let's see if we've fixed patrakov's bug. Fun wasn't it?", |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn corrects_ive_with_correct_capitalization() { |
| 128 | + assert_suggestion_result("I;ve", WrongApostrophe::default(), "I've"); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn fix_acute_dont() { |
| 133 | + assert_suggestion_result( |
| 134 | + "To see the list of available bikes for a location, you don´t need any authentication.", |
| 135 | + WrongApostrophe::default(), |
| 136 | + "To see the list of available bikes for a location, you don't need any authentication.", |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn fix_acute_im() { |
| 142 | + assert_suggestion_result( |
| 143 | + "In my research, I´m applying the latest generation of quantitative methods in epidemiology", |
| 144 | + WrongApostrophe::default(), |
| 145 | + "In my research, I'm applying the latest generation of quantitative methods in epidemiology", |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn fix_acute_its() { |
| 151 | + assert_suggestion_result( |
| 152 | + "and it´s auto-updated if that project is hosted here on github", |
| 153 | + WrongApostrophe::default(), |
| 154 | + "and it's auto-updated if that project is hosted here on github", |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn fix_acute_lets() { |
| 160 | + assert_suggestion_result( |
| 161 | + "Let´s now visit the main functionalities provided by GrimoireLab.", |
| 162 | + WrongApostrophe::default(), |
| 163 | + "Let's now visit the main functionalities provided by GrimoireLab.", |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn fix_acute_microsofts() { |
| 169 | + assert_suggestion_result( |
| 170 | + "Windows 11 Upgrade tool that bypasses new Microsoft´s requirements", |
| 171 | + WrongApostrophe::default(), |
| 172 | + "Windows 11 Upgrade tool that bypasses new Microsoft's requirements", |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn fix_acute_youre() { |
| 178 | + assert_suggestion_result( |
| 179 | + "You´re looking for clues, but you´re missing all the signs", |
| 180 | + WrongApostrophe::default(), |
| 181 | + "You're looking for clues, but you're missing all the signs", |
| 182 | + ); |
| 183 | + } |
| 184 | +} |
0 commit comments