Skip to content

Commit 107fce3

Browse files
fix: keep the key when t from useLingui is used as a shorthand property (#281)
1 parent 32bb27a commit 107fce3

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎crates/lingui_macro/src/lib.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ impl Fold for IdentReplacer {
5050

5151
n
5252
}
53+
54+
/// In a shorthand property `{ t }` the same identifier is both the key
55+
/// and the value. Expand it to `{ t: $__ }` so the key is preserved.
56+
fn fold_prop(&mut self, n: Prop) -> Prop {
57+
if let Prop::Shorthand(ident) = &n {
58+
if ident.to_id() == self.from {
59+
return Prop::KeyValue(KeyValueProp {
60+
key: PropName::Ident(IdentName::new(ident.sym.clone(), ident.span)),
61+
value: Box::new(Expr::Ident(self.to.clone())),
62+
});
63+
}
64+
}
65+
66+
n.fold_children_with(self)
67+
}
5368
}
5469

5570
pub struct LinguiMacroFolder<C>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: crates/lingui_macro/tests/use_lingui.rs
3+
---
4+
import { useLingui } from '@lingui/react/macro';
5+
6+
function useGreeting(format) {
7+
const { t } = useLingui();
8+
return format({ t });
9+
}
10+
11+
↓ ↓ ↓ ↓ ↓ ↓
12+
13+
import { useLingui as $_useLingui } from "@lingui/react";
14+
function useGreeting(format) {
15+
const { _: $__ } = $_useLingui();
16+
return format({
17+
t: $__
18+
});
19+
}

‎crates/lingui_macro/tests/use_lingui.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,15 @@ function MyComponent() {
168168
}
169169
"#
170170
);
171+
172+
to!(
173+
support_passing_t_in_shorthand_object_property,
174+
r#"
175+
import { useLingui } from '@lingui/react/macro';
176+
177+
function useGreeting(format) {
178+
const { t } = useLingui();
179+
return format({ t });
180+
}
181+
"#
182+
);

0 commit comments

Comments
 (0)