Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/parser): Support Regular Expression v flag #8690

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions crates/swc_ecma_compat_common/src/regexp.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading