Skip to content

Commit f5b17d2

Browse files
committed
[core][rewriter] destructure_rewrites: insert cleanrests to arrow functions
1 parent 5056ceb commit f5b17d2

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

rewriter/js/src/changes.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ pub enum JsChangeType<'alloc: 'data, 'data> {
8989
},
9090
/// replace span with ""
9191
Delete,
92-
// ;cfg.cleanrestfn(restids[0]); cfg.cleanrestfn(restid[1]);
93-
CleanRest {
92+
// ;cfg.cleanrestfn(restids[0]); cfg.cleanrestfn(restids[1]);
93+
// or
94+
// (cfg.cleanrestfn(restids[0]), cfg.cleanrestfn(restids[1]),
95+
CleanFunction {
9496
restids: Vec<Atom<'data>>,
97+
expression: bool,
9598
},
9699
}
97100

@@ -156,13 +159,25 @@ impl<'alloc: 'data, 'data> Transform<'data> for JsChange<'alloc, 'data> {
156159
let steps: &'static str = Box::leak(steps.into_boxed_str());
157160
LL::insert(transforms!["((t)=>(", &steps, "t))("])
158161
}
159-
Ty::CleanRest { restids } => {
160-
let mut steps = String::new();
161-
for id in restids {
162-
steps.push_str(&format!("{}({});", &cfg.cleanrestfn, id.as_str()));
162+
Ty::CleanFunction {
163+
restids,
164+
expression,
165+
} => {
166+
let mut steps = String::new();
167+
168+
if expression {
169+
for id in restids {
170+
steps.push_str(&format!("{}({}),", &cfg.cleanrestfn, id.as_str()));
171+
}
172+
let steps: &'static str = Box::leak(steps.into_boxed_str());
173+
LL::insert(transforms!["(", &steps])
174+
} else {
175+
for id in restids {
176+
steps.push_str(&format!("{}({});", &cfg.cleanrestfn, id.as_str()));
177+
}
178+
let steps: &'static str = Box::leak(steps.into_boxed_str());
179+
LL::insert(transforms![";", &steps])
163180
}
164-
let steps: &'static str = Box::leak(steps.into_boxed_str());
165-
LL::insert(transforms![";",&steps])
166181
}
167182
Ty::SetRealmFn => LL::insert(transforms![&cfg.setrealmfn, "({})."]),
168183
Ty::ScramErrFn { ident } => LL::insert(transforms!["$scramerr(", ident, ");"]),

rewriter/js/src/rewrite.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ pub(crate) enum RewriteType<'alloc: 'data, 'data> {
7373
SourceTag,
7474

7575
// ;cfg.cleanrestfn(restids[0]); cfg.cleanrestfn(restid[1]);
76-
CleanRest {
76+
CleanFunction {
7777
restids: Vec<Atom<'data>>,
78+
expression: bool,
7879
},
7980

8081
// don't use for anything static, only use for stuff like rewriteurl
@@ -151,7 +152,37 @@ impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
151152
}
152153
)
153154
],
154-
Self::CleanRest { restids } => smallvec![change!(span!(start), CleanRest { restids })],
155+
Self::CleanFunction {
156+
restids,
157+
expression,
158+
} => {
159+
if expression {
160+
smallvec![
161+
change!(
162+
Span::new(span.start, span.start),
163+
CleanFunction {
164+
restids,
165+
expression
166+
}
167+
),
168+
change!(
169+
Span::new(span.end, span.end),
170+
ClosingParen {
171+
semi: false,
172+
replace: false
173+
}
174+
)
175+
]
176+
} else {
177+
smallvec![change!(
178+
Span::new(span.start, span.start),
179+
CleanFunction {
180+
restids,
181+
expression
182+
}
183+
),]
184+
}
185+
}
155186
Self::SetRealmFn => smallvec![change!(span, SetRealmFn)],
156187
Self::ImportFn => smallvec![change!(span, ImportFn)],
157188
Self::MetaFn => smallvec![change!(span, MetaFn)],

rewriter/js/src/visitor.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,19 +488,42 @@ where
488488
for param in &it.params.items {
489489
self.recurse_binding_pattern(&param.pattern, &mut restids, &mut location_assigned);
490490
}
491-
dbg!(&restids);
492491

493492
if let Some(b) = &it.body {
494493
walk::walk_function_body(self, b);
495494
if let Some(stmt) = b.statements.get(0) {
496495
let span = stmt.span();
497496
self.jschanges.add(rewrite!(
498497
Span::new(span.start, span.start),
499-
CleanRest { restids }
498+
CleanFunction {
499+
restids,
500+
expression: false
501+
}
500502
));
501503
}
502504
}
503505
}
506+
fn visit_arrow_function_expression(
507+
&mut self,
508+
it: &oxc::ast::ast::ArrowFunctionExpression<'data>,
509+
) {
510+
let mut restids: Vec<Atom<'data>> = Vec::new();
511+
let mut location_assigned: bool = false;
512+
for param in &it.params.items {
513+
self.recurse_binding_pattern(&param.pattern, &mut restids, &mut location_assigned);
514+
}
515+
516+
walk::walk_function_body(self, &it.body);
517+
if let Some(stmt) = &it.body.statements.get(0) {
518+
self.jschanges.add(rewrite!(
519+
stmt.span(),
520+
CleanFunction {
521+
restids,
522+
expression: it.expression,
523+
}
524+
));
525+
}
526+
}
504527

505528
fn visit_function_body(&mut self, it: &FunctionBody<'data>) {
506529
// tag function for use in sourcemaps

0 commit comments

Comments
 (0)