Skip to content

Commit 235ea13

Browse files
committed
[core][rewriter] dpsc: handle destructuring in bindings
1 parent ebe70c3 commit 235ea13

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

rewriter/js/src/rewrite.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ impl<'alloc: 'data, 'data> Rewrite<'alloc, 'data> {
9090

9191
impl<'alloc: 'data, 'data> RewriteType<'alloc, 'data> {
9292
fn into_inner(self, span: Span) -> SmallVec<[JsChange<'alloc, 'data>; 2]> {
93-
dbg!(&self);
9493
macro_rules! span {
9594
(start) => {
9695
Span::new(span.start, span.start)

rewriter/js/src/visitor.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use oxc::{
44
allocator::{Allocator, StringBuilder},
55
ast::ast::{
66
AssignmentExpression, AssignmentTarget, AssignmentTargetMaybeDefault,
7-
AssignmentTargetProperty, BindingPattern, CallExpression, ComputedMemberExpression,
8-
DebuggerStatement, ExportAllDeclaration, ExportNamedDeclaration, Expression, FunctionBody,
9-
IdentifierReference, ImportDeclaration, ImportExpression, MemberExpression, MetaProperty,
10-
NewExpression, ObjectExpression, ObjectPattern, ObjectPropertyKind, PrivateIdentifier,
11-
PropertyKey, ReturnStatement, SimpleAssignmentTarget, StringLiteral, ThisExpression,
12-
UnaryExpression, UnaryOperator, UpdateExpression,
7+
AssignmentTargetProperty, BindingPattern, BindingPatternKind, BindingProperty,
8+
CallExpression, ComputedMemberExpression, DebuggerStatement, ExportAllDeclaration,
9+
ExportNamedDeclaration, Expression, FunctionBody, IdentifierReference, ImportDeclaration,
10+
ImportExpression, MemberExpression, MetaProperty, NewExpression, ObjectExpression,
11+
ObjectPattern, ObjectPropertyKind, PrivateIdentifier, PropertyKey, ReturnStatement,
12+
SimpleAssignmentTarget, StringLiteral, ThisExpression, UnaryExpression, UnaryOperator,
13+
UpdateExpression,
1314
},
1415
ast_visit::{Visit, walk},
1516
span::{Atom, GetSpan, Span},
@@ -328,6 +329,53 @@ where
328329
}
329330
}
330331

332+
fn visit_binding_pattern(&mut self, it: &BindingPattern<'data>) {
333+
match &it.kind {
334+
BindingPatternKind::BindingIdentifier(p) => {
335+
// let a = 0;
336+
dbg!(&p);
337+
walk::walk_binding_identifier(self, p);
338+
}
339+
BindingPatternKind::AssignmentPattern(p) => {
340+
walk::walk_binding_pattern(self, &p.left);
341+
walk::walk_expression(self, &p.right);
342+
dbg!(&p);
343+
}
344+
BindingPatternKind::ObjectPattern(p) => {
345+
for prop in &p.properties {
346+
match &prop.key {
347+
PropertyKey::StaticIdentifier(id) => {
348+
if UNSAFE_GLOBALS.contains(&id.name.to_string().as_str()) {
349+
if prop.shorthand {
350+
// const { location } = self;
351+
self.jschanges.add(rewrite!(
352+
id.span(),
353+
RebindProperty { ident: id.name }
354+
));
355+
} else {
356+
// const { location: a } = self;
357+
self.jschanges.add(rewrite!(
358+
id.span(),
359+
RewriteProperty { ident: id.name }
360+
));
361+
}
362+
}
363+
}
364+
PropertyKey::PrivateIdentifier(_) => {
365+
// doesn't matter
366+
}
367+
_ => {
368+
// const { ["location"]: x } = self;
369+
self.jschanges.add(rewrite!(prop.key.span(), WrapProperty));
370+
}
371+
}
372+
walk::walk_binding_pattern(self, &prop.value);
373+
}
374+
}
375+
_ => {}
376+
}
377+
}
378+
331379
fn visit_assignment_target(&mut self, it: &AssignmentTarget<'data>) {
332380
match &it {
333381
AssignmentTarget::StaticMemberExpression(s) => {

0 commit comments

Comments
 (0)