diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index ee0fbf837e32b..634fc0856999c 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 efc9cde3f86d1..6c8656d9be28b 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,14 +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 { @@ -5261,9 +5261,9 @@ impl<'a> FunctionWithoutId<'a> { #[repr(transparent)] #[derive(Debug)] -pub struct FunctionWithoutThisParam<'a>(pub(crate) *const Function<'a>); +pub struct FunctionWithoutTypeParameters<'a>(pub(crate) *const Function<'a>); -impl<'a> FunctionWithoutThisParam<'a> { +impl<'a> FunctionWithoutTypeParameters<'a> { #[inline] pub fn r#type(&self) -> &FunctionType { unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE) as *const FunctionType) } @@ -5292,6 +5292,14 @@ impl<'a> FunctionWithoutThisParam<'a> { 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 { @@ -5308,14 +5316,6 @@ impl<'a> FunctionWithoutThisParam<'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 { @@ -5339,9 +5339,9 @@ impl<'a> FunctionWithoutThisParam<'a> { #[repr(transparent)] #[derive(Debug)] -pub struct FunctionWithoutParams<'a>(pub(crate) *const Function<'a>); +pub struct FunctionWithoutThisParam<'a>(pub(crate) *const Function<'a>); -impl<'a> FunctionWithoutParams<'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) } @@ -5371,26 +5371,26 @@ impl<'a> FunctionWithoutParams<'a> { } #[inline] - pub fn this_param(&self) -> &Option> { + pub fn type_parameters(&self) -> &Option>> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) - as *const Option>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) } } #[inline] - pub fn body(&self) -> &Option>> { + pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) - as *const Option>>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) + as *const Box<'a, FormalParameters<'a>>) } } #[inline] - pub fn type_parameters(&self) -> &Option>> { + pub fn body(&self) -> &Option>> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) + as *const Option>>) } } @@ -5417,9 +5417,9 @@ impl<'a> FunctionWithoutParams<'a> { #[repr(transparent)] #[derive(Debug)] -pub struct FunctionWithoutBody<'a>(pub(crate) *const Function<'a>); +pub struct FunctionWithoutParams<'a>(pub(crate) *const Function<'a>); -impl<'a> FunctionWithoutBody<'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) } @@ -5449,26 +5449,26 @@ impl<'a> FunctionWithoutBody<'a> { } #[inline] - pub fn this_param(&self) -> &Option> { + pub fn type_parameters(&self) -> &Option>> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) - as *const Option>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) + as *const Option>>) } } #[inline] - pub fn params(&self) -> &Box<'a, FormalParameters<'a>> { + pub fn this_param(&self) -> &Option> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS) - as *const Box<'a, FormalParameters<'a>>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM) + as *const Option>) } } #[inline] - pub fn type_parameters(&self) -> &Option>> { + pub fn body(&self) -> &Option>> { unsafe { - &*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS) - as *const Option>>) + &*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY) + as *const Option>>) } } @@ -5495,9 +5495,9 @@ impl<'a> FunctionWithoutBody<'a> { #[repr(transparent)] #[derive(Debug)] -pub struct FunctionWithoutTypeParameters<'a>(pub(crate) *const Function<'a>); +pub struct FunctionWithoutBody<'a>(pub(crate) *const Function<'a>); -impl<'a> FunctionWithoutTypeParameters<'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) } @@ -5526,6 +5526,14 @@ impl<'a> FunctionWithoutTypeParameters<'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 { @@ -5542,14 +5550,6 @@ impl<'a> FunctionWithoutTypeParameters<'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 { @@ -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 d8a5e69ca730a..917fbc585bb02 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>) {