Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create_filter #1108

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/toolkit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ swc_ecma_utils = "=0.126.0"
lazy_static = "1.4.0"
sourcemap = "6.2.3"
anyhow = { version = "1.0.40", features = ["backtrace"] }
globset = "0.4.14"
95 changes: 95 additions & 0 deletions crates/toolkit/src/pluginutils/create_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
use std::error::Error;

pub fn create_filter<'a>(
include_patterns: Option<Vec<&'a str>>,
exclude_patterns: Option<Vec<&'a str>>,
) -> Result<impl Fn(&str) -> bool + 'a, Box<dyn Error>> {
let include_set = patterns_builder(include_patterns.as_deref())?;
let exclude_set = patterns_builder(exclude_patterns.as_deref())?;

Ok(move |path: &str| {
let match_include = include_set.is_match(path) || include_set.is_empty();
let match_exclude = exclude_set.is_match(path) && !exclude_set.is_empty();

match_include && !match_exclude
})
}

fn patterns_builder(patterns: Option<&[&str]>) -> Result<GlobSet, Box<dyn Error>> {
let mut builder = GlobSetBuilder::new();
if let Some(patterns) = patterns {
for &pattern in patterns {
builder.add(GlobBuilder::new(pattern).build()?);
}
}
builder.build().map_err(Into::into)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pluginutils::normalize_path::normalize_path;
use std::path::PathBuf;

#[test]
fn includes_by_default() -> Result<(), Box<dyn Error>> {
let filter = create_filter(None, None)?;
assert!(filter("index.html"));
Ok(())
}

#[test]
fn excludes_items_not_included_if_include_patterns_provided() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(vec!["*.y"]), None)?;
assert!(!filter("x"));
assert!(filter("a.y"));
Ok(())
}

#[test]
fn patterns_with_wildcards() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(vec!["*.y", "a.?"]), None)?;
assert!(filter("c.y"));
assert!(!filter("c.z"));
assert!(filter("a.x"));
assert!(!filter("b.x"));
Ok(())
}

#[test]
fn excludes_items_when_exclude_pattern_provided() -> Result<(), Box<dyn Error>> {
let filter = create_filter(None, Some(vec!["*.tmp"]))?;
assert!(filter("a.out"));
assert!(!filter("b.tmp"));
Ok(())
}

#[test]
fn properly_handles_inclusion_and_exclusion_patterns() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(vec!["*.js"]), Some(vec!["*.min.js"]))?;
assert!(filter("app.js"));
assert!(!filter("app.min.js"));
assert!(!filter("app.ts"));
Ok(())
}

#[test]
fn handles_relative_paths_correctly() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(vec!["src/*.js"]), Some(vec!["src/*.test.js"]))?;
assert!(filter(&PathBuf::from("src/main.js").to_string_lossy()));
assert!(!filter(
&PathBuf::from("src/main.test.js").to_string_lossy()
));
assert!(!filter(&PathBuf::from("lib/main.js").to_string_lossy()));
Ok(())
}

#[test]
fn handles_relative_paths() -> Result<(), Box<dyn Error>> {
let filter = create_filter(Some(vec!["./index.js"]), Some(vec!["'./foo/../a.js'"]))?;
assert!(!filter(&normalize_path("index.js")));
assert!(!filter(&normalize_path("a.js")));
assert!(!filter(&normalize_path("foo/a.js")));
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/toolkit/src/pluginutils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod add_extension;
pub mod create_filter;
pub mod normalize_path;
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
"pageerror",
"rsdoctor",
"deepmerge",
"Mergeable"
"Mergeable",
"globset"
],
"ignorePaths": [
"pnpm-lock.yaml",
Expand Down
Loading