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

refactor(transformer/react-refresh): calculate signature key once #7970

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use base64::prelude::{Engine, BASE64_STANDARD};
use compact_str::{CompactString, CompactStringExt};
use rustc_hash::FxHashMap;
use sha1::{Digest, Sha1};

Expand Down Expand Up @@ -111,7 +112,7 @@ pub struct ReactRefresh<'a, 'ctx> {
/// (eg: hoc(() => {}) -> _s1(hoc(_s1(() => {}))))
last_signature: Option<(BindingIdentifier<'a>, ArenaVec<'a, Argument<'a>>)>,
// (function_scope_id, (hook_name, hook_key, custom_hook_callee)
hook_calls: FxHashMap<ScopeId, Vec<(Atom<'a>, Atom<'a>)>>,
hook_calls: FxHashMap<ScopeId, Vec<CompactString>>,
non_builtin_hooks_callee: FxHashMap<ScopeId, Vec<Option<Expression<'a>>>>,
}

Expand Down Expand Up @@ -362,7 +363,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
}
}

let key = if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() {
let declarator_id = if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() {
// TODO: if there is no LHS, consider some other heuristic.
declarator.id().span().source_text(self.ctx.source_text)
} else {
Expand All @@ -377,15 +378,19 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
} else {
""
};

let key = format!(
"{}{}{args_key}{}",
key,
if args_key.is_empty() { "" } else { "(" },
if args_key.is_empty() { "" } else { ")" }
);

self.hook_calls.entry(current_scope_id).or_default().push((hook_name, ctx.ast.atom(&key)));
let is_empty = args_key.is_empty();
// `hook_name{{declarator_id(args_key)}}` or `hook_name{{declarator_id}}`
let key = [
hook_name.as_str(),
"{",
declarator_id,
if is_empty { "" } else { "(" },
args_key,
if is_empty { "" } else { ")" },
"}",
]
.concat_compact();
self.hook_calls.entry(current_scope_id).or_default().push(key);
}
}

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

let mut key = fn_hook_calls
.into_iter()
.map(|(hook_name, hook_key)| format!("{hook_name}{{{hook_key}}}"))
.collect::<Vec<_>>()
.join("\\n");

let mut key = fn_hook_calls.join("\\n");
if !self.emit_full_signatures {
// Prefer to hash when we can (e.g. outside of ASTExplorer).
// This makes it deterministically compact, even if there's
Expand Down
Loading