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 committed Oct 25, 2024
1 parent 14d8b70 commit f1c2802
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 5 additions & 7 deletions crates/oxc_transformer/src/react/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -667,19 +667,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
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 f1c2802

Please sign in to comment.