harper_core/linting/
whereas.rs

1use crate::{
2    Token, TokenStringExt,
3    patterns::{Pattern, SequencePattern},
4};
5
6use super::{Lint, LintKind, PatternLinter, Suggestion};
7
8pub struct Whereas {
9    pattern: Box<dyn Pattern>,
10}
11
12impl Default for Whereas {
13    fn default() -> Self {
14        let pattern = SequencePattern::default()
15            .t_aco("where")
16            .then_whitespace()
17            .t_aco("as");
18
19        Self {
20            pattern: Box::new(pattern),
21        }
22    }
23}
24
25impl PatternLinter for Whereas {
26    fn pattern(&self) -> &dyn Pattern {
27        self.pattern.as_ref()
28    }
29
30    fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
31        let span = matched_tokens.span()?;
32        let orig_chars = span.get_content(source);
33
34        Some(Lint {
35            span,
36            lint_kind: LintKind::WordChoice,
37            suggestions: vec![Suggestion::replace_with_match_case(
38                vec!['w', 'h', 'e', 'r', 'e', 'a', 's'],
39                orig_chars,
40            )],
41            message: "`Whereas` is commonly mistaken for `where as`.".to_owned(),
42            ..Default::default()
43        })
44    }
45
46    fn description(&self) -> &'static str {
47        "The Whereas rule is designed to identify instances where the phrase `where as` is used in text and suggests replacing it with the single word `whereas`."
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::linting::tests::assert_suggestion_result;
54
55    use super::Whereas;
56
57    #[test]
58    fn where_as() {
59        assert_suggestion_result(
60            "Dogs love playing fetch, where as cats are more independent creatures.",
61            Whereas::default(),
62            "Dogs love playing fetch, whereas cats are more independent creatures.",
63        );
64    }
65}