Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/transform_react): should force refresh if code contains @refresh reset #6749

Merged
merged 1 commit into from Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions crates/swc_ecma_transforms_react/src/refresh/mod.rs
Expand Up @@ -249,6 +249,12 @@ where
});
}

comments.with_leading(n.lo, |comments| {
if comments.iter().any(|c| c.text.contains("@refresh reset")) {
should_refresh = true
}
});

comments.with_trailing(n.lo, |comments| {
if comments.iter().any(|c| c.text.contains("@refresh reset")) {
should_refresh = true
Expand All @@ -265,14 +271,18 @@ impl<C: Comments> VisitMut for Refresh<C> {
// Does anyone write react without esmodule?
// fn visit_mut_script(&mut self, _: &mut Script) {}

fn visit_mut_module_items(&mut self, module_items: &mut Vec<ModuleItem>) {
fn visit_mut_module(&mut self, n: &mut Module) {
if !self.enable {
return;
}

// to collect comments
self.visit_module_items(module_items);
self.visit_module(n);

self.visit_mut_module_items(&mut n.body);
}

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

let mut items = Vec::with_capacity(module_items.len());
Expand Down
32 changes: 32 additions & 0 deletions crates/swc_ecma_transforms_react/src/refresh/tests.rs
Expand Up @@ -1535,3 +1535,35 @@ const a = (a)=>{
}
"#
);

test!(
::swc_ecma_parser::Syntax::Es(::swc_ecma_parser::EsConfig {
jsx: true,
..Default::default()
}),
tr,
issue_6022,
r#"/* @refresh reset */
import { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

return (
<button type="button" onClick={() => setCount(c => c + 1)}>{count}</button>
);
}
"#,
r#"var _s = $RefreshSig$();
import { useState } from 'react';
function Counter() {
_s();
const [count, setCount] = useState(0);
return <button type="button" onClick={()=>setCount((c)=>c + 1)}>{count}</button>;
}
_s(Counter, "useState{[count, setCount](0)}", true);
_c = Counter;
var _c;
$RefreshReg$(_c, "Counter");
"#
);