From 9e66c2949da012214bb69a2e3ed3a8f65fcd4a8c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 28 Oct 2024 02:57:55 +0000 Subject: [PATCH] refactor(transformer/react-refresh): small refactor (#6973) Follow-on after #6937. Refactor 2 small blocks of code. * Replace 2 `if` statements with 1. * Obey the mighty clippy! --- crates/oxc_transformer/src/jsx/refresh.rs | 37 ++++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/oxc_transformer/src/jsx/refresh.rs b/crates/oxc_transformer/src/jsx/refresh.rs index ce1d37df3..bfefe8e25 100644 --- a/crates/oxc_transformer/src/jsx/refresh.rs +++ b/crates/oxc_transformer/src/jsx/refresh.rs @@ -211,14 +211,17 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> { if !matches!(expr, Expression::CallExpression(_)) { // Try to get binding from parent VariableDeclarator - let id_binding = if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() { - declarator.id().get_binding_identifier().map(BoundIdentifier::from_binding_ident) - } else { - None - }; - if let Some(id_binding) = id_binding { - self.handle_function_in_variable_declarator(&id_binding, &binding, arguments, ctx); - return; + if let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() { + if let Some(ident) = declarator.id().get_binding_identifier() { + let id_binding = BoundIdentifier::from_binding_ident(ident); + self.handle_function_in_variable_declarator( + &id_binding, + &binding, + arguments, + ctx, + ); + return; + } } } @@ -813,19 +816,17 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> { ); // Get the address of the statement containing this `VariableDeclarator` - #[allow(clippy::single_match_else)] - let address = match ctx.ancestor(2) { - // For `export const Foo = () => {}` - // which is a `VariableDeclaration` inside a `Statement::ExportNamedDeclaration` - Ancestor::ExportNamedDeclarationDeclaration(export_decl) => export_decl.address(), - // Otherwise just a `const Foo = () => {}` - // which is a `Statement::VariableDeclaration` - _ => { + let address = + if let Ancestor::ExportNamedDeclarationDeclaration(export_decl) = ctx.ancestor(2) { + // For `export const Foo = () => {}` + // which is a `VariableDeclaration` inside a `Statement::ExportNamedDeclaration` + export_decl.address() + } else { + // Otherwise just a `const Foo = () => {}` which is a `Statement::VariableDeclaration` let var_decl = ctx.ancestor(1); debug_assert!(matches!(var_decl, Ancestor::VariableDeclarationDeclarations(_))); var_decl.address() - } - }; + }; self.ctx.statement_injector.insert_after(&address, statement); }