diff --git a/crates/oxc_transformer/src/jsx/mod.rs b/crates/oxc_transformer/src/jsx/mod.rs index 81055b324..41aca1f7f 100644 --- a/crates/oxc_transformer/src/jsx/mod.rs +++ b/crates/oxc_transformer/src/jsx/mod.rs @@ -1,4 +1,3 @@ -use oxc_allocator::Vec; use oxc_ast::{ast::*, AstBuilder}; use oxc_traverse::{Traverse, TraverseCtx}; @@ -84,18 +83,6 @@ impl<'a, 'ctx> Traverse<'a> for Jsx<'a, 'ctx> { } } - fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { - if self.refresh_plugin { - self.refresh.enter_statements(stmts, ctx); - } - } - - fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) { - if self.refresh_plugin { - self.refresh.exit_statements(stmts, ctx); - } - } - fn enter_call_expression( &mut self, call_expr: &mut CallExpression<'a>, diff --git a/crates/oxc_transformer/src/jsx/refresh.rs b/crates/oxc_transformer/src/jsx/refresh.rs index 42d8a7bcd..40e29afa1 100644 --- a/crates/oxc_transformer/src/jsx/refresh.rs +++ b/crates/oxc_transformer/src/jsx/refresh.rs @@ -101,7 +101,6 @@ pub struct ReactRefresh<'a, 'ctx> { ctx: &'ctx TransformCtx<'a>, // States registrations: Vec<(BoundIdentifier<'a>, Atom<'a>)>, - signature_declarator_items: Vec>>, /// Used to wrap call expression with signature. /// (eg: hoc(() => {}) -> _s1(hoc(_s1(() => {})))) last_signature: Option<(BindingIdentifier<'a>, oxc_allocator::Vec<'a, Argument<'a>>)>, @@ -120,7 +119,6 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> { refresh_reg: RefreshIdentifierResolver::parse(&options.refresh_reg, ast), refresh_sig: RefreshIdentifierResolver::parse(&options.refresh_sig, ast), emit_full_signatures: options.emit_full_signatures, - signature_declarator_items: Vec::new(), registrations: Vec::default(), ctx, last_signature: None, @@ -179,33 +177,6 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> { program.body.extend(new_statements); } - fn enter_statements( - &mut self, - _stmts: &mut oxc_allocator::Vec<'a, Statement<'a>>, - ctx: &mut TraverseCtx<'a>, - ) { - self.signature_declarator_items.push(ctx.ast.vec()); - } - - fn exit_statements( - &mut self, - stmts: &mut oxc_allocator::Vec<'a, Statement<'a>>, - ctx: &mut TraverseCtx<'a>, - ) { - let declarations = self.signature_declarator_items.pop().unwrap(); - if !declarations.is_empty() { - stmts.insert( - 0, - Statement::from(ctx.ast.declaration_variable( - SPAN, - VariableDeclarationKind::Var, - declarations, - false, - )), - ); - } - } - fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { let signature = match expr { Expression::FunctionExpression(func) => self.create_signature_call_expression( @@ -671,10 +642,8 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> { body.statements.insert(0, call_expression); // _s = refresh_sig(); - self.signature_declarator_items.last_mut().unwrap().push(ctx.ast.variable_declarator( - SPAN, - VariableDeclarationKind::Var, - binding.create_binding_pattern(ctx), + self.ctx.var_declarations.insert( + &binding, Some(ctx.ast.expression_call( SPAN, self.refresh_sig.to_expression(ctx), @@ -682,8 +651,8 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> { ctx.ast.vec(), false, )), - false, - )); + ctx, + ); // Following is the signature call expression, will be generated in call site. // _s(App, signature_key, false, function() { return [] }); diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 46af91035..a75fbd545 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -361,7 +361,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { if let Some(typescript) = self.x0_typescript.as_mut() { typescript.enter_statements(stmts, ctx); } - self.x1_jsx.enter_statements(stmts, ctx); } fn exit_arrow_function_expression( @@ -392,7 +391,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> { if let Some(typescript) = self.x0_typescript.as_mut() { typescript.exit_statements(stmts, ctx); } - self.x1_jsx.exit_statements(stmts, ctx); self.common.exit_statements(stmts, ctx); } diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/refresh/react-refresh/generates-valid-signature-for-exotic-ways-to-call-hooks/output.js b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/refresh/react-refresh/generates-valid-signature-for-exotic-ways-to-call-hooks/output.js index d1e65ba82..36dad5fdf 100644 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/refresh/react-refresh/generates-valid-signature-for-exotic-ways-to-call-hooks/output.js +++ b/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/refresh/react-refresh/generates-valid-signature-for-exotic-ways-to-call-hooks/output.js @@ -1,6 +1,6 @@ -var _s2 = $RefreshSig$(); import FancyHook from "fancy"; import { jsxs as _jsxs } from "react/jsx-runtime"; +var _s2 = $RefreshSig$(); export default function App() { _s2(); var _s = $RefreshSig$(); @@ -18,9 +18,14 @@ export default function App() { use(); return _jsxs("h1", { children: [bar, baz] }); } -_s2(App, "useFancyState{bar}\\nuseThing{baz}\\nuseState{}\\nuseThePlatform{}\\nuse{}", true, function() { - return [FancyHook.useThing]; -}); +_s2( + App, + "useFancyState{bar}\\nuseThing{baz}\\nuseState{}\\nuseThePlatform{}\\nuse{}", + true, + function () { + return [FancyHook.useThing]; + }, +); _c = App; var _c; $RefreshReg$(_c, "App");