harper_core/linting/
expr_linter.rs

1use crate::expr::{Expr, ExprExt};
2use blanket::blanket;
3
4use crate::{Document, LSend, Token, TokenStringExt};
5
6use super::{Lint, Linter};
7
8pub trait DocumentIterator {
9    type Unit;
10
11    fn iter_units<'a>(document: &'a Document) -> Box<dyn Iterator<Item = &'a [Token]> + 'a>;
12}
13
14/// Process text in chunks (clauses between commas)
15pub struct Chunk;
16/// Process text in full sentences
17pub struct Sentence;
18
19impl DocumentIterator for Chunk {
20    type Unit = Chunk;
21
22    fn iter_units<'a>(document: &'a Document) -> Box<dyn Iterator<Item = &'a [Token]> + 'a> {
23        Box::new(document.iter_chunks())
24    }
25}
26
27impl DocumentIterator for Sentence {
28    type Unit = Sentence;
29
30    fn iter_units<'a>(document: &'a Document) -> Box<dyn Iterator<Item = &'a [Token]> + 'a> {
31        Box::new(document.iter_sentences())
32    }
33}
34
35/// A trait that searches for tokens that fulfil [`Expr`]s in a [`Document`].
36///
37/// Makes use of [`TokenStringExt::iter_chunks`] by default, or [`TokenStringExt::iter_sentences`] to process either
38/// a chunk (clause) or a sentence at a time.
39#[blanket(derive(Box))]
40pub trait ExprLinter: LSend {
41    type Unit: DocumentIterator;
42
43    /// A simple getter for the expression you want Harper to search for.
44    fn expr(&self) -> &dyn Expr;
45    /// If any portions of a [`Document`] match [`Self::expr`], they are passed through [`ExprLinter::match_to_lint`]
46    /// or [`ExprLinter::match_to_lint_with_context`] to be transformed into a [`Lint`] for editor consumption.
47    ///
48    /// Transform matched tokens into a [`Lint`] for editor consumption.
49    ///
50    /// This is the simple version that only sees the matched tokens. For context-aware linting,
51    /// implement `match_to_lint_with_context` instead.
52    ///
53    /// Return `None` to skip producing a lint for this match.
54    fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
55        self.match_to_lint_with_context(matched_tokens, source, None)
56    }
57
58    /// Transform matched tokens into a [`Lint`] with access to surrounding context.
59    ///
60    /// The context provides access to tokens before and after the match. When implementing
61    /// this method, you can call `self.match_to_lint()` as a fallback if the context isn't needed.
62    ///
63    /// Return `None` to skip producing a lint for this match.
64    fn match_to_lint_with_context(
65        &self,
66        matched_tokens: &[Token],
67        source: &[char],
68        _context: Option<(&[Token], &[Token])>,
69    ) -> Option<Lint> {
70        // Default implementation falls back to the simple version
71        self.match_to_lint(matched_tokens, source)
72    }
73    /// A user-facing description of what kinds of grammatical errors this rule looks for.
74    /// It is usually shown in settings menus.
75    fn description(&self) -> &str;
76}
77
78/// Helper function to find the only occurrence of a token matching a predicate
79///
80/// Returns `Some(token)` if exactly one token matches the predicate, `None` otherwise.
81/// TODO: This can be used in the [`ThenThan`] linter when #1819 is merged.
82pub fn find_the_only_token_matching<'a, F>(
83    tokens: &'a [Token],
84    source: &[char],
85    predicate: F,
86) -> Option<&'a Token>
87where
88    F: Fn(&Token, &[char]) -> bool,
89{
90    let mut matches = tokens.iter().filter(|&tok| predicate(tok, source));
91    match (matches.next(), matches.next()) {
92        (Some(tok), None) => Some(tok),
93        _ => None,
94    }
95}
96
97impl<L, U> Linter for L
98where
99    L: ExprLinter<Unit = U>,
100    U: DocumentIterator,
101{
102    fn lint(&mut self, document: &Document) -> Vec<Lint> {
103        let mut lints = Vec::new();
104        let source = document.get_source();
105
106        for unit in U::iter_units(document) {
107            lints.extend(run_on_chunk(self, unit, source));
108        }
109
110        lints
111    }
112
113    fn description(&self) -> &str {
114        self.description()
115    }
116}
117
118pub fn run_on_chunk<'a>(
119    linter: &'a impl ExprLinter,
120    unit: &'a [Token],
121    source: &'a [char],
122) -> impl Iterator<Item = Lint> + 'a {
123    linter
124        .expr()
125        .iter_matches(unit, source)
126        .filter_map(|match_span| {
127            linter.match_to_lint_with_context(
128                &unit[match_span.start..match_span.end],
129                source,
130                Some((&unit[..match_span.start], &unit[match_span.end..])),
131            )
132        })
133}
134
135#[cfg(test)]
136mod tests_context {
137    use crate::expr::{Expr, FixedPhrase};
138    use crate::linting::expr_linter::{Chunk, Sentence};
139    use crate::linting::tests::assert_suggestion_result;
140    use crate::linting::{ExprLinter, Suggestion};
141    use crate::token_string_ext::TokenStringExt;
142    use crate::{Lint, Token};
143
144    pub struct TestSimpleLinter {
145        expr: Box<dyn Expr>,
146    }
147
148    impl Default for TestSimpleLinter {
149        fn default() -> Self {
150            Self {
151                expr: Box::new(FixedPhrase::from_phrase("two")),
152            }
153        }
154    }
155
156    impl ExprLinter for TestSimpleLinter {
157        type Unit = Chunk;
158
159        fn expr(&self) -> &dyn Expr {
160            &*self.expr
161        }
162
163        fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {
164            Some(Lint {
165                span: toks.span()?,
166                message: "simple".to_string(),
167                suggestions: vec![Suggestion::ReplaceWith(vec!['2'])],
168                ..Default::default()
169            })
170        }
171
172        fn description(&self) -> &str {
173            "test linter"
174        }
175    }
176
177    pub struct TestContextLinter {
178        expr: Box<dyn Expr>,
179    }
180
181    impl Default for TestContextLinter {
182        fn default() -> Self {
183            Self {
184                expr: Box::new(FixedPhrase::from_phrase("two")),
185            }
186        }
187    }
188
189    impl ExprLinter for TestContextLinter {
190        type Unit = Chunk;
191
192        fn expr(&self) -> &dyn Expr {
193            &*self.expr
194        }
195
196        fn match_to_lint_with_context(
197            &self,
198            toks: &[Token],
199            src: &[char],
200            context: Option<(&[Token], &[Token])>,
201        ) -> Option<Lint> {
202            if let Some((before, after)) = context {
203                let before = before.span()?.get_content_string(src);
204                let after = after.span()?.get_content_string(src);
205
206                let (message, suggestions) = if before.eq_ignore_ascii_case("one ")
207                    && after.eq_ignore_ascii_case(" three")
208                {
209                    (
210                        "ascending".to_string(),
211                        vec![Suggestion::ReplaceWith(vec!['>'])],
212                    )
213                } else if before.eq_ignore_ascii_case("three ")
214                    && after.eq_ignore_ascii_case(" one")
215                {
216                    (
217                        "descending".to_string(),
218                        vec![Suggestion::ReplaceWith(vec!['<'])],
219                    )
220                } else {
221                    (
222                        "dunno".to_string(),
223                        vec![Suggestion::ReplaceWith(vec!['?'])],
224                    )
225                };
226
227                return Some(Lint {
228                    span: toks.span()?,
229                    message,
230                    suggestions,
231                    ..Default::default()
232                });
233            } else {
234                None
235            }
236        }
237
238        fn description(&self) -> &str {
239            "context linter"
240        }
241    }
242
243    pub struct TestSentenceLinter {
244        expr: Box<dyn Expr>,
245    }
246
247    impl Default for TestSentenceLinter {
248        fn default() -> Self {
249            Self {
250                expr: Box::new(FixedPhrase::from_phrase("two, two")),
251            }
252        }
253    }
254
255    impl ExprLinter for TestSentenceLinter {
256        type Unit = Sentence;
257
258        fn expr(&self) -> &dyn Expr {
259            self.expr.as_ref()
260        }
261
262        fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {
263            Some(Lint {
264                span: toks.span()?,
265                message: "sentence".to_string(),
266                suggestions: vec![Suggestion::ReplaceWith(vec!['2', '&', '2'])],
267                ..Default::default()
268            })
269        }
270
271        fn description(&self) -> &str {
272            "sentence linter"
273        }
274    }
275
276    #[test]
277    fn simple_test_123() {
278        assert_suggestion_result("one two three", TestSimpleLinter::default(), "one 2 three");
279    }
280
281    #[test]
282    fn context_test_123() {
283        assert_suggestion_result("one two three", TestContextLinter::default(), "one > three");
284    }
285
286    #[test]
287    fn context_test_321() {
288        assert_suggestion_result("three two one", TestContextLinter::default(), "three < one");
289    }
290
291    #[test]
292    fn sentence_test_123() {
293        assert_suggestion_result(
294            "one, two, two, three",
295            TestSentenceLinter::default(),
296            "one, 2&2, three",
297        );
298    }
299}