swc_ecma_transforms_react/refresh/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
use swc_common::{
collections::AHashSet, comments::Comments, sync::Lrc, util::take::Take, BytePos, Mark,
SourceMap, SourceMapper, Span, Spanned, SyntaxContext, DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_utils::{private_ident, quote_ident, quote_str, ExprFactory};
use swc_ecma_visit::{visit_mut_pass, Visit, VisitMut, VisitMutWith};
use self::{
hook::HookRegister,
util::{collect_ident_in_jsx, is_body_arrow_fn, is_import_or_require, make_assign_stmt},
};
pub mod options;
use options::RefreshOptions;
mod hook;
mod util;
#[cfg(test)]
mod tests;
struct Hoc {
insert: bool,
reg: Vec<(Ident, Id)>,
hook: Option<HocHook>,
}
struct HocHook {
callee: Callee,
rest_arg: Vec<ExprOrSpread>,
}
enum Persist {
Hoc(Hoc),
Component(Ident),
None,
}
fn get_persistent_id(ident: &Ident) -> Persist {
if ident.sym.starts_with(|c: char| c.is_ascii_uppercase()) {
if cfg!(debug_assertions) && ident.ctxt == SyntaxContext::empty() {
panic!("`{}` should be resolved", ident)
}
Persist::Component(ident.clone())
} else {
Persist::None
}
}
/// `react-refresh/babel`
/// https://github.com/facebook/react/blob/main/packages/react-refresh/src/ReactFreshBabelPlugin.js
pub fn refresh<C: Comments>(
dev: bool,
options: Option<RefreshOptions>,
cm: Lrc<SourceMap>,
comments: Option<C>,
global_mark: Mark,
) -> impl Pass {
visit_mut_pass(Refresh {
enable: dev && options.is_some(),
cm,
comments,
should_reset: false,
options: options.unwrap_or_default(),
global_mark,
})
}
struct Refresh<C: Comments> {
enable: bool,
options: RefreshOptions,
cm: Lrc<SourceMap>,
should_reset: bool,
comments: Option<C>,
global_mark: Mark,
}
impl<C: Comments> Refresh<C> {
fn get_persistent_id_from_var_decl(
&self,
var_decl: &mut VarDecl,
used_in_jsx: &AHashSet<Id>,
hook_reg: &mut HookRegister,
) -> Persist {
// We only handle the case when a single variable is declared
if let [VarDeclarator {
name: Pat::Ident(binding),
init: Some(init_expr),
..
}] = var_decl.decls.as_mut_slice()
{
if used_in_jsx.contains(&binding.to_id()) && !is_import_or_require(init_expr) {
match init_expr.as_ref() {
// TaggedTpl is for something like styled.div`...`
Expr::Arrow(_) | Expr::Fn(_) | Expr::TaggedTpl(_) | Expr::Call(_) => {
return Persist::Component(Ident::from(&*binding))
}
_ => (),
}
}
if let Persist::Component(persistent_id) = get_persistent_id(&Ident::from(&*binding)) {
return match init_expr.as_mut() {
Expr::Fn(_) => Persist::Component(persistent_id),
Expr::Arrow(ArrowExpr { body, .. }) => {
// Ignore complex function expressions like
// let Foo = () => () => {}
if is_body_arrow_fn(body) {
Persist::None
} else {
Persist::Component(persistent_id)
}
}
// Maybe a HOC.
Expr::Call(call_expr) => {
let res = self.get_persistent_id_from_possible_hoc(
call_expr,
vec![(private_ident!("_c"), persistent_id.to_id())],
hook_reg,
);
if let Persist::Hoc(Hoc {
insert,
reg,
hook: Some(hook),
}) = res
{
make_hook_reg(init_expr.as_mut(), hook);
Persist::Hoc(Hoc {
insert,
reg,
hook: None,
})
} else {
res
}
}
_ => Persist::None,
};
}
}
Persist::None
}
fn get_persistent_id_from_possible_hoc(
&self,
call_expr: &mut CallExpr,
mut reg: Vec<(Ident, Id)>,
hook_reg: &mut HookRegister,
) -> Persist {
let first_arg = match call_expr.args.as_mut_slice() {
[first, ..] => &mut first.expr,
_ => return Persist::None,
};
let callee = if let Callee::Expr(expr) = &call_expr.callee {
expr
} else {
return Persist::None;
};
let hoc_name = match callee.as_ref() {
Expr::Ident(fn_name) => fn_name.sym.to_string(),
// original react implement use `getSource` so we just follow them
Expr::Member(member) => self.cm.span_to_snippet(member.span).unwrap_or_default(),
_ => return Persist::None,
};
let reg_str = (
format!("{}${}", reg.last().unwrap().1 .0, &hoc_name).into(),
SyntaxContext::empty(),
);
match first_arg.as_mut() {
Expr::Call(expr) => {
let reg_ident = private_ident!("_c");
reg.push((reg_ident.clone(), reg_str));
if let Persist::Hoc(hoc) =
self.get_persistent_id_from_possible_hoc(expr, reg, hook_reg)
{
let mut first = first_arg.take();
if let Some(HocHook { callee, rest_arg }) = &hoc.hook {
let span = first.span();
let mut args = vec![first.as_arg()];
args.extend(rest_arg.clone());
first = CallExpr {
span,
callee: callee.clone(),
args,
..Default::default()
}
.into()
}
*first_arg = Box::new(make_assign_stmt(reg_ident, first));
Persist::Hoc(hoc)
} else {
Persist::None
}
}
Expr::Fn(_) | Expr::Arrow(_) => {
let reg_ident = private_ident!("_c");
let mut first = first_arg.take();
first.visit_mut_with(hook_reg);
let hook = if let Expr::Call(call) = first.as_ref() {
let res = Some(HocHook {
callee: call.callee.clone(),
rest_arg: call.args[1..].to_owned(),
});
*first_arg = Box::new(make_assign_stmt(reg_ident.clone(), first));
res
} else {
*first_arg = Box::new(make_assign_stmt(reg_ident.clone(), first));
None
};
reg.push((reg_ident, reg_str));
Persist::Hoc(Hoc {
reg,
insert: true,
hook,
})
}
// export default hoc(Foo)
// const X = hoc(Foo)
Expr::Ident(ident) => {
if let Persist::Component(_) = get_persistent_id(ident) {
Persist::Hoc(Hoc {
reg,
insert: true,
hook: None,
})
} else {
Persist::None
}
}
_ => Persist::None,
}
}
}
/// We let user do /* @refresh reset */ to reset state in the whole file.
impl<C> Visit for Refresh<C>
where
C: Comments,
{
fn visit_span(&mut self, n: &Span) {
if self.should_reset {
return;
}
let mut should_refresh = self.should_reset;
if let Some(comments) = &self.comments {
if !n.hi.is_dummy() {
comments.with_leading(n.hi - BytePos(1), |comments| {
if comments.iter().any(|c| c.text.contains("@refresh reset")) {
should_refresh = true
}
});
}
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
}
});
}
self.should_reset = should_refresh;
}
}
// TODO: figure out if we can insert all registers at once
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(&mut self, n: &mut Module) {
if !self.enable {
return;
}
// to collect comments
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());
let mut refresh_regs = Vec::<(Ident, Id)>::new();
let mut hook_visitor = HookRegister {
options: &self.options,
ident: Vec::new(),
extra_stmt: Vec::new(),
current_scope: vec![SyntaxContext::empty().apply_mark(self.global_mark)],
cm: &self.cm,
should_reset: self.should_reset,
};
for mut item in module_items.take() {
let persistent_id = match &mut item {
// function Foo() {}
ModuleItem::Stmt(Stmt::Decl(Decl::Fn(FnDecl { ident, .. }))) => {
get_persistent_id(ident)
}
// export function Foo() {}
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Fn(FnDecl { ident, .. }),
..
})) => get_persistent_id(ident),
// export default function Foo() {}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
decl:
DefaultDecl::Fn(FnExpr {
// We don't currently handle anonymous default exports.
ident: Some(ident),
..
}),
..
})) => get_persistent_id(ident),
// const Foo = () => {}
// export const Foo = () => {}
ModuleItem::Stmt(Stmt::Decl(Decl::Var(var_decl)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Var(var_decl),
..
})) => {
self.get_persistent_id_from_var_decl(var_decl, &used_in_jsx, &mut hook_visitor)
}
// This code path handles nested cases like:
// export default memo(() => {})
// In those cases it is more plausible people will omit names
// so they're worth handling despite possible false positives.
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr,
span,
})) => {
if let Expr::Call(call) = expr.as_mut() {
if let Persist::Hoc(Hoc { reg, hook, .. }) = self
.get_persistent_id_from_possible_hoc(
call,
vec![(
private_ident!("_c"),
("%default%".into(), SyntaxContext::empty()),
)],
&mut hook_visitor,
)
{
if let Some(hook) = hook {
make_hook_reg(expr.as_mut(), hook)
}
item = ExportDefaultExpr {
expr: Box::new(make_assign_stmt(reg[0].0.clone(), expr.take())),
span: *span,
}
.into();
Persist::Hoc(Hoc {
insert: false,
reg,
hook: None,
})
} else {
Persist::None
}
} else {
Persist::None
}
}
_ => Persist::None,
};
if let Persist::Hoc(_) = persistent_id {
// we need to make hook transform happens after component for
// HOC
items.push(item);
} else {
item.visit_mut_children_with(&mut hook_visitor);
items.push(item);
items.extend(
hook_visitor
.extra_stmt
.take()
.into_iter()
.map(ModuleItem::Stmt),
);
}
match persistent_id {
Persist::None => (),
Persist::Component(persistent_id) => {
let registration_handle = private_ident!("_c");
refresh_regs.push((registration_handle.clone(), persistent_id.to_id()));
items.push(
ExprStmt {
span: DUMMY_SP,
expr: Box::new(make_assign_stmt(
registration_handle,
persistent_id.into(),
)),
}
.into(),
);
}
Persist::Hoc(mut hoc) => {
hoc.reg = hoc.reg.into_iter().rev().collect();
if hoc.insert {
let (ident, name) = hoc.reg.last().unwrap();
items.push(
ExprStmt {
span: DUMMY_SP,
expr: Box::new(make_assign_stmt(
ident.clone(),
Ident::new(name.0.clone(), DUMMY_SP, name.1).into(),
)),
}
.into(),
)
}
refresh_regs.append(&mut hoc.reg);
}
}
}
if !hook_visitor.ident.is_empty() {
items.insert(0, hook_visitor.gen_hook_handle().into());
}
// Insert
// ```
// var _c, _c1;
// ```
if !refresh_regs.is_empty() {
items.push(
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: refresh_regs
.iter()
.map(|(handle, _)| VarDeclarator {
span: DUMMY_SP,
name: handle.clone().into(),
init: None,
definite: false,
})
.collect(),
..Default::default()
}
.into(),
);
}
// Insert
// ```
// $RefreshReg$(_c, "Hello");
// $RefreshReg$(_c1, "Foo");
// ```
let refresh_reg = self.options.refresh_reg.as_str();
for (handle, persistent_id) in refresh_regs {
items.push(
ExprStmt {
span: DUMMY_SP,
expr: CallExpr {
callee: quote_ident!(refresh_reg).as_callee(),
args: vec![handle.as_arg(), quote_str!(persistent_id.0).as_arg()],
..Default::default()
}
.into(),
}
.into(),
);
}
*module_items = items
}
fn visit_mut_ts_module_decl(&mut self, _: &mut TsModuleDecl) {}
}
fn make_hook_reg(expr: &mut Expr, mut hook: HocHook) {
let span = expr.span();
let mut args = vec![expr.take().as_arg()];
args.append(&mut hook.rest_arg);
*expr = CallExpr {
span,
callee: hook.callee,
args,
..Default::default()
}
.into();
}