Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 14, 2024
1 parent 1518ba7 commit 4504b46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
6 changes: 6 additions & 0 deletions crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@ impl<'a, 'ctx> Traverse<'a> for ES2018<'a, 'ctx> {
self.object_rest_spread.enter_catch_clause(clause, ctx);
}
}

fn enter_array_pattern(&mut self, pat: &mut ArrayPattern<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.object_rest_spread.is_some() {
self.object_rest_spread.enter_array_pattern(pat, ctx);
}
}
}
40 changes: 23 additions & 17 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_ecmascript::{BoundNames, ToJsString};
use oxc_semantic::{IsGlobalReference, ScopeId, SymbolFlags};
use oxc_span::{GetSpan, SPAN};
use oxc_traverse::{BoundIdentifier, MaybeBoundIdentifier, Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, BoundIdentifier, MaybeBoundIdentifier, Traverse, TraverseCtx};

use crate::{common::helper_loader::Helper, TransformCtx};

Expand Down Expand Up @@ -91,6 +91,10 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
}
}

fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.transform_named_export(stmt, ctx);
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.transform_object_expression(expr, ctx);
}
Expand Down Expand Up @@ -148,8 +152,24 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
}
}

fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.transform_named_export(stmt, ctx);
fn enter_array_pattern(&mut self, pat: &mut ArrayPattern<'a>, ctx: &mut TraverseCtx<'a>) {
for a in ctx.ancestors() {
dbg!(a);
}
}
}

// Utils
impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
/// Recursively check for object rest.
fn has_nested_object_rest(pat: &BindingPattern<'a>) -> bool {
match &pat.kind {
BindingPatternKind::ObjectPattern(pat) => {
pat.rest.is_some()
|| pat.properties.iter().any(|p| Self::has_nested_object_rest(&p.value))
}
_ => false,
}
}
}

Expand Down Expand Up @@ -355,20 +375,6 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
changed
}

/// Recursively check for object rest.
fn has_nested_object_rest(pat: &BindingPattern<'a>) -> bool {
match &pat.kind {
BindingPatternKind::ObjectPattern(pat) => {
pat.rest.is_some()
|| pat.properties.iter().any(|p| Self::has_nested_object_rest(&p.value))
}
BindingPatternKind::ArrayPattern(pat) => {
pat.elements.iter().any(|e| e.as_ref().is_some_and(Self::has_nested_object_rest))
}
_ => false,
}
}

// The algorithm depth first searches for object rest,
// and then mutates the object pattern in-place.
fn transform_variable_declarator(
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}

// ALPHASORT
fn enter_array_pattern(&mut self, pat: &mut ArrayPattern<'a>, ctx: &mut TraverseCtx<'a>) {
self.x2_es2018.enter_array_pattern(pat, ctx);
}

fn enter_arrow_function_expression(
&mut self,
Expand Down

0 comments on commit 4504b46

Please sign in to comment.