Skip to content

Commit

Permalink
fix: require_literal_separator should be true
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Aug 13, 2024
1 parent 899a022 commit 54b3b31
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 53 deletions.
90 changes: 45 additions & 45 deletions crates/rspack_glob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl Iterator for Paths {
// Shouldn't happen, but we're using -1 as a special index.
assert!(self.dir_patterns.len() < !0);

fill_todo(&mut self.todo, &self.dir_patterns, 0, &scope, self.options);
fill_todo(&mut self.todo, &self.dir_patterns, 0, &scope, &self.options);
}
}

Expand Down Expand Up @@ -418,7 +418,7 @@ impl Iterator for Paths {
&self.dir_patterns,
next,
&path,
self.options,
&self.options,
);

if next == self.dir_patterns.len() - 1 {
Expand Down Expand Up @@ -450,7 +450,7 @@ impl Iterator for Paths {
Some(x) => x,
}
},
self.options,
&self.options,
) {
if idx == self.dir_patterns.len() - 1 {
// it is not possible for a pattern to match a directory
Expand All @@ -466,7 +466,7 @@ impl Iterator for Paths {
&self.dir_patterns,
idx + 1,
&path,
self.options,
&self.options,
);
}
}
Expand Down Expand Up @@ -776,7 +776,7 @@ impl Pattern {
/// assert!(Pattern::new("d*g").unwrap().matches("doog"));
/// ```
pub fn matches(&self, str: &str) -> bool {
self.matches_with(str, MatchOptions::new())
self.matches_with(str, &MatchOptions::new())
}

/// Return if the given `Path`, when converted to a `str`, matches this
Expand All @@ -788,13 +788,13 @@ impl Pattern {

/// Return if the given `str` matches this `Pattern` using the specified
/// match options.
pub fn matches_with(&self, str: &str, options: MatchOptions) -> bool {
pub fn matches_with(&self, str: &str, options: &MatchOptions) -> bool {
self.matches_from(true, str.chars(), 0, options) == Match
}

/// Return if the given `Path`, when converted to a `str`, matches this
/// `Pattern` using the specified match options.
pub fn matches_path_with(&self, path: &Path, options: MatchOptions) -> bool {
pub fn matches_path_with(&self, path: &Path, options: &MatchOptions) -> bool {
// FIXME (#9639): This needs to handle non-utf8 paths
path
.to_str()
Expand All @@ -811,7 +811,7 @@ impl Pattern {
mut follows_separator: bool,
mut file: std::str::Chars,
i: usize,
options: MatchOptions,
options: &MatchOptions,
) -> MatchResult {
for (ti, token) in self.tokens[i..].iter().enumerate() {
match token {
Expand Down Expand Up @@ -903,7 +903,7 @@ fn fill_todo(
patterns: &[Pattern],
idx: usize,
path: &PathWrapper,
options: MatchOptions,
options: &MatchOptions,
) {
// convert a pattern that's just many Char(_) to a string
fn pattern_as_str(pattern: &Pattern) -> Option<String> {
Expand Down Expand Up @@ -1019,7 +1019,7 @@ fn parse_char_specifiers(s: &[char]) -> Vec<CharSpecifier> {
cs
}

fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: MatchOptions) -> bool {
fn in_char_specifiers(specifiers: &[CharSpecifier], c: char, options: &MatchOptions) -> bool {
for &specifier in specifiers.iter() {
match specifier {
SingleChar(sc) => {
Expand Down Expand Up @@ -1284,7 +1284,7 @@ mod test {
case_sensitive: false,
..MatchOptions::new()
};
assert!(pat.matches_with(&c.to_string(), options));
assert!(pat.matches_with(&c.to_string(), &options));
}
assert!(pat.matches("1"));
assert!(pat.matches("2"));
Expand Down Expand Up @@ -1343,10 +1343,10 @@ mod test {
require_literal_leading_dot: false,
};

assert!(pat.matches_with("aBcDeFg", options));
assert!(pat.matches_with("abcdefg", options));
assert!(pat.matches_with("ABCDEFG", options));
assert!(pat.matches_with("AbCdEfG", options));
assert!(pat.matches_with("aBcDeFg", &options));
assert!(pat.matches_with("abcdefg", &options));
assert!(pat.matches_with("ABCDEFG", &options));
assert!(pat.matches_with("AbCdEfG", &options));
}

#[test]
Expand All @@ -1365,13 +1365,13 @@ mod test {
require_literal_leading_dot: false,
};

assert!(pat_within.matches_with("a", options_case_insensitive));
assert!(pat_within.matches_with("A", options_case_insensitive));
assert!(!pat_within.matches_with("A", options_case_sensitive));
assert!(pat_within.matches_with("a", &options_case_insensitive));
assert!(pat_within.matches_with("A", &options_case_insensitive));
assert!(!pat_within.matches_with("A", &options_case_sensitive));

assert!(!pat_except.matches_with("a", options_case_insensitive));
assert!(!pat_except.matches_with("A", options_case_insensitive));
assert!(pat_except.matches_with("A", options_case_sensitive));
assert!(!pat_except.matches_with("a", &options_case_insensitive));
assert!(!pat_except.matches_with("A", &options_case_insensitive));
assert!(pat_except.matches_with("A", &options_case_sensitive));
}

#[test]
Expand All @@ -1389,29 +1389,29 @@ mod test {

assert!(Pattern::new("abc/def")
.unwrap()
.matches_with("abc/def", options_require_literal));
.matches_with("abc/def", &options_require_literal));
assert!(!Pattern::new("abc?def")
.unwrap()
.matches_with("abc/def", options_require_literal));
.matches_with("abc/def", &options_require_literal));
assert!(!Pattern::new("abc*def")
.unwrap()
.matches_with("abc/def", options_require_literal));
.matches_with("abc/def", &options_require_literal));
assert!(!Pattern::new("abc[/]def")
.unwrap()
.matches_with("abc/def", options_require_literal));
.matches_with("abc/def", &options_require_literal));

assert!(Pattern::new("abc/def")
.unwrap()
.matches_with("abc/def", options_not_require_literal));
.matches_with("abc/def", &options_not_require_literal));
assert!(Pattern::new("abc?def")
.unwrap()
.matches_with("abc/def", options_not_require_literal));
.matches_with("abc/def", &options_not_require_literal));
assert!(Pattern::new("abc*def")
.unwrap()
.matches_with("abc/def", options_not_require_literal));
.matches_with("abc/def", &options_not_require_literal));
assert!(Pattern::new("abc[/]def")
.unwrap()
.matches_with("abc/def", options_not_require_literal));
.matches_with("abc/def", &options_not_require_literal));
}

#[test]
Expand All @@ -1432,60 +1432,60 @@ mod test {
.unwrap()
.matches_with(".hello.txt", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(!f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(!f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new(".*.*")
.unwrap()
.matches_with(".hello.txt", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new("aaa/bbb/*")
.unwrap()
.matches_with("aaa/bbb/.ccc", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(!f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(!f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new("aaa/bbb/*")
.unwrap()
.matches_with("aaa/bbb/c.c.c.", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new("aaa/bbb/.*")
.unwrap()
.matches_with("aaa/bbb/.ccc", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new("aaa/?bbb")
.unwrap()
.matches_with("aaa/.bbb", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(!f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(!f(&options_require_literal_leading_dot));

let f = |options| {
Pattern::new("aaa/[.]bbb")
.unwrap()
.matches_with("aaa/.bbb", options)
};
assert!(f(options_not_require_literal_leading_dot));
assert!(!f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(!f(&options_require_literal_leading_dot));

let f = |options| Pattern::new("**/*").unwrap().matches_with(".bbb", options);
assert!(f(options_not_require_literal_leading_dot));
assert!(!f(options_require_literal_leading_dot));
assert!(f(&options_not_require_literal_leading_dot));
assert!(!f(&options_require_literal_leading_dot));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rspack_core::{
NormalModuleFactoryModule, Plugin, ResolvedExportInfoTarget, SideEffectsBailoutItemWithSpan,
};
use rspack_error::Result;
use rspack_glob::MatchOptions;
use rspack_hook::{plugin, plugin_hook};
use sugar_path::SugarPath;
use swc_core::common::comments::Comments;
Expand Down Expand Up @@ -61,17 +62,22 @@ fn get_side_effects_from_package_json(
side_effects: SideEffects,
relative_path: PathBuf,
glob_pattern_cache: &DashMap<String, rspack_glob::Pattern>,
match_options: &MatchOptions,
) -> bool {
match side_effects {
SideEffects::Bool(s) => s,
SideEffects::String(s) => {
glob_match_with_normalized_pattern(&s, &relative_path.to_string_lossy(), glob_pattern_cache)
}
SideEffects::String(s) => glob_match_with_normalized_pattern(
&s,
&relative_path.to_string_lossy(),
glob_pattern_cache,
match_options,
),
SideEffects::Array(patterns) => patterns.iter().any(|pattern| {
glob_match_with_normalized_pattern(
pattern,
&relative_path.to_string_lossy(),
glob_pattern_cache,
match_options,
)
}),
}
Expand All @@ -81,6 +87,7 @@ fn glob_match_with_normalized_pattern(
pattern: &str,
string: &str,
glob_pattern_cache: &DashMap<String, rspack_glob::Pattern>,
match_options: &MatchOptions,
) -> bool {
let trim_start = pattern.trim_start_matches("./");
let normalized_glob = if trim_start.contains('/') {
Expand All @@ -91,14 +98,14 @@ fn glob_match_with_normalized_pattern(
match glob_pattern_cache.entry(normalized_glob) {
dashmap::mapref::entry::Entry::Occupied(entry) => {
let pattern = entry.get();
pattern.matches(string.trim_start_matches("./"))
pattern.matches_with(string.trim_start_matches("./"), match_options)
}
dashmap::mapref::entry::Entry::Vacant(entry) => {
let pattern = rspack_glob::Pattern::new(entry.key());
match pattern {
Ok(pat) => {
let pat = entry.insert(pat);
pat.matches(string.trim_start_matches("./"))
pat.matches_with(string.trim_start_matches("./"), match_options)
}
Err(_) => false,
}
Expand Down Expand Up @@ -648,6 +655,18 @@ impl ClassExt for ClassMember {
#[derive(Debug, Default)]
pub struct SideEffectsFlagPlugin {
glob_pattern_cache: DashMap<String, rspack_glob::Pattern>,
match_options: MatchOptions,
}

impl SideEffectsFlagPlugin {
pub fn new() -> Self {
let match_options = MatchOptions {
case_sensitive: false,
require_literal_separator: true,
require_literal_leading_dot: false,
};
Self::new_inner(DashMap::default(), match_options)
}
}

#[plugin_hook(NormalModuleFactoryModule for SideEffectsFlagPlugin)]
Expand Down Expand Up @@ -675,8 +694,12 @@ async fn nmf_module(
return Ok(());
};
let relative_path = resource_path.relative(package_path);
let has_side_effects =
get_side_effects_from_package_json(side_effects, relative_path, &self.glob_pattern_cache);
let has_side_effects = get_side_effects_from_package_json(
side_effects,
relative_path,
&self.glob_pattern_cache,
&self.match_options,
);
module.set_factory_meta(FactoryMeta {
side_effect_free: Some(!has_side_effects),
});
Expand Down Expand Up @@ -859,6 +882,12 @@ fn get_level_order_module_ids(mg: &ModuleGraph, entries: IdentifierSet) -> Vec<M
mod test_side_effects {
use super::*;

static MATCH_OPTIONS: MatchOptions = MatchOptions {
case_sensitive: false,
require_literal_separator: true,
require_literal_leading_dot: false,
};

fn get_side_effects_from_package_json_helper(
side_effects_config: Vec<&str>,
relative_path: &str,
Expand All @@ -877,7 +906,12 @@ mod test_side_effects {
SideEffects::String((&side_effects_config[0]).to_string())
};

get_side_effects_from_package_json(side_effects, relative_path, glob_pattern_cache)
get_side_effects_from_package_json(
side_effects,
relative_path,
glob_pattern_cache,
&MATCH_OPTIONS,
)
}

#[test]
Expand Down

0 comments on commit 54b3b31

Please sign in to comment.