From f0dd19a48fe01262d5361e4c939131373f3c15ff Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 19 Oct 2024 19:50:11 +0000 Subject: [PATCH] refactor(ast_tools): shorten code (#6678) Follow-on after #6404. Pure refactor. Shorten code for deriving `ESTree` by reducing repetition. --- tasks/ast_tools/src/derives/estree.rs | 29 ++++++++------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/tasks/ast_tools/src/derives/estree.rs b/tasks/ast_tools/src/derives/estree.rs index 4ff6e6291c84b..b0ea25186d639 100644 --- a/tasks/ast_tools/src/derives/estree.rs +++ b/tasks/ast_tools/src/derives/estree.rs @@ -48,30 +48,17 @@ impl Derive for DeriveESTree { }; let ident = def.ident(); - if def.has_lifetime() { - quote! { - impl<'a> Serialize for #ident<'a> { - #[allow(clippy::match_same_arms, unused_mut)] - fn serialize(&self, serializer: S) -> Result - where S: Serializer, - { - #body - } - } - #ts_type_def - } - } else { - quote! { - impl Serialize for #ident { - #[allow(clippy::match_same_arms, unused_mut)] - fn serialize(&self, serializer: S) -> Result + let lifetime = if def.has_lifetime() { quote!(<'a>) } else { TokenStream::new() }; + quote! { + impl #lifetime Serialize for #ident #lifetime { + #[allow(clippy::match_same_arms, unused_mut)] + fn serialize(&self, serializer: S) -> Result where S: Serializer, - { - #body - } + { + #body } - #ts_type_def } + #ts_type_def } }