From 4ce39ebf32d79982a31458b7b70d9fecde40cd35 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Wed, 6 Mar 2024 12:40:25 +0800 Subject: [PATCH] feat(es/parser): Support Regular Expression `v` flag (#8690) **Related issue:** - Closes #8462 --- crates/swc_ecma_compat_common/src/regexp.rs | 3 +++ crates/swc_ecma_compat_es2015/src/lib.rs | 1 + crates/swc_ecma_compat_es2018/src/lib.rs | 1 + crates/swc_ecma_compat_es2022/src/lib.rs | 1 + crates/swc_ecma_parser/src/parser/expr.rs | 3 ++- crates/swc_ecma_preset_env/src/lib.rs | 2 ++ 6 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_compat_common/src/regexp.rs b/crates/swc_ecma_compat_common/src/regexp.rs index fc68ee1631ab..7c24107baf05 100644 --- a/crates/swc_ecma_compat_common/src/regexp.rs +++ b/crates/swc_ecma_compat_common/src/regexp.rs @@ -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 { @@ -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("(?<=") diff --git a/crates/swc_ecma_compat_es2015/src/lib.rs b/crates/swc_ecma_compat_es2015/src/lib.rs index 95f883c2fd5d..e536e0704b4a 100644 --- a/crates/swc_ecma_compat_es2015/src/lib.rs +++ b/crates/swc_ecma_compat_es2015/src/lib.rs @@ -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), diff --git a/crates/swc_ecma_compat_es2018/src/lib.rs b/crates/swc_ecma_compat_es2018/src/lib.rs index 9ec1d7c69485..041032b2102c 100644 --- a/crates/swc_ecma_compat_es2018/src/lib.rs +++ b/crates/swc_ecma_compat_es2018/src/lib.rs @@ -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) ) diff --git a/crates/swc_ecma_compat_es2022/src/lib.rs b/crates/swc_ecma_compat_es2022/src/lib.rs index 2611710a02e7..bfe0bf226af8 100644 --- a/crates/swc_ecma_compat_es2022/src/lib.rs +++ b/crates/swc_ecma_compat_es2022/src/lib.rs @@ -24,6 +24,7 @@ pub fn es2022(cm: Option, 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), diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 6ec36478ad47..cbbffbef53a0 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -340,7 +340,8 @@ impl Parser { AHashMap::::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); diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index e86def9631b6..cb19217b090d 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -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 @@ -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 )