From c26dc16a0e6a75d1c75f9d5f5ebd3b26c0a3e917 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Sat, 21 Dec 2024 12:21:25 +0100 Subject: [PATCH] fix: nested macros used in useLingui in arrow functions --- src/lib.rs | 8 ++++++-- src/tests/use_lingui.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7d59609..d01d5b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -366,11 +366,15 @@ impl<'a> Fold for LinguiMacroFolder { return expr; } + if let Expr::Arrow(arrow_expr) = expr { + return Expr::Arrow(self.fold_arrow_expr(arrow_expr)) + } + let mut folder = JsMacroFolder::new(&mut self.ctx); - + folder.fold_expr(expr).fold_children_with(self) } - + fn fold_call_expr(&mut self, expr: CallExpr) -> CallExpr { // If no package that we care about is imported, skip the following // transformation logic. diff --git a/src/tests/use_lingui.rs b/src/tests/use_lingui.rs index 4398330..c94efde 100644 --- a/src/tests/use_lingui.rs +++ b/src/tests/use_lingui.rs @@ -135,6 +135,39 @@ function MyComponent() { "# ); +to!( + support_nested_macro_when_in_arrow_function_issue_2095, + // input + r#" +import { plural } from '@lingui/core/macro' +import { useLingui } from '@lingui/react/macro' + +const MyComponent = () => { + const { t } = useLingui(); + const a = t`Text ${plural(users.length, { + offset: 1, + 0: "No books", + 1: "1 book", + other: "\# books" + })}`; +} + "#, + // output after transform + r#" +import { useLingui as $_useLingui } from "@lingui/react"; +const MyComponent = () => { + const { i18n: $__i18n, _: $__ } = $_useLingui(); + const a = $__i18n._({ + id: "hJRCh6", + message: "Text {0, plural, offset:1 =0 {No books} =1 {1 book} other {# books}}", + values: { + 0: users.length + } + }); +} + "# +); + to!( support_passing_t_variable_as_dependency, // input