Skip to content

Commit

Permalink
refactor(transformer/react-refres): using VarDeclarations to insert d…
Browse files Browse the repository at this point in the history
…eclarators
  • Loading branch information
Dunqing authored and overlookmotel committed Oct 25, 2024
1 parent dc2e0ff commit a2c7da3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 54 deletions.
13 changes: 0 additions & 13 deletions crates/oxc_transformer/src/jsx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use oxc_allocator::Vec;
use oxc_ast::{ast::*, AstBuilder};
use oxc_traverse::{Traverse, TraverseCtx};

Expand Down Expand Up @@ -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>,
Expand Down
39 changes: 4 additions & 35 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>)>,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -667,19 +638,17 @@ 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),
NONE,
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 [] });
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
fn enter_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.common.enter_statements(stmts, ctx);
self.x0_typescript.enter_statements(stmts, ctx);
self.x1_jsx.enter_statements(stmts, ctx);
}

fn exit_arrow_function_expression(
Expand Down Expand Up @@ -341,7 +340,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {

fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.exit_statements(stmts, ctx);
self.x1_jsx.exit_statements(stmts, ctx);
self.common.exit_statements(stmts, ctx);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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$();
Expand All @@ -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");

0 comments on commit a2c7da3

Please sign in to comment.