1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
pub fn edge_default_param() -> impl Fold {
EdgeDefaultParam::default()
}
#[derive(Default, Clone, Copy)]
struct EdgeDefaultParam {
in_arrow: bool,
}
impl Fold for EdgeDefaultParam {
noop_fold_type!();
fn fold_arrow_expr(&mut self, n: ArrowExpr) -> ArrowExpr {
self.in_arrow = true;
let params = n.params.fold_with(self);
self.in_arrow = false;
let body = n.body.fold_with(self);
ArrowExpr { params, body, ..n }
}
fn fold_object_pat(&mut self, n: ObjectPat) -> ObjectPat {
let n = n.fold_children_with(self);
if !self.in_arrow {
return n;
}
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()),
value: Box::new(Pat::Assign(AssignPat {
span: assign_pat.span,
left: Box::new(Pat::Ident(BindingIdent::from(
assign_pat.key.clone(),
))),
right: value.clone(),
type_ann: None,
})),
})
} else {
ObjectPatProp::Assign(assign_pat)
}
}
_ => prop,
})
.collect();
ObjectPat { props, ..n }
}
}
#[cfg(test)]
mod tests {
use super::*;
use swc_ecma_transforms_testing::test;
test!(
::swc_ecma_parser::Syntax::default(),
|_| edge_default_param(),
destructured_default_value,
"const f = ({ a = 1 }) => a;",
"const f = ({ a: a = 1 }) => a;"
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| edge_default_param(),
destructured_no_default_value,
"const f = ({ a }) => a;",
"const f = ({ a }) => a;"
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| edge_default_param(),
nested_default_value,
"const f = ({ a: { b = 1 } }) => [a, b];",
"const f = ({ a: { b: b = 1 } }) => [a, b];"
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| edge_default_param(),
non_arguments,
"const f = () => { const { a = 1 } = {}; };",
"const f = () => { const { a = 1 } = {}; };"
);
}