Skip to content

Commit

Permalink
fix(transformer): ArrowfunctionExpression's expression is true but ha…
Browse files Browse the repository at this point in the history
…s more than one body statement
  • Loading branch information
Dunqing committed Aug 31, 2024
1 parent 8d565d5 commit 9e53460
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use oxc_allocator::{Allocator, Vec};
use oxc_ast::{ast::*, Trivias};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::SourceType;
use oxc_span::{SourceType, SPAN};
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};

pub use crate::{
Expand Down Expand Up @@ -284,6 +284,26 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x2_es2016.enter_statements(stmts, ctx);
}

fn exit_arrow_function_expression(
&mut self,
arrow: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if arrow.expression && arrow.body.statements.len() > 1 {
arrow.expression = false;

let Some(Statement::ExpressionStatement(statement)) = arrow.body.statements.pop()
else {
unreachable!("arrow function body is never empty")
};

arrow
.body
.statements
.push(ctx.ast.statement_return(SPAN, Some(statement.unbox().expression)));
}
}

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.transform_statements_on_exit(stmts, ctx);
self.x1_react.transform_statements_on_exit(stmts, ctx);
Expand Down

0 comments on commit 9e53460

Please sign in to comment.