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
507
508
509
510
511
512
513
514
515
//! Config parsers to recognize the config fields of [`typos`](https://crates.io/crates/typos-cli).
// It is based on <https://github.com/crate-ci/typos/blob/master/crates/typos-cli/src/config.rs>
// but it has been modified to remove fields that we do not care about for the moment.
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::Context;

use ignore::WalkBuilder;

use crate::lang::Language;

/// List of file names that can contain the configuration
pub const SUPPORTED_FILE_NAMES: &[&str] = &[
    "typos.toml",
    "_typos.toml",
    ".typos.toml",
    CARGO_TOML,
    PYPROJECT_TOML,
];
const CARGO_TOML: &str = "Cargo.toml";
const PYPROJECT_TOML: &str = "pyproject.toml";

/// Defines the configuration of the linter.
///
/// It is compatible with a subset of the configuration of [`typos`](https://crates.io/crates/typos-cli).
///
/// # Example
///
/// ```toml
/// [files]
/// ignore-hidden = false
///
/// [default]
/// extend-ignore-re = ["some regex.*rrrregex"]
///
/// [type.cpp]
/// check-file = false
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
    pub files: Walk,
    pub default: EngineConfig,
    #[serde(rename = "type")]
    pub type_: TypeEngineConfig,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
struct CargoTomlConfig {
    pub workspace: Option<CargoTomlPackage>,
    pub package: Option<CargoTomlPackage>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
struct CargoTomlPackage {
    pub metadata: CargoTomlMetadata,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
struct CargoTomlMetadata {
    pub typope: Option<Config>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
struct PyprojectTomlConfig {
    tool: PyprojectTomlTool,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
struct PyprojectTomlTool {
    typos: Option<Config>,
}

impl Config {
    /// Tries to load a config from a directory.
    ///
    /// It looks for the file names listed in [`SUPPORTED_FILE_NAMES`].
    pub fn from_dir(cwd: &Path) -> anyhow::Result<Option<Self>> {
        for file in find_project_files(cwd, SUPPORTED_FILE_NAMES) {
            if let Some(config) = Self::from_file(&file)? {
                return Ok(Some(config));
            }
        }

        Ok(None)
    }

    /// Loads a config from a file
    pub fn from_file(path: &Path) -> anyhow::Result<Option<Self>> {
        let s = std::fs::read_to_string(path)
            .with_context(|| format!("could not read config at `{}`", path.display()))?;

        match path.file_name() {
            Some(name) if name == CARGO_TOML => {
                let config = toml::from_str::<CargoTomlConfig>(&s)
                    .with_context(|| format!("could not parse config at `{}`", path.display()))?;
                let typos = config
                    .workspace
                    .and_then(|w| w.metadata.typope)
                    .or_else(|| config.package.and_then(|p| p.metadata.typope));

                if let Some(typos) = typos {
                    Ok(Some(typos))
                } else {
                    Ok(None)
                }
            }
            Some(name) if name == PYPROJECT_TOML => {
                let config = toml::from_str::<PyprojectTomlConfig>(&s)
                    .with_context(|| format!("could not parse config at `{}`", path.display()))?;

                if let Some(typos) = config.tool.typos {
                    Ok(Some(typos))
                } else {
                    Ok(None)
                }
            }
            _ => Self::from_toml(&s)
                .map(Some)
                .with_context(|| format!("could not parse config at `{}`", path.display())),
        }
    }

    /// Loads a config from TOML
    pub fn from_toml(data: &str) -> anyhow::Result<Self> {
        toml::from_str(data).map_err(Into::into)
    }

    /// Updates the config based on the value of another config
    pub fn update(&mut self, source: &Self) {
        self.files.update(&source.files);
        self.default.update(&source.default);
        self.type_.update(&source.type_);
    }

    /// Builds a [`WalkBuilder`] to find files based on the config
    pub fn to_walk_builder(&self, path: &Path) -> WalkBuilder {
        let mut walk = ignore::WalkBuilder::new(path);
        walk.skip_stdout(true)
            .git_global(self.files.ignore_global())
            .git_ignore(self.files.ignore_vcs())
            .git_exclude(self.files.ignore_vcs())
            .hidden(self.files.ignore_hidden())
            .parents(self.files.ignore_parent())
            .ignore(self.files.ignore_dot());

        walk
    }

    pub fn config_from_path(&self, path: impl AsRef<Path>) -> Cow<'_, EngineConfig> {
        let path = path.as_ref();
        let Some(extension) = path.extension() else {
            return Cow::Borrowed(&self.default);
        };
        let Some(lang) = Language::from_filename(extension) else {
            return Cow::Borrowed(&self.default);
        };

        let mut config = self.default.clone();
        if let Some(type_config) = self.type_.patterns.get(lang.name()) {
            config.update(type_config);
        }

        Cow::Owned(config)
    }
}

/// Defines how to ignore files from being checked by the linter
///
/// # Example
///
/// ```toml
/// [files]
/// ignore-hidden = false
/// ```
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct Walk {
    /// Additional list of regexes to exclude files from being checked
    pub extend_exclude: Vec<String>,

    /// Skip hidden files and directories.
    pub ignore_hidden: Option<bool>,

    /// Respect ignore files.
    pub ignore_files: Option<bool>,

    /// Respect .ignore files.
    pub ignore_dot: Option<bool>,

    /// Respect ignore files in vcs directories.
    pub ignore_vcs: Option<bool>,

    /// Respect global ignore files.
    pub ignore_global: Option<bool>,

    /// Respect ignore files in parent directories.
    pub ignore_parent: Option<bool>,
}

impl Default for Walk {
    fn default() -> Self {
        Self {
            extend_exclude: Default::default(),
            ignore_hidden: Some(true),
            ignore_files: Some(true),
            ignore_dot: Some(true),
            ignore_vcs: Some(true),
            ignore_global: Some(true),
            ignore_parent: Some(true),
        }
    }
}

impl Walk {
    /// Updates the config based on the value of another config
    pub fn update(&mut self, source: &Self) {
        self.extend_exclude
            .extend(source.extend_exclude.iter().cloned());
        if let Some(source) = source.ignore_hidden {
            self.ignore_hidden = Some(source);
        }
        if let Some(source) = source.ignore_files {
            self.ignore_files = Some(source);
            self.ignore_dot = None;
            self.ignore_vcs = None;
            self.ignore_global = None;
            self.ignore_parent = None;
        }
        if let Some(source) = source.ignore_dot {
            self.ignore_dot = Some(source);
        }
        if let Some(source) = source.ignore_vcs {
            self.ignore_vcs = Some(source);
            self.ignore_global = None;
        }
        if let Some(source) = source.ignore_global {
            self.ignore_global = Some(source);
        }
        if let Some(source) = source.ignore_parent {
            self.ignore_parent = Some(source);
        }
    }

    /// Whether to skip hidden files and directories
    pub fn ignore_hidden(&self) -> bool {
        self.ignore_hidden.unwrap_or(true)
    }

    /// Whether to respect .ignore files
    pub fn ignore_dot(&self) -> bool {
        self.ignore_dot.or(self.ignore_files).unwrap_or(true)
    }

    /// Whether to respect ignore files in vcs directories
    pub fn ignore_vcs(&self) -> bool {
        self.ignore_vcs.or(self.ignore_files).unwrap_or(true)
    }

    /// Whether to respect global ignore files
    pub fn ignore_global(&self) -> bool {
        self.ignore_global
            .or(self.ignore_vcs)
            .or(self.ignore_files)
            .unwrap_or(true)
    }

    /// Whether to respect ignore files in parent directories
    pub fn ignore_parent(&self) -> bool {
        self.ignore_parent.or(self.ignore_files).unwrap_or(true)
    }
}

/// File type specific settings.
///
/// It helps a user define settings that only apply to some file types.
///
/// # Example
///
/// ```toml
/// [type.rust]
/// check-file = false
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(transparent)]
pub struct TypeEngineConfig {
    /// Maps a file type to a custom config
    pub patterns: HashMap<String, EngineConfig>,
}

impl TypeEngineConfig {
    /// Updates the config based on the value of another config
    pub fn update(&mut self, source: &Self) {
        for (type_name, engine) in &source.patterns {
            self.patterns
                .entry(type_name.to_owned())
                .or_default()
                .update(engine);
        }
    }
}

/// Configuration for the linter's engine that can be applied globally or on a type of file
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct EngineConfig {
    /// Whether to check files
    pub check_file: Option<bool>,

    /// Additional list of regexes to prevent strings from being checked
    #[serde(with = "serde_regex")]
    pub extend_ignore_re: Vec<regex::Regex>,
}

impl PartialEq for EngineConfig {
    fn eq(&self, other: &Self) -> bool {
        self.check_file == other.check_file
            && self
                .extend_ignore_re
                .iter()
                .map(|r| r.as_str())
                .eq(other.extend_ignore_re.iter().map(|r| r.as_str()))
    }
}

impl Eq for EngineConfig {}

impl Default for EngineConfig {
    fn default() -> Self {
        Self {
            check_file: Some(true),
            extend_ignore_re: Default::default(),
        }
    }
}

impl EngineConfig {
    /// Updates the config based on the value of another config
    pub fn update(&mut self, source: &Self) {
        if let Some(source) = source.check_file {
            self.check_file = Some(source);
        }
        self.extend_ignore_re
            .extend_from_slice(&source.extend_ignore_re);
    }

    /// Whether to check this file type
    pub fn check_file(&self) -> bool {
        self.check_file.unwrap_or(true)
    }
}

fn find_project_files<'a>(
    dir: &'a Path,
    names: &'a [&'a str],
) -> impl Iterator<Item = PathBuf> + 'a {
    names
        .iter()
        .map(|name| dir.join(name))
        .filter(|path| path.exists())
}

#[cfg(test)]
mod test {
    use std::path::Path;

    use regex::Regex;

    use tempfile::{NamedTempFile, tempdir};

    use super::{Config, EngineConfig};

    #[test]
    fn from_file() {
        let config = r#"
[files]
ignore-hidden = false

[default]
extend-ignore-re = ["some regex.*rrrregex"]

[type.cpp]
check-file = false
        "#;
        let file = NamedTempFile::new().unwrap();
        std::fs::write(file.path(), config).unwrap();
        let config = Config::from_file(file.path()).unwrap().unwrap();
        assert!(!config.files.ignore_hidden());
        assert_eq!(
            config
                .default
                .extend_ignore_re
                .iter()
                .map(Regex::as_str)
                .collect::<Vec<_>>(),
            [Regex::new("some regex.*rrrregex").unwrap().as_str()]
        );
    }

    #[test]
    fn from_file_invalid() {
        let file = NamedTempFile::new().unwrap();
        std::fs::write(file.path(), "invaliddddddd").unwrap();
        Config::from_file(file.path()).unwrap_err();
        Config::from_file(Path::new("file that does not exist.toml")).unwrap_err();
    }

    #[test]
    fn from_dir() {
        let config = r#"
[files]
ignore-hidden = false

[default]
extend-ignore-re = ["some regex.*rrrregex"]

[type.cpp]
check-file = false
        "#;
        let dir: tempfile::TempDir = tempdir().unwrap();
        assert!(Config::from_dir(dir.path()).unwrap().is_none());

        let typos_config_file = dir.path().join(".typos.toml");
        std::fs::write(&typos_config_file, config).unwrap();
        let config = Config::from_dir(dir.path()).unwrap().unwrap();
        assert!(!config.files.ignore_hidden());
        assert_eq!(
            config
                .default
                .extend_ignore_re
                .iter()
                .map(Regex::as_str)
                .collect::<Vec<_>>(),
            [Regex::new("some regex.*rrrregex").unwrap().as_str()]
        );
    }

    #[test]
    fn from_cargo_toml() {
        let config = r#"
[package]
name = "abc"
edition = "2021"
publish = false

[package.metadata.typope.files]
ignore-hidden = false

[package.metadata.typope.default]
extend-ignore-re = ["some regex.*rrrregex"]

[package.metadata.typope.type.cpp]
check-file = false
        "#;

        let dir: tempfile::TempDir = tempdir().unwrap();

        let cargo_toml = dir.path().join("Cargo.toml");
        std::fs::write(&cargo_toml, config).unwrap();
        let config = Config::from_file(&cargo_toml).unwrap().unwrap();
        assert!(!config.files.ignore_hidden());
        assert_eq!(
            config
                .default
                .extend_ignore_re
                .iter()
                .map(Regex::as_str)
                .collect::<Vec<_>>(),
            [Regex::new("some regex.*rrrregex").unwrap().as_str()]
        );
    }

    #[test]
    fn test_update_from_nothing() {
        let defaulted = Config::default();

        let mut actual = defaulted.clone();
        actual.update(&Config::default());

        assert_eq!(actual, defaulted);
    }

    #[test]
    fn parse_extend_globs() {
        let input = r"[type.po]
check-file = true
";
        let mut expected = Config::default();
        expected.type_.patterns.insert(
            "po".into(),
            EngineConfig {
                check_file: Some(true),
                ..Default::default()
            },
        );
        let actual = Config::from_toml(input).unwrap();
        assert_eq!(actual, expected);
    }
}