Skip to content

Commit

Permalink
fix(traverse): change visit order for Function (#3685)
Browse files Browse the repository at this point in the history
Alter `Traverse`'s visitation order for fields of `Function`, to match
the change made to `Visit` in #3681.
  • Loading branch information
overlookmotel authored Jun 17, 2024
1 parent 6c1525e commit 90743e2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 73 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 @@ -2328,6 +2328,7 @@ pub struct Function<'a> {
pub id: Option<BindingIdentifier<'a>>,
pub generator: bool,
pub r#async: bool,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
/// Declaring `this` in a Function <https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function>
///
/// The JavaScript specification states that you cannot have a parameter called `this`,
Expand All @@ -2346,7 +2347,6 @@ pub struct Function<'a> {
pub this_param: Option<TSThisParameter<'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
132 changes: 66 additions & 66 deletions crates/oxc_traverse/src/ancestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1230,10 +1230,10 @@ impl<'a> Ancestor<'a> {
matches!(
self,
Self::FunctionId(_)
| Self::FunctionTypeParameters(_)
| Self::FunctionThisParam(_)
| Self::FunctionParams(_)
| Self::FunctionBody(_)
| Self::FunctionTypeParameters(_)
| Self::FunctionReturnType(_)
)
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<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 this_param(&self) -> &Option<TSThisParameter<'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 All @@ -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) }
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 this_param(&self) -> &Option<TSThisParameter<'a>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM)
as *const Option<TSThisParameter<'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 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) }
Expand Down Expand Up @@ -5371,26 +5371,26 @@ impl<'a> FunctionWithoutParams<'a> {
}

#[inline]
pub fn this_param(&self) -> &Option<TSThisParameter<'a>> {
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM)
as *const Option<TSThisParameter<'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>>)
}
}

#[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 @@ -5449,26 +5449,26 @@ impl<'a> FunctionWithoutBody<'a> {
}

#[inline]
pub fn this_param(&self) -> &Option<TSThisParameter<'a>> {
pub fn type_parameters(&self) -> &Option<Box<'a, TSTypeParameterDeclaration<'a>>> {
unsafe {
&*((self.0 as *const u8).add(OFFSET_FUNCTION_THIS_PARAM)
as *const Option<TSThisParameter<'a>>)
&*((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>> {
pub fn this_param(&self) -> &Option<TSThisParameter<'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_THIS_PARAM)
as *const Option<TSThisParameter<'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 @@ -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<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 this_param(&self) -> &Option<TSThisParameter<'a>> {
unsafe {
Expand All @@ -5542,14 +5550,6 @@ impl<'a> FunctionWithoutTypeParameters<'a> {
}
}

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

#[inline]
pub fn return_type(&self) -> &Option<Box<'a, TSTypeAnnotation<'a>>> {
unsafe {
Expand Down Expand Up @@ -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<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 this_param(&self) -> &Option<TSThisParameter<'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 @@ -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<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_THIS_PARAM)
as *mut Option<TSThisParameter>)
{
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 90743e2

Please sign in to comment.