Skip to content

Commit

Permalink
refactor(es/transforms/bugfix): Migrate edge_default_param to `Visi…
Browse files Browse the repository at this point in the history
…tMut` (#2676)
  • Loading branch information
kwonoj authored Nov 8, 2021
1 parent 6b478cc commit 8915913
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions ecmascript/transforms/compat/src/bugfixes/edge_default_param.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,60 @@
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

/// Converts destructured parameters with default values to non-shorthand
/// syntax. This fixes the only arguments-related bug in ES Modules-supporting
/// browsers (Edge 16 & 17). Use this plugin instead of
/// @babel/plugin-transform-parameters when targeting ES Modules.
pub fn edge_default_param() -> impl Fold {
EdgeDefaultParam::default()
pub fn edge_default_param() -> impl Fold + VisitMut {
as_folder(EdgeDefaultParam::default())
}
#[derive(Default, Clone, Copy)]
struct EdgeDefaultParam {
in_arrow: bool,
}

/// TODO: VisitMut
impl Fold for EdgeDefaultParam {
noop_fold_type!();
impl VisitMut for EdgeDefaultParam {
noop_visit_mut_type!();

fn fold_arrow_expr(&mut self, n: ArrowExpr) -> ArrowExpr {
fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
self.in_arrow = true;
let params = n.params.fold_with(self);
n.params.visit_mut_children_with(self);
self.in_arrow = false;

let body = n.body.fold_with(self);
ArrowExpr { params, body, ..n }
n.body.visit_mut_children_with(self);
}

fn fold_object_pat(&mut self, n: ObjectPat) -> ObjectPat {
let n = n.fold_children_with(self);
fn visit_mut_object_pat(&mut self, n: &mut ObjectPat) {
n.visit_mut_children_with(self);
if !self.in_arrow {
return n;
return;
}

let props = n
.props
.into_iter()
.map(|prop| match prop {
ObjectPatProp::Assign(assign_pat) => {
if let Some(value) = assign_pat.value {
ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(assign_pat.key.clone()),
for idx in 0..n.props.len() {
let prop = &(n.props[idx]);

match prop {
ObjectPatProp::Assign(AssignPatProp {
value, key, span, ..
}) => match &value {
Some(value) => {
let prop = ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(key.clone()),
value: Box::new(Pat::Assign(AssignPat {
span: assign_pat.span,
left: Box::new(Pat::Ident(BindingIdent::from(
assign_pat.key.clone(),
))),
span: *span,
left: Box::new(Pat::Ident(BindingIdent::from(key.clone()))),
right: value.clone(),
type_ann: None,
})),
})
} else {
ObjectPatProp::Assign(assign_pat)
});

n.props[idx] = prop;
}
}
_ => prop,
})
.collect();
ObjectPat { props, ..n }
_ => {}
},
_ => {}
}
}
}
}

Expand Down

1 comment on commit 8915913

@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: 8915913 Previous: 6b478cc Ratio
base_tr_fixer 27509 ns/iter (± 1917) 32635 ns/iter (± 4058) 0.84
base_tr_resolver_and_hygiene 153140 ns/iter (± 8299) 172615 ns/iter (± 26561) 0.89
codegen_es2015 63043 ns/iter (± 4869) 64491 ns/iter (± 11410) 0.98
codegen_es2016 61757 ns/iter (± 2640) 62922 ns/iter (± 6854) 0.98
codegen_es2017 62363 ns/iter (± 2278) 60432 ns/iter (± 5970) 1.03
codegen_es2018 61653 ns/iter (± 1328) 62804 ns/iter (± 7792) 0.98
codegen_es2019 62652 ns/iter (± 2260) 63764 ns/iter (± 7939) 0.98
codegen_es2020 61944 ns/iter (± 829) 62217 ns/iter (± 8509) 1.00
codegen_es3 63081 ns/iter (± 654) 64948 ns/iter (± 10656) 0.97
codegen_es5 62830 ns/iter (± 2424) 65188 ns/iter (± 20500) 0.96
full_es2015 211024057 ns/iter (± 10095459) 220183817 ns/iter (± 20742892) 0.96
full_es2016 164879535 ns/iter (± 10491343) 165021744 ns/iter (± 12366398) 1.00
full_es2017 171775836 ns/iter (± 7901664) 174844805 ns/iter (± 15583934) 0.98
full_es2018 168396844 ns/iter (± 12980542) 183281058 ns/iter (± 23801228) 0.92
full_es2019 168864010 ns/iter (± 10787909) 186824113 ns/iter (± 17855734) 0.90
full_es2020 166850078 ns/iter (± 8832645) 182766341 ns/iter (± 19477533) 0.91
full_es3 242058286 ns/iter (± 21831336) 273888223 ns/iter (± 36979872) 0.88
full_es5 229993498 ns/iter (± 15292279) 266075309 ns/iter (± 25730125) 0.86
parser 757782 ns/iter (± 20576) 859869 ns/iter (± 64658) 0.88
ser_ast_node 176 ns/iter (± 4) 188 ns/iter (± 13) 0.94
ser_serde 188 ns/iter (± 5) 201 ns/iter (± 9) 0.94
emit_colors 19306975 ns/iter (± 24999074) 20284715 ns/iter (± 25541432) 0.95
emit_large 104690447 ns/iter (± 160671292) 83148530 ns/iter (± 109680778) 1.26
base_clone 2655089 ns/iter (± 85471) 3022350 ns/iter (± 388081) 0.88
fold_span 4459011 ns/iter (± 127171) 4993356 ns/iter (± 761163) 0.89
fold_span_panic 4728591 ns/iter (± 173027) 5772216 ns/iter (± 1185010) 0.82
visit_mut_span 3209093 ns/iter (± 75921) 3539050 ns/iter (± 503500) 0.91
visit_mut_span_panic 3306851 ns/iter (± 87401) 3609174 ns/iter (± 286181) 0.92
boxing_boxed 175 ns/iter (± 9) 208 ns/iter (± 21) 0.84
boxing_boxed_clone 81 ns/iter (± 0) 81 ns/iter (± 8) 1
boxing_unboxed 155 ns/iter (± 4) 169 ns/iter (± 23) 0.92
boxing_unboxed_clone 77 ns/iter (± 3) 105 ns/iter (± 18) 0.73

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

Please sign in to comment.