harper_core/linting/
likewise.rs1use crate::{
2 Token, TokenStringExt,
3 patterns::{All, Invert, Pattern, SequencePattern},
4};
5
6use super::{Lint, LintKind, PatternLinter, Suggestion};
7
8pub struct Likewise {
9 pattern: Box<dyn Pattern>,
10}
11impl Default for Likewise {
12 fn default() -> Self {
13 let mut pattern = All::default();
14
15 pattern.add(Box::new(
16 SequencePattern::aco("like").then_whitespace().t_aco("wise"),
17 ));
18
19 pattern.add(Box::new(Invert::new(
20 SequencePattern::default()
21 .then_anything()
22 .then_whitespace()
23 .then_anything()
24 .then_whitespace()
25 .then_noun(),
26 )));
27
28 Self {
29 pattern: Box::new(pattern),
30 }
31 }
32}
33impl PatternLinter for Likewise {
34 fn pattern(&self) -> &dyn Pattern {
35 self.pattern.as_ref()
36 }
37 fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
38 let span = matched_tokens.span()?;
39 let orig_chars = span.get_content(source);
40 Some(Lint {
41 span,
42 lint_kind: LintKind::WordChoice,
43 suggestions: vec![Suggestion::replace_with_match_case(
44 "likewise".chars().collect(),
45 orig_chars,
46 )],
47 message: format!("Did you mean the closed compound `{}`?", "likewise"),
48 ..Default::default()
49 })
50 }
51 fn description(&self) -> &'static str {
52 "Looks for incorrect spacing inside the closed compound `likewise`."
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use crate::linting::tests::assert_suggestion_result;
59
60 use super::Likewise;
61
62 #[test]
63 fn wise_men() {
64 assert_suggestion_result(
65 "Like wise men, we waited.",
66 Likewise::default(),
67 "Like wise men, we waited.",
68 );
69 }
70
71 #[test]
72 fn like_wise() {
73 assert_suggestion_result(
74 "He acted, like wise, without hesitation.",
75 Likewise::default(),
76 "He acted, likewise, without hesitation.",
77 );
78 }
79}