harper_core/linting/
left_right_hand.rs

1use crate::{
2    Token,
3    patterns::{Pattern, SequencePattern, WordSet},
4};
5
6use super::{Lint, LintKind, PatternLinter, Suggestion};
7
8pub struct LeftRightHand {
9    pattern: Box<dyn Pattern>,
10}
11
12impl Default for LeftRightHand {
13    fn default() -> Self {
14        let pattern = SequencePattern::default()
15            .then(WordSet::new(&["left", "right"]))
16            .then_whitespace()
17            .t_aco("hand")
18            .then_whitespace()
19            .then_noun();
20
21        Self {
22            pattern: Box::new(pattern),
23        }
24    }
25}
26
27impl PatternLinter for LeftRightHand {
28    fn pattern(&self) -> &dyn Pattern {
29        self.pattern.as_ref()
30    }
31
32    fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {
33        let space = &matched_tokens[1];
34
35        Some(Lint {
36            span: space.span,
37            lint_kind: LintKind::Miscellaneous,
38            suggestions: vec![Suggestion::ReplaceWith(vec!['-'])],
39            message: "Use a hyphen in `left-hand` or `right-hand` when modifying a noun."
40                .to_owned(),
41            priority: 31,
42        })
43    }
44
45    fn description(&self) -> &'static str {
46        "Ensures `left hand` and `right hand` are hyphenated when used as adjectives before a noun, such as in `left-hand side` or `right-hand corner`."
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::LeftRightHand;
53    use crate::linting::tests::assert_suggestion_result;
54
55    #[test]
56    fn corrects_left_hand_side() {
57        assert_suggestion_result(
58            "You'll see it on the left hand side.",
59            LeftRightHand::default(),
60            "You'll see it on the left-hand side.",
61        );
62    }
63
64    #[test]
65    fn corrects_right_hand_corner() {
66        assert_suggestion_result(
67            "It's in the right hand corner.",
68            LeftRightHand::default(),
69            "It's in the right-hand corner.",
70        );
71    }
72
73    #[test]
74    fn does_not_correct_noun_usage() {
75        assert_suggestion_result(
76            "She raised her right hand.",
77            LeftRightHand::default(),
78            "She raised her right hand.",
79        );
80    }
81}