|
| 1 | +use crate::{ |
| 2 | + Dialect, Token, |
| 3 | + expr::{Expr, SequenceExpr}, |
| 4 | + linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk}, |
| 5 | +}; |
| 6 | + |
| 7 | +pub struct FedUpWith { |
| 8 | + expr: Box<dyn Expr>, |
| 9 | + dialect: Dialect, |
| 10 | +} |
| 11 | + |
| 12 | +impl FedUpWith { |
| 13 | + pub fn new(dialect: Dialect) -> Self { |
| 14 | + let expr = SequenceExpr::fixed_phrase("fed up of"); |
| 15 | + |
| 16 | + Self { |
| 17 | + expr: Box::new(expr), |
| 18 | + dialect, |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl ExprLinter for FedUpWith { |
| 24 | + type Unit = Chunk; |
| 25 | + |
| 26 | + fn expr(&self) -> &dyn Expr { |
| 27 | + self.expr.as_ref() |
| 28 | + } |
| 29 | + |
| 30 | + fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> { |
| 31 | + if self.dialect == Dialect::British { |
| 32 | + return None; |
| 33 | + } |
| 34 | + |
| 35 | + let oftok = toks.last()?; |
| 36 | + let ofspan = oftok.span; |
| 37 | + |
| 38 | + Some(Lint { |
| 39 | + span: ofspan, |
| 40 | + lint_kind: LintKind::Usage, |
| 41 | + suggestions: vec![Suggestion::replace_with_match_case_str( |
| 42 | + "with", |
| 43 | + ofspan.get_content(src), |
| 44 | + )], |
| 45 | + message: "`Fed up of` is not accepted outside of British English.".to_string(), |
| 46 | + ..Default::default() |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + fn description(&self) -> &str { |
| 51 | + "Corrects `fed up of` to `fed up with` in dialects other than British English." |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::FedUpWith; |
| 58 | + use crate::Dialect; |
| 59 | + use crate::linting::tests::{assert_no_lints, assert_suggestion_result}; |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn correct_fed_up_of_in_us_english() { |
| 63 | + assert_suggestion_result( |
| 64 | + "I am fed up of Bugzilla reports being ignored.", |
| 65 | + FedUpWith::new(Dialect::American), |
| 66 | + "I am fed up with Bugzilla reports being ignored.", |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn correct_fed_up_of_in_canadian_english() { |
| 72 | + assert_suggestion_result( |
| 73 | + "Fed up of long links ??? Use ✨ Linsh ✨, a CLI tool to shorten links.", |
| 74 | + FedUpWith::new(Dialect::Canadian), |
| 75 | + "Fed up with long links ??? Use ✨ Linsh ✨, a CLI tool to shorten links.", |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn correct_fed_up_of_in_aus_english() { |
| 81 | + assert_suggestion_result( |
| 82 | + "Fed up of the lack of Twitter embedded timeline styling options?", |
| 83 | + FedUpWith::new(Dialect::Australian), |
| 84 | + "Fed up with the lack of Twitter embedded timeline styling options?", |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn correct_fed_up_of_in_indian_english() { |
| 90 | + assert_suggestion_result( |
| 91 | + "I got fed up of finding my IP (v4) address in the big pile of text that ifconfig outputs on OS X.", |
| 92 | + FedUpWith::new(Dialect::Indian), |
| 93 | + "I got fed up with finding my IP (v4) address in the big pile of text that ifconfig outputs on OS X.", |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn dont_flag_fed_up_of_in_british_english() { |
| 99 | + assert_no_lints( |
| 100 | + "Fed up of having to repeat the same actions for installing webmin so here's a script for 16.04+", |
| 101 | + FedUpWith::new(Dialect::British), |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments