Skip to content
Merged
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
71 changes: 47 additions & 24 deletions crates/oxc_regular_expression/src/ast_impl/support.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::ast::{CharacterClass, CharacterClassContents, LookAroundAssertionKind, Pattern, Term};
use crate::ast::{
Alternative, CharacterClass, CharacterClassContents, Disjunction, LookAroundAssertionKind,
Pattern, Term,
};

pub struct RegexUnsupportedPatterns {
pub named_capture_groups: bool,
Expand All @@ -13,34 +16,54 @@ pub fn has_unsupported_regular_expression_pattern(
pattern: &Pattern,
unsupported: &RegexUnsupportedPatterns,
) -> bool {
pattern.body.body.iter().any(|alternative| {
alternative.body.iter().any(|term| term_contains_unsupported(term, unsupported))
})
disjunction_contains_unsupported(&pattern.body, unsupported)
}

fn term_contains_unsupported(mut term: &Term, unsupported: &RegexUnsupportedPatterns) -> bool {
// Loop because `Term::Quantifier` contains a nested `Term`
loop {
match term {
Term::CapturingGroup(group) => {
return group.name.is_some() && unsupported.named_capture_groups;
}
Term::UnicodePropertyEscape(_) => return unsupported.unicode_property_escapes,
Term::CharacterClass(character_class) => {
return unsupported.unicode_property_escapes
&& character_class_has_unicode_property_escape(character_class);
fn disjunction_contains_unsupported(
disjunction: &Disjunction,
unsupported: &RegexUnsupportedPatterns,
) -> bool {
disjunction
.body
.iter()
.any(|alternative| alternative_contains_unsupported(alternative, unsupported))
}

fn alternative_contains_unsupported(
alternative: &Alternative,
unsupported: &RegexUnsupportedPatterns,
) -> bool {
alternative.body.iter().any(|term| term_contains_unsupported(term, unsupported))
}

fn term_contains_unsupported(term: &Term, unsupported: &RegexUnsupportedPatterns) -> bool {
match term {
Term::LookAroundAssertion(assertion) => {
if unsupported.look_behind_assertions
&& matches!(
assertion.kind,
LookAroundAssertionKind::Lookbehind
| LookAroundAssertionKind::NegativeLookbehind
)
{
return true;
}
Term::LookAroundAssertion(assertion) => {
return unsupported.look_behind_assertions
&& matches!(
assertion.kind,
LookAroundAssertionKind::Lookbehind
| LookAroundAssertionKind::NegativeLookbehind
);
disjunction_contains_unsupported(&assertion.body, unsupported)
}
Term::Quantifier(quantifier) => term_contains_unsupported(&quantifier.body, unsupported),
Term::UnicodePropertyEscape(_) => unsupported.unicode_property_escapes,
Term::CharacterClass(character_class) => {
unsupported.unicode_property_escapes
&& character_class_has_unicode_property_escape(character_class)
}
Term::CapturingGroup(group) => {
if group.name.is_some() && unsupported.named_capture_groups {
return true;
}
Term::Quantifier(quantifier) => term = &quantifier.body,
_ => return false,
disjunction_contains_unsupported(&group.body, unsupported)
}
Term::IgnoreGroup(group) => disjunction_contains_unsupported(&group.body, unsupported),
_ => false,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ a1 = /a.b/s
// RegExpLookbehindAssertions
b1 = /(?<!x)/
b2 = /(?<=x)/
b3 = /((?<!x)){2}/ // FIXME(sapphi-red): will be fixed in the next PR
b4 = /((?<=x)){3}/ // FIXME(sapphi-red): will be fixed in the next PR
b3 = /((?<!x)){2}/
b4 = /((?<=x)){3}/
// RegExpNamedCaptureGroups
c1 = /(?<a>b)/
c2 = /((?<c>d)){4}/; // FIXME(sapphi-red): will be fixed in the next PR
c2 = /((?<c>d)){4}/
// RegExpUnicodePropertyEscapes
d1 = /\p{Emoji}/u
// ES2022
Expand All @@ -22,3 +22,6 @@ f1 = /y/d
// ES2024
// RegExpSetNotation
g1 = /[\p{White_Space}&&\p{ASCII}]/v

// Nested cases
nested1 = /(?:(?<!x))/
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ x2 = new RegExp(".", "u");
a1 = new RegExp("a.b", "s");
b1 = new RegExp("(?<!x)", "");
b2 = new RegExp("(?<=x)", "");
b3 = /((?<!x)){2}/;
b4 = /((?<=x)){3}/;
b3 = new RegExp("((?<!x)){2}", "");
b4 = new RegExp("((?<=x)){3}", "");
c1 = new RegExp("(?<a>b)", "");
c2 = /((?<c>d)){4}/;
c2 = new RegExp("((?<c>d)){4}", "");
d1 = new RegExp("\\p{Emoji}", "u");
f1 = new RegExp("y", "d");
g1 = new RegExp("[\\p{White_Space}&&\\p{ASCII}]", "v");

nested1 = new RegExp("(?:(?<!x))", "");
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
c1 = /(?<a>b)/
c2 = /((?<a>b)){2}/ // FIXME(sapphi-red): will be fixed in the next PR
c2 = /((?<a>b)){2}/

n1 = /(a)/
nested1 = /(?<!(?<a>b))/
nested2 = /((?<a>b))/
nested3 = /(?:(?<a>b))/
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
c1 = new RegExp("(?<a>b)", "");
c2 = /((?<a>b)){2}/;
c2 = new RegExp("((?<a>b)){2}", "");

n1 = /(a)/;
nested1 = new RegExp("(?<!(?<a>b))", "");
nested2 = new RegExp("((?<a>b))", "");
nested3 = new RegExp("(?:(?<a>b))", "");
Loading