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 58718a3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
24 changes: 23 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,28 @@ 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>,
) {
// Some plugins may add new statements to the ArrowFunctionExpression's body,
// which can cause issues with the `() => x;` case, as it only allows a single statement.
// To address this, we wrap the last statement in a return statement and set the expression to false.
// This transforms the arrow function into the form `() => { return x; };`.
if arrow.expression && arrow.body.statements.len() > 1 {
let Statement::ExpressionStatement(statement) = arrow.body.statements.pop().unwrap()
else {
unreachable!("The last statement in an ArrowFunctionExpression should always be an ExpressionStatement.")
};
arrow
.body
.statements
.push(ctx.ast.statement_return(SPAN, Some(statement.unbox().expression)));
arrow.expression = false;
}
}

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
10 changes: 9 additions & 1 deletion tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
commit: 12619ffe

Passed: 10/37
Passed: 10/38

# All Passed:
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-arrow-functions


# babel-plugin-transform-nullish-coalescing-operator (0/1)
* transform-in-arrow-function-expression/input.js
x Reference flags mismatch:
| after transform: ReferenceId(3): ReferenceFlags(Write)
| rebuilt : ReferenceId(0): ReferenceFlags(Read | Write)



# babel-plugin-transform-typescript (2/8)
* class-property-definition/input.ts
x Unresolved references mismatch:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["transform-nullish-coalescing-operator"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(() => a ?? b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
() => {
var _a;
return (_a = a) !== null && _a !== void 0 ? _a : b;
};

0 comments on commit 58718a3

Please sign in to comment.