Skip to content

Commit

Permalink
fix(traverse): change visit order for Function
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jun 15, 2024
1 parent b22b59a commit 8351f69
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2344,9 +2344,9 @@ pub struct Function<'a> {
/// });
/// ```
pub this_param: Option<TSThisParameter<'a>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub params: Box<'a, FormalParameters<'a>>,
pub body: Option<Box<'a, FunctionBody<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
/// Valid modifiers: `export`, `default`, `async`
pub modifiers: Modifiers<'a>,
Expand Down
110 changes: 55 additions & 55 deletions crates/oxc_traverse/src/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ pub(crate) enum AncestorType {
BindingRestElementArgument = 109,
FunctionId = 110,
FunctionThisParam = 111,
FunctionParams = 112,
FunctionBody = 113,
FunctionTypeParameters = 114,
FunctionTypeParameters = 112,
FunctionParams = 113,
FunctionBody = 114,
FunctionReturnType = 115,
FormalParametersItems = 116,
FormalParametersRest = 117,
Expand Down Expand Up @@ -525,10 +525,10 @@ pub enum Ancestor<'a> {
AncestorType::BindingRestElementArgument as u16,
FunctionId(FunctionWithoutId<'a>) = AncestorType::FunctionId 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,
FunctionParams(FunctionWithoutParams<'a>) = AncestorType::FunctionParams as u16,
FunctionBody(FunctionWithoutBody<'a>) = AncestorType::FunctionBody as u16,
FunctionReturnType(FunctionWithoutReturnType<'a>) = AncestorType::FunctionReturnType as u16,
FormalParametersItems(FormalParametersWithoutItems<'a>) =
AncestorType::FormalParametersItems as u16,
Expand Down Expand Up @@ -1231,9 +1231,9 @@ impl<'a> Ancestor<'a> {
self,
Self::FunctionId(_)
| Self::FunctionThisParam(_)
| Self::FunctionTypeParameters(_)
| Self::FunctionParams(_)
| Self::FunctionBody(_)
| Self::FunctionTypeParameters(_)
| Self::FunctionReturnType(_)
)
}
Expand Down Expand Up @@ -5174,9 +5174,9 @@ 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_THIS_PARAM: usize = offset_of!(Function, this_param);
pub(crate) const OFFSET_FUNCTION_TYPE_PARAMETERS: usize = offset_of!(Function, type_parameters);
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);
Expand Down Expand Up @@ -5214,6 +5214,14 @@ impl<'a> FunctionWithoutId<'a> {
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
unsafe {
Expand All @@ -5230,14 +5238,6 @@ impl<'a> FunctionWithoutId<'a> {
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn return_type(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand Down Expand Up @@ -5292,6 +5292,14 @@ impl<'a> FunctionWithoutThisParam<'a> {
unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_ASYNC) as *const bool) }
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
unsafe {
Expand All @@ -5308,14 +5316,6 @@ impl<'a> FunctionWithoutThisParam<'a> {
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn return_type(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand All @@ -5339,9 +5339,9 @@ impl<'a> FunctionWithoutThisParam<'a> {

#[repr(transparent)]
#[derive(Debug)]
pub struct FunctionWithoutParams<'a>(pub(crate) *const Function<'a>);
pub struct FunctionWithoutTypeParameters<'a>(pub(crate) *const Function<'a>);

impl<'a> FunctionWithoutParams<'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) }
Expand Down Expand Up @@ -5379,18 +5379,18 @@ impl<'a> FunctionWithoutParams<'a> {
}

#[inline]
pub fn body(&self) -> &Option<Box<'a, FunctionBody<'a>>> {
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY)
as *const Option<Box<'a, FunctionBody<'a>>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS)
as *const Box<'a, FormalParameters<'a>>)
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
pub fn body(&self) -> &Option<Box<'a, FunctionBody<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY)
as *const Option<Box<'a, FunctionBody<'a>>>)
}
}

Expand All @@ -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) }
Expand Down Expand Up @@ -5457,18 +5457,18 @@ impl<'a> FunctionWithoutBody<'a> {
}

#[inline]
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS)
as *const Box<'a, FormalParameters<'a>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
pub fn body(&self) -> &Option<Box<'a, FunctionBody<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY)
as *const Option<Box<'a, FunctionBody<'a>>>)
}
}

Expand All @@ -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) }
Expand Down Expand Up @@ -5535,18 +5535,18 @@ impl<'a> FunctionWithoutTypeParameters<'a> {
}

#[inline]
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS)
as *const Box<'a, FormalParameters<'a>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn body(&self) -> &Option<Box<'a, FunctionBody<'a>>> {
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_BODY)
as *const Option<Box<'a, FunctionBody<'a>>>)
&*((self.0 as *const u8).add(OFFSET_FUNCTION_PARAMS)
as *const Box<'a, FormalParameters<'a>>)
}
}

Expand Down Expand Up @@ -5612,6 +5612,14 @@ impl<'a> FunctionWithoutReturnType<'a> {
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn params(&self) -> &Box<'a, FormalParameters<'a>> {
unsafe {
Expand All @@ -5628,14 +5636,6 @@ impl<'a> FunctionWithoutReturnType<'a> {
}
}

#[inline]
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_TYPE_PARAMETERS)
as *const Option<Box<'a, TSTypeParameterDeclaration<'a>>>)
}
}

#[inline]
pub fn modifiers(&self) -> &Modifiers<'a> {
unsafe { &*((self.0 as *const u8).add(OFFSET_FUNCTION_MODIFIERS) as *const Modifiers<'a>) }
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_traverse/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,12 @@ pub(crate) unsafe fn walk_function<'a, Tr: Traverse<'a>>(
ctx.retag_stack(AncestorType::FunctionThisParam);
walk_ts_this_parameter(traverser, field as *mut _, ctx);
}
if let Some(field) = &mut *((node as *mut u8).add(ancestor::OFFSET_FUNCTION_TYPE_PARAMETERS)
as *mut Option<Box<TSTypeParameterDeclaration>>)
{
ctx.retag_stack(AncestorType::FunctionTypeParameters);
walk_ts_type_parameter_declaration(traverser, (&mut **field) as *mut _, ctx);
}
ctx.retag_stack(AncestorType::FunctionParams);
walk_formal_parameters(
traverser,
Expand All @@ -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<Box<TSTypeParameterDeclaration>>)
{
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<Box<TSTypeAnnotation>>)
{
Expand Down

0 comments on commit 8351f69

Please sign in to comment.