mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 18:41:05 -05:00
Merge pull request #729 from scop/feat/trim-in-extension
feat(cli): Strip `.in` suffix(es) before attempting filename match
This commit is contained in:
commit
f69eec1ce3
1 changed files with 26 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
|
||||
use kstring::KString;
|
||||
|
||||
|
@ -123,9 +124,24 @@ impl Types {
|
|||
}
|
||||
|
||||
pub fn file_matched(&self, path: &std::path::Path) -> Option<&str> {
|
||||
let file_name = path.file_name()?;
|
||||
let mut mpath = Path::new(path);
|
||||
let mut matches = self.matches.get_or_default().borrow_mut();
|
||||
self.set.matches_into(file_name, &mut *matches);
|
||||
loop {
|
||||
self.set.matches_into(mpath.file_name()?, &mut *matches);
|
||||
if !matches.is_empty() {
|
||||
break;
|
||||
}
|
||||
match mpath.extension() {
|
||||
None => break,
|
||||
Some(ext) => {
|
||||
if ext == "in" {
|
||||
mpath = Path::new(mpath.file_stem()?);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
matches
|
||||
.last()
|
||||
.copied()
|
||||
|
@ -161,14 +177,22 @@ mod tests {
|
|||
("js", &["*.js"]),
|
||||
("json", &["*.json"]),
|
||||
("lock", &["package-lock.json", "*.lock"]),
|
||||
("js-in", &["*.js.in"]),
|
||||
]
|
||||
}
|
||||
fn in_types() -> &'static [(&'static str, &'static [&'static str])] {
|
||||
&[("html", &["*.html", "*.htm"]), ("in-canary", &["*.in"])]
|
||||
}
|
||||
|
||||
matched!(basic_match, types(), "leftpad.js", "js");
|
||||
matched!(multi_def_1, types(), "index.html", "html");
|
||||
matched!(multi_def_2, types(), "index.htm", "html");
|
||||
matched!(no_match, types(), "leftpad.ada", None);
|
||||
matched!(more_specific, types(), "package-lock.json", "lock");
|
||||
matched!(basic_in, types(), "index.html.in", "html");
|
||||
matched!(basic_in_in, types(), "index.html.in.in", "html");
|
||||
matched!(ext_plus_in, types(), "foo.js.in", "js-in");
|
||||
matched!(toplevel_in, in_types(), "index.html.in", "in-canary");
|
||||
|
||||
macro_rules! sort {
|
||||
($name:ident, $actual:expr, $expected:expr) => {
|
||||
|
|
Loading…
Reference in a new issue