Skip to content

Commit

Permalink
feat(es/parser): Support d in regex flag for es2022 (#5127)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwincoronado committed Jul 7, 2022
1 parent d1b99a2 commit 2d94797
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<I: Tokens> Parser<I> {
AHashMap::<char, usize>::default(),
|mut map, flag| {
let key = match flag {
'g' | 'i' | 'm' | 's' | 'u' | 'y' => flag,
'g' | 'i' | 'm' | 's' | 'u' | 'y' | 'd' => flag,
_ => '\u{0000}', // special marker for unknown flags
};
map.entry(key).and_modify(|count| *count += 1).or_insert(1);
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ where
Optional::new(
regexp(regexp::Config {
dot_all_regex: enable_dot_all_regex,

// TODO: add Feature:HasIndicesRegex
has_indices: false,
// TODO: add Feature::LookbehindAssertion
lookbehind_assertion: false,
named_capturing_groups_regex: enable_named_capturing_groups_regex,
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_compat/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ where
chain!(
regexp(regexp::Config {
dot_all_regex: false,
has_indices: false,
lookbehind_assertion: false,
named_capturing_groups_regex: false,
sticky_regex: true,
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_compat/src/es2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn es2018(c: Config) -> impl Fold {
chain!(
regexp(regexp::Config {
dot_all_regex: true,
has_indices: false,
lookbehind_assertion: true,
named_capturing_groups_regex: true,
sticky_regex: false,
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_ecma_transforms_compat/src/es2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use self::{
class_properties::class_properties, private_in_object::private_in_object,
static_blocks::static_blocks,
};
use crate::regexp::{self, regexp};

pub mod class_properties;
pub mod private_in_object;
Expand All @@ -13,6 +14,15 @@ pub mod static_blocks;
#[tracing::instrument(level = "info", skip_all)]
pub fn es2022<C: Comments>(cm: Option<C>, config: Config) -> impl Fold {
chain!(
regexp(regexp::Config {
dot_all_regex: true,
has_indices: true,
lookbehind_assertion: true,
named_capturing_groups_regex: true,
sticky_regex: false,
unicode_property_regex: true,
unicode_regex: false,
}),
static_blocks(),
class_properties(cm, config.class_properties),
private_in_object(),
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_transforms_compat/src/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub fn regexp(config: Config) -> impl Fold + VisitMut {
pub struct Config {
/// [s/dotAll flag for regular expressions](https://tc39.github.io/proposal-regexp-dotall-flag/)
pub dot_all_regex: bool,
/// [RegExp.prototype.hasIndices](https://262.ecma-international.org/13.0/#sec-get-regexp.prototype.hasIndices)
pub has_indices: bool,
/// [RegExp Lookbehind Assertions](https://tc39.es/proposal-regexp-lookbehind/)
pub lookbehind_assertion: bool,
/// [Named capture groups in regular expressions](https://tc39.es/proposal-regexp-named-groups/)
Expand All @@ -37,6 +39,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.has_indices && regex.flags.contains('d'))
|| (self.config.named_capturing_groups_regex && regex.exp.contains("(?<"))
|| (self.config.lookbehind_assertion && regex.exp.contains("(?<=")
|| regex.exp.contains("(?<!"))
Expand Down

1 comment on commit 2d94797

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2d94797 Previous: a0ec370 Ratio
es/full/minify/libraries/antd 1734328696 ns/iter (± 50767317) 1743412474 ns/iter (± 20877766) 0.99
es/full/minify/libraries/d3 442631737 ns/iter (± 6013908) 444119209 ns/iter (± 20625491) 1.00
es/full/minify/libraries/echarts 1726399510 ns/iter (± 48266517) 1791164186 ns/iter (± 77281094) 0.96
es/full/minify/libraries/jquery 117022853 ns/iter (± 3300719) 109863277 ns/iter (± 10843495) 1.07
es/full/minify/libraries/lodash 150657272 ns/iter (± 3638316) 124261010 ns/iter (± 5879677) 1.21
es/full/minify/libraries/moment 62720088 ns/iter (± 1248718) 55770773 ns/iter (± 4525611) 1.12
es/full/minify/libraries/react 19521889 ns/iter (± 824715) 17917533 ns/iter (± 453002) 1.09
es/full/minify/libraries/terser 634884118 ns/iter (± 7958812) 635694716 ns/iter (± 17117828) 1.00
es/full/minify/libraries/three 587582352 ns/iter (± 6765890) 562189911 ns/iter (± 6246185) 1.05
es/full/minify/libraries/typescript 3674839239 ns/iter (± 53036928) 3699082066 ns/iter (± 61119955) 0.99
es/full/minify/libraries/victory 768971754 ns/iter (± 12656775) 822663051 ns/iter (± 43728249) 0.93
es/full/minify/libraries/vue 167028037 ns/iter (± 7025087) 156870002 ns/iter (± 5594086) 1.06
es/full/codegen/es3 31602 ns/iter (± 1321) 33368 ns/iter (± 4317) 0.95
es/full/codegen/es5 31269 ns/iter (± 803) 40129 ns/iter (± 9321) 0.78
es/full/codegen/es2015 30896 ns/iter (± 798) 34375 ns/iter (± 8049) 0.90
es/full/codegen/es2016 31105 ns/iter (± 992) 33592 ns/iter (± 6056) 0.93
es/full/codegen/es2017 31020 ns/iter (± 1058) 32583 ns/iter (± 5205) 0.95
es/full/codegen/es2018 31171 ns/iter (± 1059) 32010 ns/iter (± 5190) 0.97
es/full/codegen/es2019 30874 ns/iter (± 1233) 31901 ns/iter (± 5578) 0.97
es/full/codegen/es2020 31210 ns/iter (± 1486) 31254 ns/iter (± 1658) 1.00
es/full/all/es3 213842862 ns/iter (± 8374225) 202913609 ns/iter (± 10491814) 1.05
es/full/all/es5 195014665 ns/iter (± 7822715) 188687837 ns/iter (± 8648054) 1.03
es/full/all/es2015 163685281 ns/iter (± 8821729) 149494872 ns/iter (± 7751158) 1.09
es/full/all/es2016 152771712 ns/iter (± 8377685) 150926694 ns/iter (± 11192667) 1.01
es/full/all/es2017 154162363 ns/iter (± 6760453) 147700708 ns/iter (± 5077286) 1.04
es/full/all/es2018 151006180 ns/iter (± 6202723) 145777618 ns/iter (± 4530082) 1.04
es/full/all/es2019 150741943 ns/iter (± 8686024) 145487215 ns/iter (± 3237190) 1.04
es/full/all/es2020 145948865 ns/iter (± 7805512) 139878370 ns/iter (± 6136155) 1.04
es/full/parser 711217 ns/iter (± 21180) 738395 ns/iter (± 38236) 0.96
es/full/base/fixer 29458 ns/iter (± 1719) 30083 ns/iter (± 1698) 0.98
es/full/base/resolver_and_hygiene 88219 ns/iter (± 1695) 89866 ns/iter (± 4828) 0.98
serialization of ast node 217 ns/iter (± 3) 228 ns/iter (± 14) 0.95
serialization of serde 226 ns/iter (± 2) 237 ns/iter (± 18) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.