From 4504b4624aa1146277703eab98bd8709542f9513 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 14 Nov 2024 23:29:35 +0800 Subject: [PATCH] w --- crates/oxc_transformer/src/es2018/mod.rs | 6 +++ .../src/es2018/object_rest_spread.rs | 40 +++++++++++-------- crates/oxc_transformer/src/lib.rs | 3 ++ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crates/oxc_transformer/src/es2018/mod.rs b/crates/oxc_transformer/src/es2018/mod.rs index 381ae21f183b5..9073ca09f5291 100644 --- a/crates/oxc_transformer/src/es2018/mod.rs +++ b/crates/oxc_transformer/src/es2018/mod.rs @@ -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); + } + } } diff --git a/crates/oxc_transformer/src/es2018/object_rest_spread.rs b/crates/oxc_transformer/src/es2018/object_rest_spread.rs index 631bdf16615c7..0ca49bbfce2ca 100644 --- a/crates/oxc_transformer/src/es2018/object_rest_spread.rs +++ b/crates/oxc_transformer/src/es2018/object_rest_spread.rs @@ -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}; @@ -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); } @@ -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, + } } } @@ -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( diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 7db39832e2345..43131cc32f665 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -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,