From 0e7f335ba994a957d11904ae3dec6ce036c4195d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Tue, 27 Aug 2024 12:21:27 +0200 Subject: [PATCH] Fix tr macros not extracted at returns in translations checker (#237) --- CHANGELOG.md | 6 +++ Cargo.lock | 4 +- leptos-fluent-macros/Cargo.toml | 2 +- .../src/translations_checker/tr_macros.rs | 44 ++++++++++++++----- leptos-fluent/Cargo.toml | 2 +- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f40098..1da58f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 2024-08-27 - [0.1.21] + +- Fix translation macros not extracted from some locations when a variable + binding is not used in translations checker. + ## 2024-08-26 - [0.1.20] ### Bug fixes @@ -500,6 +505,7 @@ version to `0.1` during installation. - Added all ISO-639-1 and ISO-639-2 languages. +[0.1.21]: https://github.com/mondeja/leptos-fluent/compare/v0.1.20...v0.1.21 [0.1.20]: https://github.com/mondeja/leptos-fluent/compare/v0.1.19...v0.1.20 [0.1.19]: https://github.com/mondeja/leptos-fluent/compare/v0.1.18...v0.1.19 [0.1.18]: https://github.com/mondeja/leptos-fluent/compare/v0.1.17...v0.1.18 diff --git a/Cargo.lock b/Cargo.lock index a2cc1afa..a57ed92f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1794,7 +1794,7 @@ dependencies = [ [[package]] name = "leptos-fluent" -version = "0.1.20" +version = "0.1.21" dependencies = [ "current_locale", "directories", @@ -1857,7 +1857,7 @@ dependencies = [ [[package]] name = "leptos-fluent-macros" -version = "0.1.20" +version = "0.1.21" dependencies = [ "cfg-expr 0.15.8", "current_platform", diff --git a/leptos-fluent-macros/Cargo.toml b/leptos-fluent-macros/Cargo.toml index 8ff5a242..a6ce4ac6 100644 --- a/leptos-fluent-macros/Cargo.toml +++ b/leptos-fluent-macros/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent-macros" description = "Macros for leptos-fluent" edition.workspace = true -version = "0.1.20" +version = "0.1.21" license = "MIT" documentation.workspace = true repository.workspace = true diff --git a/leptos-fluent-macros/src/translations_checker/tr_macros.rs b/leptos-fluent-macros/src/translations_checker/tr_macros.rs index a24e3eee..9010b115 100644 --- a/leptos-fluent-macros/src/translations_checker/tr_macros.rs +++ b/leptos-fluent-macros/src/translations_checker/tr_macros.rs @@ -38,7 +38,7 @@ pub(crate) fn gather_tr_macro_defs_from_rs_files( (tr_macros, errors) } Err(error) => ( - vec![], + Vec::new(), vec![format!( r#"Error parsing glob pattern "{}": {}"#, glob_pattern, error, @@ -302,19 +302,12 @@ impl<'ast> TranslationsMacrosVisitor { impl<'ast> Visit<'ast> for TranslationsMacrosVisitor { fn visit_macro(&mut self, node: &'ast syn::Macro) { - for token in node.tokens.clone() { - if let proc_macro2::TokenTree::Group(group) = token { - self.visit_maybe_macro_tokens_stream(&group.stream()); - } - } - + self.visit_maybe_macro_tokens_stream(&node.to_token_stream()); syn::visit::visit_macro(self, node); } fn visit_stmt_macro(&mut self, node: &'ast syn::StmtMacro) { - let stream = node.to_token_stream(); - self.visit_maybe_macro_tokens_stream(&stream); - + self.visit_maybe_macro_tokens_stream(&node.to_token_stream()); syn::visit::visit_stmt_macro(self, node); } @@ -570,7 +563,7 @@ mod tests { let tr_macros = tr_macros_from_file_content(&content.to_string()); - assert_eq!(tr_macros, vec![]); + assert_eq!(tr_macros, Vec::new()); } #[test] @@ -620,4 +613,33 @@ mod tests { ] ); } + + #[test] + fn at_return() { + let content = quote! { + pub fn foo() -> String { + tr!("now") + } + + pub fn bar() -> String { + return move_tr!("after"); + } + + fn baz() -> String { + (tr!("before"), move_tr!("tomorrow")) + } + }; + + let tr_macros = tr_macros_from_file_content(&content.to_string()); + + assert_eq!( + tr_macros, + vec![ + tr_macro!("tr", "now", Vec::new()), + tr_macro!("move_tr", "after", Vec::new()), + tr_macro!("tr", "before", Vec::new()), + tr_macro!("move_tr", "tomorrow", Vec::new()), + ] + ); + } } diff --git a/leptos-fluent/Cargo.toml b/leptos-fluent/Cargo.toml index 2ff38c4b..f4eb5a47 100644 --- a/leptos-fluent/Cargo.toml +++ b/leptos-fluent/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent" description = "Fluent framework for internationalization of Leptos applications" edition.workspace = true -version = "0.1.20" +version = "0.1.21" license = "MIT" documentation.workspace = true repository.workspace = true