|
| 1 | +use crate::{ |
| 2 | + Token, TokenStringExt, |
| 3 | + expr::{Expr, SequenceExpr}, |
| 4 | + linting::{ExprLinter, Lint, LintKind, Suggestion}, |
| 5 | + patterns::{NominalPhrase, WordSet}, |
| 6 | +}; |
| 7 | + |
| 8 | +/// Linter that corrects "take X serious" to "take X seriously". |
| 9 | +/// |
| 10 | +/// This linter identifies and corrects the common mistake of using the adjective "serious" |
| 11 | +/// instead of the adverb "seriously" in phrases like "take it serious". |
| 12 | +pub struct TakeSerious { |
| 13 | + expr: Box<dyn Expr>, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for TakeSerious { |
| 17 | + /// Creates a new `TakeSerious` instance with the default pattern. |
| 18 | + /// |
| 19 | + /// The pattern matches: |
| 20 | + /// - Any form of "take" (take/takes/taking/took/taken) |
| 21 | + /// - Followed by a nominal phrase |
| 22 | + /// - Ending with "serious" |
| 23 | + fn default() -> Self { |
| 24 | + let pattern = SequenceExpr::default() |
| 25 | + .then(WordSet::new(&["take", "taken", "takes", "taking", "took"])) |
| 26 | + .t_ws() |
| 27 | + .then(NominalPhrase) |
| 28 | + .t_ws() |
| 29 | + .t_aco("serious"); |
| 30 | + |
| 31 | + Self { |
| 32 | + expr: Box::new(pattern), |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl ExprLinter for TakeSerious { |
| 38 | + fn expr(&self) -> &dyn Expr { |
| 39 | + self.expr.as_ref() |
| 40 | + } |
| 41 | + |
| 42 | + fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> { |
| 43 | + let whole_phrase_span = matched_tokens.span()?; |
| 44 | + let all_but_last_token = matched_tokens[..matched_tokens.len() - 1].span()?; |
| 45 | + |
| 46 | + let mut sugg_value = all_but_last_token.get_content(source).to_vec(); |
| 47 | + sugg_value.extend_from_slice(&['s', 'e', 'r', 'i', 'o', 'u', 's', 'l', 'y']); |
| 48 | + |
| 49 | + let sugg_template = whole_phrase_span.get_content(source); |
| 50 | + |
| 51 | + let suggestions = vec![Suggestion::replace_with_match_case( |
| 52 | + sugg_value, |
| 53 | + sugg_template, |
| 54 | + )]; |
| 55 | + |
| 56 | + Some(Lint { |
| 57 | + span: whole_phrase_span, |
| 58 | + lint_kind: LintKind::WordChoice, |
| 59 | + suggestions, |
| 60 | + message: "Take seriously".to_string(), |
| 61 | + priority: 63, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + fn description(&self) -> &'static str { |
| 66 | + "Ensures the correct use of the adverb `seriously` instead of the adjective `serious` in phrases like `take it seriously`." |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use crate::linting::tests::assert_suggestion_result; |
| 73 | + |
| 74 | + use super::TakeSerious; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn take_it() { |
| 78 | + assert_suggestion_result( |
| 79 | + "I take it serious.", |
| 80 | + TakeSerious::default(), |
| 81 | + "I take it seriously.", |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + #[ignore = "'This' and 'that', which can be determiners and pronouns, are not handled properly by `NominalPhrase`"] |
| 87 | + fn take_this() { |
| 88 | + assert_suggestion_result( |
| 89 | + "What's more important is, that it's impossible to actually take this serious when ...", |
| 90 | + TakeSerious::default(), |
| 91 | + "What's more important is, that it's impossible to actually take this seriously when ...", |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn not_take_security() { |
| 97 | + assert_suggestion_result( |
| 98 | + "When you say someone does not take security serious you are being judgemental / destructive.", |
| 99 | + TakeSerious::default(), |
| 100 | + "When you say someone does not take security seriously you are being judgemental / destructive.", |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn we_take_security() { |
| 106 | + assert_suggestion_result( |
| 107 | + "We take security serious.", |
| 108 | + TakeSerious::default(), |
| 109 | + "We take security seriously.", |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn take_me() { |
| 115 | + assert_suggestion_result( |
| 116 | + "Yeah , don't take me serious , i do this as a hobby - jusspatel.", |
| 117 | + TakeSerious::default(), |
| 118 | + "Yeah , don't take me seriously , i do this as a hobby - jusspatel.", |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + #[ignore = "Passive voice and adverbs are not yet supported"] |
| 124 | + fn taken_adv() { |
| 125 | + assert_suggestion_result( |
| 126 | + "This is not meant to be taken overly serious", |
| 127 | + TakeSerious::default(), |
| 128 | + "This is not meant to be taken overly seriously", |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn takes_these_numbers() { |
| 134 | + assert_suggestion_result( |
| 135 | + "if a program actually takes these numbers serious the results could be catastrophic.", |
| 136 | + TakeSerious::default(), |
| 137 | + "if a program actually takes these numbers seriously the results could be catastrophic.", |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + #[ignore = "'No one' is not handled properly by `NominalPhrase`"] |
| 143 | + fn takes_bf() { |
| 144 | + assert_suggestion_result( |
| 145 | + "And obviously no one takes brainfuck serious as a language.", |
| 146 | + TakeSerious::default(), |
| 147 | + "And obviously no one takes brainfuck seriously as a language.", |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + #[ignore = "Adverbs are not yet supported"] |
| 153 | + fn taken_very() { |
| 154 | + assert_suggestion_result( |
| 155 | + "Hmm flaky soldering iron is something that must be taken very serious.", |
| 156 | + TakeSerious::default(), |
| 157 | + "Hmm flaky soldering iron is something that must be taken very seriously.", |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments