refactor(transformer/react-refresh): use VarDeclarations to insert declarators (#6884)

This commit is contained in:
Dunqing 2024-10-27 07:05:24 +00:00
parent df3b089f29
commit 5f153aca0c
4 changed files with 13 additions and 54 deletions

View file

@ -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>,

View file

@ -101,7 +101,6 @@ pub struct ReactRefresh<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
// States
registrations: Vec<(BoundIdentifier<'a>, Atom<'a>)>,
signature_declarator_items: Vec<oxc_allocator::Vec<'a, VariableDeclarator<'a>>>,
/// 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 [] });

View file

@ -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);
}

View file

@ -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");