Skip to content

Commit 67d255c

Browse files
committed
refactor(transformer/react-refresh): calculate signature key once
1 parent 1314c97 commit 67d255c

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

crates/oxc_transformer/src/jsx/refresh.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use base64::prelude::{Engine, BASE64_STANDARD};
2+
use compact_str::{CompactString, CompactStringExt};
23
use rustc_hash::FxHashMap;
34
use sha1::{Digest, Sha1};
45

@@ -111,7 +112,7 @@ pub struct ReactRefresh<'a, 'ctx> {
111112
/// (eg: hoc(() => {}) -> _s1(hoc(_s1(() => {}))))
112113
last_signature: Option<(BindingIdentifier<'a>, ArenaVec<'a, Argument<'a>>)>,
113114
// (function_scope_id, (hook_name, hook_key, custom_hook_callee)
114-
hook_calls: FxHashMap<ScopeId, Vec<(Atom<'a>, Atom<'a>)>>,
115+
hook_calls: FxHashMap<ScopeId, Vec<CompactString>>,
115116
non_builtin_hooks_callee: FxHashMap<ScopeId, Vec<Option<Expression<'a>>>>,
116117
}
117118

@@ -362,7 +363,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
362363
}
363364
}
364365

365-
let key = if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() {
366+
let declarator_id = if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() {
366367
// TODO: if there is no LHS, consider some other heuristic.
367368
declarator.id().span().source_text(self.ctx.source_text)
368369
} else {
@@ -377,15 +378,19 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
377378
} else {
378379
""
379380
};
380-
381-
let key = format!(
382-
"{}{}{args_key}{}",
383-
key,
384-
if args_key.is_empty() { "" } else { "(" },
385-
if args_key.is_empty() { "" } else { ")" }
386-
);
387-
388-
self.hook_calls.entry(current_scope_id).or_default().push((hook_name, ctx.ast.atom(&key)));
381+
let is_empty = args_key.is_empty();
382+
// `hook_name{{declarator_id(args_key)}}` or `hook_name{{declarator_id}}`
383+
let key = [
384+
hook_name.as_str(),
385+
"{",
386+
declarator_id,
387+
if is_empty { "" } else { "(" },
388+
args_key,
389+
if is_empty { "" } else { ")" },
390+
"}",
391+
]
392+
.concat_compact();
393+
self.hook_calls.entry(current_scope_id).or_default().push(key);
389394
}
390395
}
391396

@@ -512,12 +517,7 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
512517
) -> Option<(BindingIdentifier<'a>, ArenaVec<'a, Argument<'a>>)> {
513518
let fn_hook_calls = self.hook_calls.remove(&scope_id)?;
514519

515-
let mut key = fn_hook_calls
516-
.into_iter()
517-
.map(|(hook_name, hook_key)| format!("{hook_name}{{{hook_key}}}"))
518-
.collect::<Vec<_>>()
519-
.join("\\n");
520-
520+
let mut key = fn_hook_calls.join("\\n");
521521
if !self.emit_full_signatures {
522522
// Prefer to hash when we can (e.g. outside of ASTExplorer).
523523
// This makes it deterministically compact, even if there's

0 commit comments

Comments
 (0)