mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(transformer): clarify match in RegExp transform (#5498)
Refactor match on `Term` in RegExp transform.
This is personal taste, really. I prefer this style:
```rs
match enum {
Enum::Variant(x) => x.is_something(),
_ => false,
}
```
over:
```rs
match enum {
Enum::Variant(x) if x.is_something() => true,
_ => false,
}
```
because then you don't have to reason about what happens when the `if` is false and it "falls through" to the next match arm. The compiler *might* also find the former easier to optimize, though likely in most cases it can figure out that they're equivalent.
Also rename a couple of vars for clarity.
This commit is contained in:
parent
09c522aa5b
commit
6abde0a69f
1 changed files with 13 additions and 15 deletions
|
|
@ -45,7 +45,6 @@ mod options;
|
|||
use std::borrow::Cow;
|
||||
|
||||
pub use options::RegExpOptions;
|
||||
use oxc_allocator::Vec;
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_diagnostics::Result;
|
||||
use oxc_regular_expression::ast::{
|
||||
|
|
@ -198,36 +197,35 @@ impl<'a> RegExp<'a> {
|
|||
///
|
||||
/// Based on parsed regular expression pattern.
|
||||
fn has_unsupported_regular_expression_pattern(&self, pattern: &Pattern<'a>) -> bool {
|
||||
let check_terms = |terms: &Vec<'a, Term>| {
|
||||
terms.iter().any(|element| match element {
|
||||
Term::CapturingGroup(_) if self.named_capture_groups => true,
|
||||
Term::UnicodePropertyEscape(_) if self.unicode_property_escapes => true,
|
||||
Term::CharacterClass(character_class) if self.unicode_property_escapes => {
|
||||
has_unicode_property_escape_character_class(character_class)
|
||||
let terms_contains_unsupported = |terms: &[Term]| {
|
||||
terms.iter().any(|term| match term {
|
||||
Term::CapturingGroup(_) => self.named_capture_groups,
|
||||
Term::UnicodePropertyEscape(_) => self.unicode_property_escapes,
|
||||
Term::CharacterClass(character_class) => {
|
||||
self.unicode_property_escapes
|
||||
&& character_class_has_unicode_property_escape(character_class)
|
||||
}
|
||||
Term::LookAroundAssertion(assertion)
|
||||
if self.look_behind_assertions
|
||||
Term::LookAroundAssertion(assertion) => {
|
||||
self.look_behind_assertions
|
||||
&& matches!(
|
||||
assertion.kind,
|
||||
LookAroundAssertionKind::Lookbehind
|
||||
| LookAroundAssertionKind::NegativeLookbehind
|
||||
) =>
|
||||
{
|
||||
true
|
||||
)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
};
|
||||
|
||||
pattern.body.body.iter().any(|alternative| check_terms(&alternative.body))
|
||||
pattern.body.body.iter().any(|alternative| terms_contains_unsupported(&alternative.body))
|
||||
}
|
||||
}
|
||||
|
||||
fn has_unicode_property_escape_character_class(character_class: &CharacterClass) -> bool {
|
||||
fn character_class_has_unicode_property_escape(character_class: &CharacterClass) -> bool {
|
||||
character_class.body.iter().any(|element| match element {
|
||||
CharacterClassContents::UnicodePropertyEscape(_) => true,
|
||||
CharacterClassContents::NestedCharacterClass(character_class) => {
|
||||
has_unicode_property_escape_character_class(character_class)
|
||||
character_class_has_unicode_property_escape(character_class)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue