Skip to content

Commit 36b26d3

Browse files
authoredJan 4, 2023
fix(es/react): Force refresh if code contains @refresh reset (#6749)
1 parent 790c478 commit 36b26d3

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed
 

‎crates/swc_ecma_transforms_react/src/refresh/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ where
249249
});
250250
}
251251

252+
comments.with_leading(n.lo, |comments| {
253+
if comments.iter().any(|c| c.text.contains("@refresh reset")) {
254+
should_refresh = true
255+
}
256+
});
257+
252258
comments.with_trailing(n.lo, |comments| {
253259
if comments.iter().any(|c| c.text.contains("@refresh reset")) {
254260
should_refresh = true
@@ -265,14 +271,18 @@ impl<C: Comments> VisitMut for Refresh<C> {
265271
// Does anyone write react without esmodule?
266272
// fn visit_mut_script(&mut self, _: &mut Script) {}
267273

268-
fn visit_mut_module_items(&mut self, module_items: &mut Vec<ModuleItem>) {
274+
fn visit_mut_module(&mut self, n: &mut Module) {
269275
if !self.enable {
270276
return;
271277
}
272278

273279
// to collect comments
274-
self.visit_module_items(module_items);
280+
self.visit_module(n);
281+
282+
self.visit_mut_module_items(&mut n.body);
283+
}
275284

285+
fn visit_mut_module_items(&mut self, module_items: &mut Vec<ModuleItem>) {
276286
let used_in_jsx = collect_ident_in_jsx(module_items);
277287

278288
let mut items = Vec::with_capacity(module_items.len());

‎crates/swc_ecma_transforms_react/src/refresh/tests.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1535,3 +1535,35 @@ const a = (a)=>{
15351535
}
15361536
"#
15371537
);
1538+
1539+
test!(
1540+
::swc_ecma_parser::Syntax::Es(::swc_ecma_parser::EsConfig {
1541+
jsx: true,
1542+
..Default::default()
1543+
}),
1544+
tr,
1545+
issue_6022,
1546+
r#"/* @refresh reset */
1547+
import { useState } from 'react';
1548+
1549+
function Counter() {
1550+
const [count, setCount] = useState(0);
1551+
1552+
return (
1553+
<button type="button" onClick={() => setCount(c => c + 1)}>{count}</button>
1554+
);
1555+
}
1556+
"#,
1557+
r#"var _s = $RefreshSig$();
1558+
import { useState } from 'react';
1559+
function Counter() {
1560+
_s();
1561+
const [count, setCount] = useState(0);
1562+
return <button type="button" onClick={()=>setCount((c)=>c + 1)}>{count}</button>;
1563+
}
1564+
_s(Counter, "useState{[count, setCount](0)}", true);
1565+
_c = Counter;
1566+
var _c;
1567+
$RefreshReg$(_c, "Counter");
1568+
"#
1569+
);

0 commit comments

Comments
 (0)
Please sign in to comment.