From f1c28024e1170f8ac936cda51aab76565023fa76 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 25 Oct 2024 18:33:00 +0800 Subject: [PATCH] refactor(transformer/react-refres): using VarDeclarations to insert declarators --- crates/oxc_transformer/src/react/refresh.rs | 12 +++++------- .../output.js | 13 +++++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/oxc_transformer/src/react/refresh.rs b/crates/oxc_transformer/src/react/refresh.rs index 33934cdaad4e4e..707b22de322789 100644 --- a/crates/oxc_transformer/src/react/refresh.rs +++ b/crates/oxc_transformer/src/react/refresh.rs @@ -4,7 +4,7 @@ use sha1::{Digest, Sha1}; use oxc_allocator::CloneIn; use oxc_ast::{ast::*, match_expression, AstBuilder, NONE}; -use oxc_semantic::{Reference, ReferenceFlags, ScopeFlags, ScopeId, SymbolFlags, SymbolId}; +use oxc_semantic::{Reference, ReferenceFlags, ScopeFlags, ScopeId, SymbolFlags}; use oxc_span::{Atom, GetSpan, SPAN}; use oxc_syntax::operator::AssignmentOperator; use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx}; @@ -667,10 +667,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), @@ -678,8 +676,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/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/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/generates-valid-signature-for-exotic-ways-to-call-hooks/output.js index d1e65ba821d6af..36dad5fdf1beff 100644 --- a/tasks/transform_conformance/tests/babel-plugin-transform-react-jsx/test/fixtures/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/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");