Skip to content

Commit

Permalink
Refactor parcel config (#9766)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak authored Jun 4, 2024
1 parent e12a947 commit abd6c9e
Show file tree
Hide file tree
Showing 11 changed files with 845 additions and 528 deletions.
6 changes: 0 additions & 6 deletions crates/parcel_config/src/config_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ use thiserror::Error;
pub enum ConfigError {
#[error("{0}")]
InvalidConfig(String),
#[error("No {phase} found for {path} with pipeline {pipeline:?}")]
MissingPlugin {
path: PathBuf,
phase: String,
pipeline: Option<String>,
},
#[error("Unable to locate .parcelrc from {0}")]
MissingParcelRc(PathBuf),
#[error("Failed to parse {path}")]
Expand Down
5 changes: 2 additions & 3 deletions crates/parcel_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub mod config_error;
pub mod map;
pub mod parcel_config;
#[cfg(test)]
mod parcel_config_fixtures;
pub mod parcel_config_fixtures;
pub mod parcel_rc;
pub mod parcel_rc_config_loader;
mod partial_parcel_config;
pub mod pipeline;

pub use parcel_config::ParcelConfig;
pub use parcel_config::PluginNode;
11 changes: 11 additions & 0 deletions crates/parcel_config/src/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod matcher;
use matcher::*;

mod named_pipelines_map;
pub use named_pipelines_map::*;

mod pipeline_map;
pub use pipeline_map::*;

mod pipelines_map;
pub use pipelines_map::*;
111 changes: 111 additions & 0 deletions crates/parcel_config/src/map/matcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::path::Path;

use glob_match::glob_match;

pub fn named_pattern_matcher<'a>(path: &'a Path) -> impl Fn(&str, &str) -> bool + 'a {
let basename = path.file_name().unwrap().to_str().unwrap();
let path = path.as_os_str().to_str().unwrap();

|pattern, pipeline| {
let (named_pipeline, pattern) = pattern.split_once(':').unwrap_or(("", pattern));
pipeline == named_pipeline && (glob_match(pattern, basename) || glob_match(pattern, path))
}
}

pub fn pattern_matcher<'a>(path: &'a Path) -> impl Fn(&str) -> bool + 'a {
let is_match = named_pattern_matcher(path);

move |pattern| is_match(pattern, "")
}

#[cfg(test)]
mod tests {
use std::env;
use std::path::PathBuf;

use super::*;

fn paths(filename: &str) -> Vec<PathBuf> {
let cwd = env::current_dir().unwrap();
vec![
PathBuf::from(filename),
cwd.join(filename),
cwd.join("src").join(filename),
]
}

mod named_pattern_matcher {
use super::*;

#[test]
fn returns_false_when_path_does_not_match_pattern_with_empty_pipeline() {
for path in paths("a.ts") {
let is_match = named_pattern_matcher(&path);

assert_eq!(is_match("*.t", ""), false);
assert_eq!(is_match("*.tsx", ""), false);
assert_eq!(is_match("types:*.{ts,tsx}", ""), false);
assert_eq!(is_match("url:*", ""), false);
}
}

#[test]
fn returns_false_when_path_does_not_match_pipeline() {
for path in paths("a.ts") {
let is_match = named_pattern_matcher(&path);

assert_eq!(is_match("types:*.{ts,tsx}", "type"), false);
assert_eq!(is_match("types:*.{ts,tsx}", "typesa"), false);
}
}

#[test]
fn returns_true_when_path_matches_pattern_with_empty_pipeline() {
for path in paths("a.ts") {
let is_match = named_pattern_matcher(&path);

assert_eq!(is_match("*.{ts,tsx}", ""), true);
assert_eq!(is_match("*.ts", ""), true);
assert_eq!(is_match("*", ""), true);
}
}

#[test]
fn returns_true_when_path_matches_pattern_and_pipeline() {
for path in paths("a.ts") {
let is_match = named_pattern_matcher(&path);

assert_eq!(is_match("types:*.{ts,tsx}", "types"), true);
assert_eq!(is_match("types:*.ts", "types"), true);
assert_eq!(is_match("types:*", "types"), true);
}
}
}

mod pattern_matcher {
use super::*;

#[test]
fn returns_false_when_path_does_not_match_pattern() {
for path in paths("a.ts") {
let is_match = pattern_matcher(&path);

assert_eq!(is_match("*.t"), false);
assert_eq!(is_match("*.tsx"), false);
assert_eq!(is_match("types:*.{ts,tsx}"), false);
assert_eq!(is_match("url:*"), false);
}
}

#[test]
fn returns_true_when_path_matches_pattern_with_empty_pipeline() {
for path in paths("a.ts") {
let is_match = pattern_matcher(&path);

assert_eq!(is_match("*.{ts,tsx}"), true);
assert_eq!(is_match("*.ts"), true);
assert_eq!(is_match("*"), true);
}
}
}
}
Loading

0 comments on commit abd6c9e

Please sign in to comment.