harper_core/linting/
despite_of.rs1use crate::{
2 Token, TokenStringExt,
3 patterns::{Pattern, SequencePattern},
4};
5
6use super::{Lint, LintKind, PatternLinter, Suggestion};
7
8pub struct DespiteOf {
9 pattern: Box<dyn Pattern>,
10}
11
12impl Default for DespiteOf {
13 fn default() -> Self {
14 let pattern = SequencePattern::aco("despite")
15 .then_whitespace()
16 .then_exact_word("of");
17
18 Self {
19 pattern: Box::new(pattern),
20 }
21 }
22}
23
24impl PatternLinter for DespiteOf {
25 fn pattern(&self) -> &dyn Pattern {
26 self.pattern.as_ref()
27 }
28
29 fn match_to_lint(&self, matched: &[Token], source: &[char]) -> Option<Lint> {
30 let span = matched.span()?;
31 let matched = span.get_content(source);
32
33 Some(Lint {
34 span,
35 lint_kind: LintKind::WordChoice,
36 suggestions: vec![
37 Suggestion::replace_with_match_case_str("despite", matched),
38 Suggestion::replace_with_match_case_str("in spite of", matched)
39 ],
40 message: "The phrase “despite of” is incorrect. Please use either “despite” or “in spite of” instead.".to_string(),
41 priority: 126,
42 })
43 }
44
45 fn description(&self) -> &'static str {
46 "Corrects the misuse of `despite of` and suggests the proper alternatives `despite` or `in spite of`."
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::DespiteOf;
53 use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
54
55 #[test]
56 fn catches_lowercase() {
57 assert_suggestion_result(
58 "The team performed well, despite of the difficulties they faced.",
59 DespiteOf::default(),
60 "The team performed well, despite the difficulties they faced.",
61 );
62 }
63
64 #[test]
65 fn catches_different_cases() {
66 assert_lint_count(
67 "Despite of the rain, we went for a walk.",
68 DespiteOf::default(),
69 1,
70 );
71 }
72
73 #[test]
74 fn likes_correction() {
75 assert_lint_count(
76 "The team performed well, despite the difficulties they faced. In spite of the rain, we went for a walk.",
77 DespiteOf::default(),
78 0,
79 );
80 }
81}