refactor: Encapsulate PerFileIgnore impl details

This commit is contained in:
Martin Fischer 2023-01-21 04:25:17 +01:00 committed by Charlie Marsh
parent 028436af81
commit 4cc492a17a
3 changed files with 14 additions and 13 deletions

View File

@ -1,8 +1,7 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use clap::{command, Parser};
use regex::Regex;
use ruff::fs;
use ruff::logging::LogLevel;
use ruff::registry::{Rule, RuleSelector};
use ruff::resolver::ConfigProcessor;
@ -444,9 +443,6 @@ pub fn collect_per_file_ignores(pairs: Vec<PatternPrefixPair>) -> Vec<PerFileIgn
}
per_file_ignores
.into_iter()
.map(|(pattern, prefixes)| {
let absolute = fs::normalize_path(Path::new(&pattern));
PerFileIgnore::new(pattern, absolute, &prefixes)
})
.map(|(pattern, prefixes)| PerFileIgnore::new(pattern, &prefixes, None))
.collect()
}

View File

@ -149,8 +149,7 @@ impl Configuration {
per_file_ignores
.into_iter()
.map(|(pattern, prefixes)| {
let absolute = fs::normalize_path_to(Path::new(&pattern), project_root);
PerFileIgnore::new(pattern, absolute, &prefixes)
PerFileIgnore::new(pattern, &prefixes, Some(project_root))
})
.collect()
}),

View File

@ -87,16 +87,22 @@ impl FromStr for FilePattern {
#[derive(Debug, Clone)]
pub struct PerFileIgnore {
pub basename: String,
pub absolute: PathBuf,
pub codes: HashableHashSet<Rule>,
pub(crate) basename: String,
pub(crate) absolute: PathBuf,
pub(crate) codes: HashableHashSet<Rule>,
}
impl PerFileIgnore {
pub fn new(basename: String, absolute: PathBuf, prefixes: &[RuleSelector]) -> Self {
pub fn new(pattern: String, prefixes: &[RuleSelector], project_root: Option<&Path>) -> Self {
let codes: FxHashSet<_> = prefixes.iter().flat_map(RuleSelector::codes).collect();
let path = Path::new(&pattern);
let absolute = match project_root {
Some(project_root) => fs::normalize_path_to(path, project_root),
None => fs::normalize_path(path),
};
Self {
basename,
basename: pattern,
absolute,
codes: codes.into(),
}