Skip to content

Commit

Permalink
chore: follow the suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Jan 26, 2025
1 parent 3466e26 commit 2fa3bc6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 54 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ mime_guess = "2.0.5"
nonmax = "0.5.5"
num-bigint = "0.4.6"
num-traits = "0.2.19"
pathdiff = "0.2.3"
petgraph = "0.7.0"
phf = "0.11.2"
pico-args = "0.5.0"
Expand Down
1 change: 0 additions & 1 deletion apps/oxlint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ bpaf = { workspace = true, features = ["autocomplete", "bright-color", "derive"]
cow-utils = { workspace = true }
ignore = { workspace = true, features = ["simd-accel"] }
miette = { workspace = true }
pathdiff = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
Expand Down
90 changes: 48 additions & 42 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,51 +82,58 @@ impl Runner for LintRunner {
}

let mut oxlintrc = config_search_result.unwrap();
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();

let mut override_builder = OverrideBuilder::new(&self.cwd);
if !ignore_options.ignore_pattern.is_empty() {
for pattern in &ignore_options.ignore_pattern {
// Meaning of ignore pattern is reversed
// <https://docs.rs/ignore/latest/ignore/overrides/struct.OverrideBuilder.html#method.add>
let pattern = format!("!{pattern}");
override_builder.add(&pattern).unwrap();
let mut builder = None;

if !ignore_options.no_ignore {
let mut override_builder = OverrideBuilder::new(&self.cwd);

if !ignore_options.ignore_pattern.is_empty() {
for pattern in &ignore_options.ignore_pattern {
// Meaning of ignore pattern is reversed
// <https://docs.rs/ignore/latest/ignore/overrides/struct.OverrideBuilder.html#method.add>
let pattern = format!("!{pattern}");
override_builder.add(&pattern).unwrap();
}
}
}
if !oxlintrc.ignore_patterns.is_empty() {
oxlintrc.ignore_patterns = Self::adjust_ignore_patterns(
&self.cwd.to_string_lossy(),
&oxlint_wd.to_string_lossy(),
oxlintrc.ignore_patterns,
);
for pattern in &oxlintrc.ignore_patterns {
let pattern = format!("!{pattern}");
override_builder.add(&pattern).unwrap();
if !oxlintrc.ignore_patterns.is_empty() {
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();
oxlintrc.ignore_patterns = Self::adjust_ignore_patterns(
&self.cwd.to_string_lossy(),
&oxlint_wd.to_string_lossy(),
oxlintrc.ignore_patterns,
);
for pattern in &oxlintrc.ignore_patterns {
let pattern = format!("!{pattern}");
override_builder.add(&pattern).unwrap();
}
}
}
let override_builder = override_builder.build().unwrap();

// The ignore crate whitelists explicit paths, but priority
// should be given to the ignore file. Many users lint
// automatically and pass a list of changed files explicitly.
// To accommodate this, unless `--no-ignore` is passed,
// pre-filter the paths.
if !paths.is_empty() && !ignore_options.no_ignore {
let (ignore, _err) = Gitignore::new(&ignore_options.ignore_path);
let override_builder = override_builder.build().unwrap();

paths.retain_mut(|p| {
// Append cwd to all paths
let mut path = self.cwd.join(&p);
// The ignore crate whitelists explicit paths, but priority
// should be given to the ignore file. Many users lint
// automatically and pass a list of changed files explicitly.
// To accommodate this, unless `--no-ignore` is passed,
// pre-filter the paths.
if !paths.is_empty() {
let (ignore, _err) = Gitignore::new(&ignore_options.ignore_path);

std::mem::swap(p, &mut path);
paths.retain_mut(|p| {
// Append cwd to all paths
let mut path = self.cwd.join(&p);

if path.is_dir() {
true
} else {
!(override_builder.matched(p, false).is_ignore()
|| ignore.matched(path, false).is_ignore())
}
});
std::mem::swap(p, &mut path);

if path.is_dir() {
true
} else {
!(override_builder.matched(p, false).is_ignore()
|| ignore.matched(path, false).is_ignore())
}
});
}

builder = Some(override_builder);
}

if paths.is_empty() {
Expand All @@ -140,7 +147,7 @@ impl Runner for LintRunner {
paths.push(self.cwd.clone());
}

let walker = Walk::new(&paths, &ignore_options, override_builder);
let walker = Walk::new(&paths, &ignore_options, builder);
let paths = walker.with_extensions(Extensions(extensions)).paths();

let number_of_files = paths.len();
Expand Down Expand Up @@ -363,8 +370,7 @@ impl LintRunner {
if base == path {
ignore_patterns
} else {
let relative_ignore_path =
pathdiff::diff_paths(path, base).unwrap_or_else(|| PathBuf::from("."));
let relative_ignore_path = path.strip_prefix(base).unwrap_or(".");

ignore_patterns
.into_iter()
Expand Down
13 changes: 10 additions & 3 deletions apps/oxlint/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ impl ignore::ParallelVisitor for WalkCollector {
impl Walk {
/// Will not canonicalize paths.
/// # Panics
pub fn new(paths: &[PathBuf], options: &IgnoreOptions, override_builder: Override) -> Self {
pub fn new(
paths: &[PathBuf],
options: &IgnoreOptions,
override_builder: Option<Override>,
) -> Self {
assert!(!paths.is_empty(), "At least one path must be provided to Walk::new");

let mut inner = ignore::WalkBuilder::new(
Expand All @@ -85,7 +89,10 @@ impl Walk {

if !options.no_ignore {
inner.add_custom_ignore_filename(&options.ignore_path);
inner.overrides(override_builder);

if let Some(override_builder) = override_builder {
inner.overrides(override_builder);
}
}

// Turning off `follow_links` because:
Expand Down Expand Up @@ -146,7 +153,7 @@ mod test {

let override_builder = OverrideBuilder::new("/").build().unwrap();

let mut paths = Walk::new(&fixtures, &ignore_options, override_builder)
let mut paths = Walk::new(&fixtures, &ignore_options, Some(override_builder))
.with_extensions(Extensions(["js", "vue"].to_vec()))
.paths()
.into_iter()
Expand Down

0 comments on commit 2fa3bc6

Please sign in to comment.