Skip to content

Commit

Permalink
fix(core): fix handling of globs with {,.snap} (#20461)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Nov 28, 2023
1 parent 9e5c61f commit b9b75a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/nx/src/native/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub(crate) fn build_glob_set<S: AsRef<str> + Debug>(globs: &[S]) -> anyhow::Resu
.iter()
.flat_map(|s| potential_glob_split(s.as_ref()))
.map(|glob| {
if glob.contains('!') || glob.contains('|') || glob.contains('(') {
if glob.contains('!') || glob.contains('|') || glob.contains('(') || glob.contains("{,")
{
convert_glob(glob)
} else {
Ok(vec![glob.to_string()])
Expand Down Expand Up @@ -226,6 +227,13 @@ mod test {
assert!(!glob_set.is_match("dist/file.js"));
assert!(!glob_set.is_match("dist/cache/"));
assert!(!glob_set.is_match("dist/main/"));

let glob_set = build_glob_set(&["**/*.spec.ts{,.snap}"]).unwrap();
// matches
assert!(glob_set.is_match("src/file.spec.ts"));
assert!(glob_set.is_match("src/file.spec.ts.snap"));
// no matches
assert!(!glob_set.is_match("src/file.ts"));
}

#[test]
Expand Down
46 changes: 46 additions & 0 deletions packages/nx/src/native/glob/glob_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ fn one_or_more_group(input: &str) -> IResult<&str, GlobGroup, VerboseError<&str>
)(input)
}

fn brace_group_with_empty_item(input: &str) -> IResult<&str, GlobGroup, VerboseError<&str>> {
context(
"brace_group_with_empty_item",
map(preceded(tag("{,"), brace_group), GlobGroup::ZeroOrOne),
)(input)
}

fn exact_one_group(input: &str) -> IResult<&str, GlobGroup, VerboseError<&str>> {
context(
"exact_one_group",
Expand Down Expand Up @@ -71,6 +78,7 @@ fn non_special_character(input: &str) -> IResult<&str, GlobGroup, VerboseError<&
"non_special_character",
map(
alt((
take_until("{,"),
take_while(|c| c != '?' && c != '+' && c != '@' && c != '!' && c != '('),
is_not("*("),
)),
Expand All @@ -86,6 +94,13 @@ fn group(input: &str) -> IResult<&str, Cow<str>, VerboseError<&str>> {
)(input)
}

fn brace_group(input: &str) -> IResult<&str, Cow<str>, VerboseError<&str>> {
context(
"brace_group",
map_parser(terminated(take_until("}"), tag("}")), separated_group_items),
)(input)
}

fn separated_group_items(input: &str) -> IResult<&str, Cow<str>, VerboseError<&str>> {
map(
separated_list0(
Expand Down Expand Up @@ -117,6 +132,7 @@ fn parse_segment(input: &str) -> IResult<&str, Vec<GlobGroup>, VerboseError<&str
negated_file_group,
negated_wildcard,
negated_group,
brace_group_with_empty_item,
non_special_character,
)),
),
Expand Down Expand Up @@ -296,4 +312,34 @@ mod test {
)
);
}
#[test]
fn should_parse_globs_with_braces() {
let result = parse_glob("**/*.spec.ts{,.snap}").unwrap();

assert_eq!(
result,
(
false,
vec![
vec![GlobGroup::NonSpecial("**".into())],
vec![
GlobGroup::NonSpecial("*.spec.ts".into()),
GlobGroup::ZeroOrOne(".snap".into())
]
]
)
);
let result = parse_glob("**/*.spec.ts{.snapshot,.snap}").unwrap();

assert_eq!(
result,
(
false,
vec![
vec![GlobGroup::NonSpecial("**".into())],
vec![GlobGroup::NonSpecial("*.spec.ts{.snapshot,.snap}".into()),]
]
)
);
}
}

0 comments on commit b9b75a7

Please sign in to comment.