diff --git a/crates/lingui_macro/src/lib.rs b/crates/lingui_macro/src/lib.rs index 977187b..c9198a0 100644 --- a/crates/lingui_macro/src/lib.rs +++ b/crates/lingui_macro/src/lib.rs @@ -50,6 +50,21 @@ impl Fold for IdentReplacer { n } + + /// In a shorthand property `{ t }` the same identifier is both the key + /// and the value. Expand it to `{ t: $__ }` so the key is preserved. + fn fold_prop(&mut self, n: Prop) -> Prop { + if let Prop::Shorthand(ident) = &n { + if ident.to_id() == self.from { + return Prop::KeyValue(KeyValueProp { + key: PropName::Ident(IdentName::new(ident.sym.clone(), ident.span)), + value: Box::new(Expr::Ident(self.to.clone())), + }); + } + } + + n.fold_children_with(self) + } } pub struct LinguiMacroFolder diff --git a/crates/lingui_macro/tests/snapshots/use_lingui__support_passing_t_in_shorthand_object_property.snap b/crates/lingui_macro/tests/snapshots/use_lingui__support_passing_t_in_shorthand_object_property.snap new file mode 100644 index 0000000..aa30ff4 --- /dev/null +++ b/crates/lingui_macro/tests/snapshots/use_lingui__support_passing_t_in_shorthand_object_property.snap @@ -0,0 +1,19 @@ +--- +source: crates/lingui_macro/tests/use_lingui.rs +--- +import { useLingui } from '@lingui/react/macro'; + +function useGreeting(format) { + const { t } = useLingui(); + return format({ t }); +} + +↓ ↓ ↓ ↓ ↓ ↓ + +import { useLingui as $_useLingui } from "@lingui/react"; +function useGreeting(format) { + const { _: $__ } = $_useLingui(); + return format({ + t: $__ + }); +} diff --git a/crates/lingui_macro/tests/use_lingui.rs b/crates/lingui_macro/tests/use_lingui.rs index 280284f..67111c7 100644 --- a/crates/lingui_macro/tests/use_lingui.rs +++ b/crates/lingui_macro/tests/use_lingui.rs @@ -168,3 +168,15 @@ function MyComponent() { } "# ); + +to!( + support_passing_t_in_shorthand_object_property, + r#" +import { useLingui } from '@lingui/react/macro'; + +function useGreeting(format) { + const { t } = useLingui(); + return format({ t }); +} + "# +);