Skip to content

Commit

Permalink
fix: support FunctionExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jun 29, 2024
1 parent 9308f79 commit 8f22bc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
31 changes: 26 additions & 5 deletions oxc-rsc/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,12 @@ fn ast_hoist_declaration<'a>(
}

impl<'a> Traverse<'a> for HoistTransformer<'a> {
// Expression::ArrowFunctionExpression
fn exit_expression(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut oxc_traverse::TraverseCtx<'a>,
) {
match expr {
Expression::FunctionExpression(_node) => {
// TODO
}
Expression::ArrowFunctionExpression(node) => {
if has_directive(&node.body, &self.directive) {
// replace function definition with action register and bind
Expand Down Expand Up @@ -217,11 +213,36 @@ impl<'a> Traverse<'a> for HoistTransformer<'a> {
*expr = new_expr;
}
}
Expression::FunctionExpression(node) => {
if let Some(body) = &node.body {
if has_directive(&body, &self.directive) {
let new_name = format!("$$hoist_{}", self.hoisted_functions.len());
let bind_vars = get_bind_vars(ctx, node.span);
let new_expr = ast_register_bind_expression(
ctx,
&self.id,
&self.runtime,
&new_name,
&bind_vars,
);

self.hoisted_functions.push(ast_hoist_declaration(
ctx,
node.span,
&new_name,
&node.params,
node.body.as_ref().unwrap(),
&bind_vars,
));

*expr = new_expr;
}
}
}
_ => {}
}
}

// Statement::FunctionDeclaration
fn exit_statement(
&mut self,
stmt: &mut Statement<'a>,
Expand Down
8 changes: 4 additions & 4 deletions oxc-rsc/tests/hoist/function-expression.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let count = 0;
export function Counter() {
return {
type: 'form',
action: function(formData) {
'use server';
count += Number(formData.get('name'));
}
action: $$register($$hoist_0, '<id>', '$$hoist_0')
};
}
export async function $$hoist_0(formData) {
count += Number(formData.get('name'));
}

0 comments on commit 8f22bc7

Please sign in to comment.