From 90743e2a07ade652ec7938283a4113e340b813d9 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 17 Jun 2024 04:45:36 +0200 Subject: [PATCH] fix(traverse): change visit order for `Function` (#3685) Alter `Traverse`'s visitation order for fields of `Function`, to match the change made to `Visit` in #3681. --- crates/oxc_ast/src/ast/js.rs | 2 +- crates/oxc_traverse/src/ancestor.rs | 516 ++++++++++++++-------------- crates/oxc_traverse/src/walk.rs | 12 +- 3 files changed, 265 insertions(+), 265 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index ee0fbf837..634fc0856 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -2328,6 +2328,7 @@ pub struct Function<'a> { pub id: Option>, pub generator: bool, pub r#async: bool, + pub type_parameters: Option>>, /// Declaring `this` in a Function /// /// The JavaScript specification states that you cannot have a parameter called `this`, @@ -2346,7 +2347,6 @@ pub struct Function<'a> { pub this_param: Option>, pub params: Box<'a, FormalParameters<'a>>, pub body: Option>>, - pub type_parameters: Option>>, pub return_type: Option>>, /// Valid modifiers: `export`, `default`, `async` pub modifiers: Modifiers<'a>, diff --git a/crates/oxc_traverse/src/ancestor.rs b/crates/oxc_traverse/src/ancestor.rs index efc9cde3f..6c8656d9b 100644 --- a/crates/oxc_traverse/src/ancestor.rs +++ b/crates/oxc_traverse/src/ancestor.rs @@ -140,10 +140,10 @@ pub(crate) enum AncestorType { ArrayPatternRest = 108, BindingRestElementArgument = 109, FunctionId = 110, - FunctionThisParam = 111, - FunctionParams = 112, - FunctionBody = 113, - FunctionTypeParameters = 114, + FunctionTypeParameters = 111, + FunctionThisParam = 112, + FunctionParams = 113, + FunctionBody = 114, FunctionReturnType = 115, FormalParametersItems = 116, FormalParametersRest = 117, @@ -524,11 +524,11 @@ pub enum Ancestor<'a> { BindingRestElementArgument(BindingRestElementWithoutArgument<'a>) = AncestorType::BindingRestElementArgument as u16, FunctionId(FunctionWithoutId<'a>) = AncestorType::FunctionId as u16, + FunctionTypeParameters(FunctionWithoutTypeParameters<'a>) = + AncestorType::FunctionTypeParameters as u16, FunctionThisParam(FunctionWithoutThisParam<'a>) = AncestorType::FunctionThisParam as u16, FunctionParams(FunctionWithoutParams<'a>) = AncestorType::FunctionParams as u16, FunctionBody(FunctionWithoutBody<'a>) = AncestorType::FunctionBody as u16, - FunctionTypeParameters(FunctionWithoutTypeParameters<'a>) = - AncestorType::FunctionTypeParameters as u16, FunctionReturnType(FunctionWithoutReturnType<'a>) = AncestorType::FunctionReturnType as u16, FormalParametersItems(FormalParametersWithoutItems<'a>) = AncestorType::FormalParametersItems as u16, @@ -1230,10 +1230,10 @@ impl<'a> Ancestor<'a> { matches!( self, Self::FunctionId(_) + | Self::FunctionTypeParameters(_) | Self::FunctionThisParam(_) | Self::FunctionParams(_) | Self::FunctionBody(_) - | Self::FunctionTypeParameters(_) | Self::FunctionReturnType(_) ) } @@ -5173,10 +5173,10 @@ pub(crate) const OFFSET_FUNCTION_SPAN: usize = offset_of!(Function, span); pub(crate) const OFFSET_FUNCTION_ID: usize = offset_of!(Function, id); pub(crate) const OFFSET_FUNCTION_GENERATOR: usize = offset_of!(Function, generator); pub(crate) const OFFSET_FUNCTION_ASYNC: usize = offset_of!(Function, r#async); +pub(crate) const OFFSET_FUNCTION_TYPE_PARAMETERS: usize = offset_of!(Function, type_parameters); pub(crate) const OFFSET_FUNCTION_THIS_PARAM: usize = offset_of!(Function, this_param); pub(crate) const OFFSET_FUNCTION_PARAMS: usize = offset_of!(Function, params); pub(crate) const OFFSET_FUNCTION_BODY: usize = offset_of!(Function, body); -pub(crate) const OFFSET_FUNCTION_TYPE_PARAMETERS: usize = offset_of!(Function, type_parameters); pub(crate) const OFFSET_FUNCTION_RETURN_TYPE: usize = offset_of!(Function, return_type); pub(crate) const OFFSET_FUNCTION_MODIFIERS: usize = offset_of!(Function, modifiers); pub(crate) const OFFSET_FUNCTION_SCOPE_ID: usize = offset_of!(Function, scope_id); @@ -5206,6 +5206,14 @@ impl<'a> FunctionWithoutId<'a> { unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } } + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) + } + } + #[inline] pub fn this_param(&self) -> &Option> { unsafe { @@ -5230,248 +5238,6 @@ impl<'a> FunctionWithoutId<'a> { } } - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn return_type(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) - as *const Option>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } - } - - #[inline] - pub fn scope_id(&self) -> &Cell> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) - } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct FunctionWithoutThisParam<'a>(pub(crate) *const Function<'a>); - -impl<'a> FunctionWithoutThisParam<'a> { - #[inline] - pub fn r#type(&self) -> &FunctionType { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) - as *const Option>) - } - } - - #[inline] - pub fn generator(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } - } - - #[inline] - pub fn r#async(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } - } - - #[inline] - pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) - as *const Box<'a, FormalParameters<'a>>) - } - } - - #[inline] - pub fn body(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) - as *const Option>>) - } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn return_type(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) - as *const Option>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } - } - - #[inline] - pub fn scope_id(&self) -> &Cell> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) - } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct FunctionWithoutParams<'a>(pub(crate) *const Function<'a>); - -impl<'a> FunctionWithoutParams<'a> { - #[inline] - pub fn r#type(&self) -> &FunctionType { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) - as *const Option>) - } - } - - #[inline] - pub fn generator(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } - } - - #[inline] - pub fn r#async(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } - } - - #[inline] - pub fn this_param(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) - as *const Option>) - } - } - - #[inline] - pub fn body(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) - as *const Option>>) - } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) - } - } - - #[inline] - pub fn return_type(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) - as *const Option>>) - } - } - - #[inline] - pub fn modifiers(&self) -> &Modifiers<'a> { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } - } - - #[inline] - pub fn scope_id(&self) -> &Cell> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) - } - } -} - -#[repr(transparent)] -#[derive(Debug)] -pub struct FunctionWithoutBody<'a>(pub(crate) *const Function<'a>); - -impl<'a> FunctionWithoutBody<'a> { - #[inline] - pub fn r#type(&self) -> &FunctionType { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } - } - - #[inline] - pub fn span(&self) -> &Span { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } - } - - #[inline] - pub fn id(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) - as *const Option>) - } - } - - #[inline] - pub fn generator(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } - } - - #[inline] - pub fn r#async(&self) -> &bool { - unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } - } - - #[inline] - pub fn this_param(&self) -> &Option> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) - as *const Option>) - } - } - - #[inline] - pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) - as *const Box<'a, FormalParameters<'a>>) - } - } - - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) - } - } - #[inline] pub fn return_type(&self) -> &Option>> { unsafe { @@ -5571,6 +5337,240 @@ impl<'a> FunctionWithoutTypeParameters<'a> { } } +#[repr(transparent)] +#[derive(Debug)] +pub struct FunctionWithoutThisParam<'a>(pub(crate) *const Function<'a>); + +impl<'a> FunctionWithoutThisParam<'a> { + #[inline] + pub fn r#type(&self) -> &FunctionType { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) + as *const Option>) + } + } + + #[inline] + pub fn generator(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } + } + + #[inline] + pub fn r#async(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) + as *const Box<'a, FormalParameters<'a>>) + } + } + + #[inline] + pub fn body(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) + as *const Option>>) + } + } + + #[inline] + pub fn return_type(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } + } + + #[inline] + pub fn scope_id(&self) -> &Cell> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) + } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct FunctionWithoutParams<'a>(pub(crate) *const Function<'a>); + +impl<'a> FunctionWithoutParams<'a> { + #[inline] + pub fn r#type(&self) -> &FunctionType { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) + as *const Option>) + } + } + + #[inline] + pub fn generator(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } + } + + #[inline] + pub fn r#async(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn this_param(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) + as *const Option>) + } + } + + #[inline] + pub fn body(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) + as *const Option>>) + } + } + + #[inline] + pub fn return_type(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } + } + + #[inline] + pub fn scope_id(&self) -> &Cell> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) + } + } +} + +#[repr(transparent)] +#[derive(Debug)] +pub struct FunctionWithoutBody<'a>(pub(crate) *const Function<'a>); + +impl<'a> FunctionWithoutBody<'a> { + #[inline] + pub fn r#type(&self) -> &FunctionType { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } + } + + #[inline] + pub fn span(&self) -> &Span { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_SPAN) as *const Span) } + } + + #[inline] + pub fn id(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_ID) + as *const Option>) + } + } + + #[inline] + pub fn generator(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_GENERATOR) as *const bool) } + } + + #[inline] + pub fn r#async(&self) -> &bool { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } + } + + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) + } + } + + #[inline] + pub fn this_param(&self) -> &Option> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) + as *const Option>) + } + } + + #[inline] + pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) + as *const Box<'a, FormalParameters<'a>>) + } + } + + #[inline] + pub fn return_type(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_RETURN_TYPE) + as *const Option>>) + } + } + + #[inline] + pub fn modifiers(&self) -> &Modifiers<'a> { + unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } + } + + #[inline] + pub fn scope_id(&self) -> &Cell> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_SCOPE_ID) as *const Cell>) + } + } +} + #[repr(transparent)] #[derive(Debug)] pub struct FunctionWithoutReturnType<'a>(pub(crate) *const Function<'a>); @@ -5604,6 +5604,14 @@ impl<'a> FunctionWithoutReturnType<'a> { unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) } } + #[inline] + pub fn type_parameters(&self) -> &Option>> { + unsafe { + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) + } + } + #[inline] pub fn this_param(&self) -> &Option> { unsafe { @@ -5628,14 +5636,6 @@ impl<'a> FunctionWithoutReturnType<'a> { } } - #[inline] - pub fn type_parameters(&self) -> &Option>> { - unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) - } - } - #[inline] pub fn modifiers(&self) -> &Modifiers<'a> { unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) } diff --git a/crates/oxc_traverse/src/walk.rs b/crates/oxc_traverse/src/walk.rs index d8a5e69ca..917fbc585 100644 --- a/crates/oxc_traverse/src/walk.rs +++ b/crates/oxc_traverse/src/walk.rs @@ -2266,6 +2266,12 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>( { walk_binding_identifier(traverser, field as *mut _, ctx); } + if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_TYPE_PARAMETERS) + as *mut Option>) + { + ctx.retag_stack(AncestorType::FunctionTypeParameters); + walk_ts_type_parameter_declaration(traverser, (&mut **field) as *mut _, ctx); + } if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_THIS_PARAM) as *mut Option) { @@ -2285,12 +2291,6 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>( ctx.retag_stack(AncestorType::FunctionBody); walk_function_body(traverser, (&mut **field) as *mut _, ctx); } - if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_TYPE_PARAMETERS) - as *mut Option>) - { - ctx.retag_stack(AncestorType::FunctionTypeParameters); - walk_ts_type_parameter_declaration(traverser, (&mut **field) as *mut _, ctx); - } if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_RETURN_TYPE) as *mut Option>) {