typope 0.4.1

Pedantic source code checker for orthotypography mistakes and other typographical errors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use std::io::Write;
use std::path::{Path, PathBuf};

use miette::{SourceCode, SourceSpan};

pub mod punctuation;

use self::punctuation::Punctuation;

use crate::SharedSource;
use crate::lang::{Language, LintableString, Parsed};

/// Type that represents a rule that checks for typos
pub trait Rule {
    /// Returns the typos found by applying this rule to an array of bytes
    fn check(&self, bytes: &[u8]) -> Vec<Box<dyn Typo>>;
}

/// The kind of action to perform to fix the lint suggestion
pub enum Fix {
    /// Unclear how to fix the typo, nothing is done
    Unknown,

    /// Removes some characters
    Remove { span: SourceSpan },
}

pub struct TypoFixer {
    path: PathBuf,
    buffer: Vec<u8>,
    offset: isize,
}

impl TypoFixer {
    pub fn new(path: impl AsRef<Path>) -> anyhow::Result<Self> {
        let path = path.as_ref();
        let buffer = std::fs::read(path)?;

        Ok(Self {
            path: path.into(),
            buffer,
            offset: 0,
        })
    }

    pub fn fix(&mut self, typo: &dyn Typo) -> anyhow::Result<()> {
        self.offset += typo
            .fix()
            .apply_with_offset(&mut self.buffer, self.offset)?;

        Ok(())
    }
}

impl Drop for TypoFixer {
    fn drop(&mut self) {
        let write_changes = || -> anyhow::Result<()> {
            let mut file = if let Some(parent) = self.path.parent() {
                tempfile::NamedTempFile::new_in(parent)?
            } else {
                tempfile::NamedTempFile::new()?
            };
            file.write_all(&self.buffer)?;
            file.persist(&self.path)?;

            Ok(())
        };

        let _ = write_changes();
    }
}

impl Fix {
    /// Applies the action on the given buffer.
    ///
    /// Returns the offset that needs to be given to this function for
    /// the following calls.
    fn apply_with_offset(&self, buffer: &mut Vec<u8>, offset: isize) -> anyhow::Result<isize> {
        match self {
            Self::Unknown => Ok(0),
            Self::Remove { span } => {
                let typo_offset: isize = span.offset().try_into()?;
                let start: usize = (offset + typo_offset).try_into()?;
                let end = start + span.len();

                buffer.splice(start..end, []);

                Ok(-span.len().try_into()?)
            }
        }
    }
}

/// Type that represents a typo found
pub trait Typo: miette::Diagnostic + std::error::Error + Sync + Send {
    /// Span that identify where the typo is located
    fn span(&self) -> SourceSpan;

    /// Specify within which source the typo has been found
    fn with_source(&mut self, src: SharedSource, offset: usize);

    /// Returns the action to perform to fix the typo
    fn fix(&self) -> Fix {
        Fix::Unknown
    }
}

/// Detects typos in a file
pub struct Linter {
    parsed: Box<dyn Parsed>,
    source: SharedSource,
    rules: Vec<Box<dyn Rule>>,
    ignore_re: Vec<regex::Regex>,
}

impl Linter {
    /// Builds a linter that checks for typos in the file at the given path
    pub fn from_path(source: impl AsRef<Path>) -> anyhow::Result<Option<Self>> {
        let path = source.as_ref();
        let filename = path.file_name().unwrap_or_default();
        let Some(language) = Language::from_filename(filename) else {
            // TODO: parse the file as a text file without tree-sitter
            return Ok(None);
        };

        let source_content = std::fs::read(path)?;
        let linter = Self::new(language, source_content, path.to_string_lossy())?;

        Ok(Some(linter))
    }

    fn new(
        lang: &Language,
        source_content: impl Into<Vec<u8>>,
        source_name: impl AsRef<str>,
    ) -> anyhow::Result<Self> {
        let source_content = source_content.into();
        let source = SharedSource::new(source_name, source_content);
        let parsed = lang.parse(&source)?;

        let rules = vec![Box::new(Punctuation) as Box<dyn Rule>];

        Ok(Self {
            parsed,
            source,
            rules,
            ignore_re: Vec::new(),
        })
    }

    /// Extends the list of regexes that prevents some strings from being checked
    pub fn extend_ignore_re(&mut self, ignore_re: &[regex::Regex]) {
        self.ignore_re.extend_from_slice(ignore_re);
    }

    /// Returns an iterator over the typos found in the source
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use typope::lint::Linter;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let Some(mut linter) = Linter::from_path("file.rs")? else { return Ok(()); };
    /// for typo in linter.iter() {
    ///     let typo: miette::Report = typo.into();
    ///     eprintln!("{typo:?}");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn iter(&mut self) -> Iter<'_> {
        Iter::new(self)
    }

    /// Returns an iterator over the strings that can be linted in the source
    pub fn strings(&mut self) -> impl Iterator<Item = String> + '_ {
        self.parsed.strings(self.source.as_ref()).map(Into::into)
    }
}

/// Iterator over the typos found in a file
pub struct Iter<'t> {
    strings: Box<dyn Iterator<Item = LintableString> + 't>,
    source: SharedSource,
    typos: Vec<Box<dyn Typo>>,
    rules: &'t [Box<dyn Rule>],
    ignore_re: &'t [regex::Regex],
}

impl<'t> Iter<'t> {
    fn new(linter: &'t mut Linter) -> Self {
        Self {
            strings: linter.parsed.strings(linter.source.as_ref()),
            source: linter.source.clone(),
            typos: vec![],
            rules: &linter.rules,
            ignore_re: &linter.ignore_re,
        }
    }
}

impl Iterator for Iter<'_> {
    type Item = Box<dyn Typo>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(typo) = self.typos.pop() {
                return Some(typo);
            }

            let string = self.strings.next()?;

            let offset = string.offset();
            let ignored = self.ignore_re.iter().any(|re| re.is_match(string.as_str()));
            if ignored {
                continue;
            }

            let source = self.source.clone();
            let typos = self
                .rules
                .iter()
                .flat_map(move |rule| rule.check(string.as_str().as_bytes()))
                .map(move |mut typo| {
                    typo.with_source(source.clone(), offset);
                    typo
                });

            self.typos.extend(typos);
        }
    }
}

impl std::error::Error for Box<dyn Typo> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        (**self).source()
    }

    fn cause(&self) -> Option<&dyn std::error::Error> {
        self.source()
    }
}

impl miette::Diagnostic for Box<dyn Typo> {
    fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        (**self).code()
    }

    fn severity(&self) -> Option<miette::Severity> {
        (**self).severity()
    }

    fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        (**self).help()
    }

    fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        (**self).url()
    }

    fn source_code(&self) -> Option<&dyn SourceCode> {
        (**self).source_code()
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
        (**self).labels()
    }

    fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
        (**self).related()
    }

    fn diagnostic_source(&self) -> Option<&dyn miette::Diagnostic> {
        (**self).diagnostic_source()
    }
}

#[cfg(test)]
mod tests {
    use crate::lint::Language;

    use super::{Fix, Linter};

    #[test]
    fn from_path_unknown_extension() {
        assert!(
            Linter::from_path("file.with_unknown_extension")
                .unwrap()
                .is_none()
        );
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_string() {
        let rust = r#"
        /// Doc comment
        fn func() -> anyhow::Result<()> {
            anyhow::bail!("failed to do something for the following reason : foobar foo");
        }
        "#;
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

        let mut typos = linter.iter().collect::<Vec<_>>();
        assert_eq!(typos.len(), 1);
        let typo = typos.pop().unwrap();
        assert_eq!(
            format!("{}", typo.code().unwrap()),
            "typope::space-before-punctuation-mark"
        );
        assert_eq!(typo.span(), (141, 1).into());
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_into_report() {
        use miette::LabeledSpan;

        let rust = r#"
        /// Doc comment
        fn func() -> anyhow::Result<()> {
            anyhow::bail!("failed to do something for the following reason : foobar foo");
        }
        "#;
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

        let mut typos = linter.iter().collect::<Vec<_>>();
        assert_eq!(typos.len(), 1);
        let typo = typos.pop().unwrap();
        let report: miette::Report = typo.into();
        assert_eq!(
            format!("{}", report.code().unwrap()),
            "typope::space-before-punctuation-mark"
        );
        assert_eq!(
            report.labels().unwrap().collect::<Vec<_>>(),
            [LabeledSpan::new(Some("Invalid space here".into()), 141, 1)]
        );
        assert_eq!(
            report.help().unwrap().to_string(),
            "remove the space before `:`"
        );
        assert!(
            report
                .url()
                .unwrap()
                .to_string()
                .starts_with("https://docs.rs")
        );
        assert!(report.source_code().is_some());
        assert!(report.diagnostic_source().is_none());
        assert_eq!(report.severity(), None);
        assert!(report.related().is_none());
        assert!(report.source().is_none());
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_from_path() {
        let rust = r#"
        /// Doc comment
        fn func() -> anyhow::Result<()> {
            anyhow::bail!("failed to do something for the following reason : foobar foo");
        }
        "#;
        let file = tempfile::Builder::new().suffix(".rs").tempfile().unwrap();
        std::fs::write(file.path(), rust).unwrap();
        let mut linter = Linter::from_path(file.path()).unwrap().unwrap();

        let mut typos = linter.iter().collect::<Vec<_>>();
        assert_eq!(typos.len(), 1);
        let typo = typos.pop().unwrap();
        assert_eq!(
            format!("{}", typo.code().unwrap()),
            "typope::space-before-punctuation-mark"
        );
        assert_eq!(typo.span(), (141, 1).into());
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_string_ignored() {
        let rust = r#"
        /// Doc comment
        fn func() -> anyhow::Result<()> {
            anyhow::bail!("failed to do something for the following reason : foobar foo");
        }
        "#;
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();
        linter.extend_ignore_re(&[regex::Regex::new(r"foobar foo").unwrap()]);

        let typos = linter.iter().count();
        assert_eq!(typos, 0);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_rawstring() {
        let rust = r#"
        fn regex() -> &str {
            r"a ?regex.that ?match ?something ?"
        }
        "#;
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

        let typos = linter.iter().count();
        assert_eq!(typos, 0);
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn typo_rust_doctest() {
        let rust = r"
        /// Doc comment
        ///
        /// The example below should not trigger a warning since this is code:
        /// ```
        /// let english_words : u8 = 1;
        /// ```
        fn func() -> bool {
            true
        }
        ";
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

        let typos = linter.iter().count();
        assert_eq!(typos, 0);
    }

    #[cfg(feature = "lang-markdown")]
    #[test]
    fn typo_markdown_inline() {
        let markdown = r"# Hello
Hello mate `this should not trigger the rule : foobar` abc
        ";
        let mut linter = Linter::new(
            &Language::markdown(),
            markdown.as_bytes().to_vec(),
            "file.md",
        )
        .unwrap();

        let typos = linter.iter().count();
        assert_eq!(typos, 0);
    }

    #[test]
    fn apply_with_offset() {
        let mut content = b"123456".to_vec();

        let fix = Fix::Remove {
            span: (1, 2).into(),
        };
        fix.apply_with_offset(&mut content, 0).unwrap();
        assert_eq!("1456", String::from_utf8_lossy(&content));

        let fix = Fix::Remove {
            span: (1, 1).into(),
        };
        fix.apply_with_offset(&mut content, 2).unwrap();
        assert_eq!("145", String::from_utf8_lossy(&content));
    }

    #[cfg(feature = "lang-markdown")]
    #[test]
    fn typo_markdown_apply_multiple_fixes() {
        use crate::lint::TypoFixer;

        let markdown = r#"This should trigger the rule : foobar This one ! Annnnnd here ??? And !!! what about ! or this! and this ?? "another test with ! in it, but nothing else""#;
        let markdown_fixed = r#"This should trigger the rule: foobar This one! Annnnnd here??? And!!! what about! or this! and this?? "another test with! in it, but nothing else""#;
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("file.md");
        std::fs::write(&file_path, markdown.as_bytes()).unwrap();

        let mut linter = Linter::from_path(&file_path).unwrap().unwrap();

        let typos = linter.iter().collect::<Vec<_>>();
        assert_eq!(typos.len(), 7);

        let mut fixer = TypoFixer::new(&file_path).unwrap();
        for typo in typos.into_iter().rev() {
            fixer.fix(typo.as_ref()).unwrap();
        }

        drop(fixer);

        assert_eq!(markdown_fixed, std::fs::read_to_string(file_path).unwrap());
    }

    #[cfg(feature = "lang-rust")]
    #[test]
    fn strings() {
        let rust = r#"
        /// Docstring
        fn regex() -> &str {
            let _unused = String::from("abcd");
            "something"
        }
        "#;
        let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

        let strings = linter.strings().collect::<Vec<_>>();
        assert_eq!(strings, &["abcd", "something"]);
    }
}