Skip to content

Commit

Permalink
feat(es/parser): Support Regular Expression v flag (#8690)
Browse files Browse the repository at this point in the history
**Related issue:**

- Closes #8462
  • Loading branch information
magic-akari committed Mar 6, 2024
1 parent 4066dfa commit 4ce39eb
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/swc_ecma_compat_common/src/regexp.rs
Expand Up @@ -23,6 +23,8 @@ pub struct Config {
pub unicode_property_regex: bool,
/// [RegExp.prototype.unicode](https://tc39.es/ecma262/multipage/text-processing.html#sec-get-regexp.prototype.unicode)
pub unicode_regex: bool,
// [RegExp.prototype.unicodeSets](https://github.com/tc39/proposal-regexp-v-flag)
pub unicode_sets_regex: bool,
}

struct RegExp {
Expand All @@ -39,6 +41,7 @@ impl VisitMut for RegExp {
if (self.config.dot_all_regex && regex.flags.contains('s'))
|| (self.config.sticky_regex && regex.flags.contains('y'))
|| (self.config.unicode_regex && regex.flags.contains('u'))
|| (self.config.unicode_sets_regex && regex.flags.contains('v'))
|| (self.config.has_indices && regex.flags.contains('d'))
|| (self.config.named_capturing_groups_regex && regex.exp.contains("(?<"))
|| (self.config.lookbehind_assertion && regex.exp.contains("(?<=")
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_compat_es2015/src/lib.rs
Expand Up @@ -68,6 +68,7 @@ where
sticky_regex: true,
unicode_property_regex: false,
unicode_regex: true,
unicode_sets_regex: false,
}),
block_scoped_functions(),
template_literal(c.template_literal),
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_compat_es2018/src/lib.rs
Expand Up @@ -22,6 +22,7 @@ pub fn es2018(c: Config) -> impl Fold {
sticky_regex: false,
unicode_property_regex: true,
unicode_regex: false,
unicode_sets_regex: false,
}),
object_rest_spread(c.object_rest_spread)
)
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_compat_es2022/src/lib.rs
Expand Up @@ -24,6 +24,7 @@ pub fn es2022<C: Comments>(cm: Option<C>, config: Config, unresolved_mark: Mark)
sticky_regex: false,
unicode_property_regex: true,
unicode_regex: false,
unicode_sets_regex: false,
}),
static_blocks(config.class_properties.static_blocks_mark),
class_properties(cm, config.class_properties, unresolved_mark),
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_parser/src/parser/expr.rs
Expand Up @@ -340,7 +340,8 @@ impl<I: Tokens> Parser<I> {
AHashMap::<char, usize>::default(),
|mut map, flag| {
let key = match flag {
'g' | 'i' | 'm' | 's' | 'u' | 'y' | 'd' => flag,
// https://tc39.es/ecma262/#sec-isvalidregularexpressionliteral
'd' | 'g' | 'i' | 'm' | 's' | 'u' | 'v' | 'y' => flag,
_ => '\u{0000}', // special marker for unknown flags
};
map.entry(key).and_modify(|count| *count += 1).or_insert(1);
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_preset_env/src/lib.rs
Expand Up @@ -99,6 +99,7 @@ where
let enable_sticky_regex = should_enable!(StickyRegex, false);
let enable_unicode_property_regex = should_enable!(UnicodePropertyRegex, false);
let enable_unicode_regex = should_enable!(UnicodeRegex, false);
let enable_unicode_sets_regex = should_enable!(UnicodeSetsRegex, false);

let enable = enable_dot_all_regex
|| enable_named_capturing_groups_regex
Expand All @@ -119,6 +120,7 @@ where
sticky_regex: enable_sticky_regex,
unicode_property_regex: enable_unicode_property_regex,
unicode_regex: enable_unicode_regex,
unicode_sets_regex: enable_unicode_sets_regex,
}),
enable
)
Expand Down

0 comments on commit 4ce39eb

Please sign in to comment.