Skip to content

Commit 6688f72

Browse files
fix: incorrect index for placeholders in useLingui macro (#2204)
1 parent d36c323 commit 6688f72

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

packages/babel-plugin-lingui-macro/src/macroJs.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,22 @@ export class MacroJs {
256256
// parent would be an Expression with this identifier which we are interesting in
257257
const currentPath = refPath.parentPath
258258

259+
const _ctx = createMacroJsContext(
260+
ctx.isLinguiIdentifier,
261+
ctx.stripNonEssentialProps,
262+
ctx.stripMessageProp
263+
)
264+
259265
// { t } = useLingui()
260266
// t`Hello!`
261267
if (currentPath.isTaggedTemplateExpression()) {
262-
const tokens = tokenizeTemplateLiteral(currentPath.node, ctx)
268+
const tokens = tokenizeTemplateLiteral(currentPath.node, _ctx)
263269

264270
const descriptor = createMessageDescriptorFromTokens(
265271
tokens,
266272
currentPath.node.loc,
267-
ctx.stripNonEssentialProps,
268-
ctx.stripMessageProp
273+
_ctx.stripNonEssentialProps,
274+
_ctx.stripMessageProp
269275
)
270276

271277
const callExpr = t.callExpression(
@@ -285,7 +291,7 @@ export class MacroJs {
285291
const descriptor = processDescriptor(
286292
(currentPath.get("arguments")[0] as NodePath<ObjectExpression>)
287293
.node,
288-
ctx
294+
_ctx
289295
)
290296
const callExpr = t.callExpression(
291297
t.identifier(uniqTIdentifier.name),

packages/babel-plugin-lingui-macro/test/__snapshots__/js-useLingui.test.ts.snap

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`correctly process indexed placeholders in few t calls 1`] = `
4+
import { useLingui } from "@lingui/react/macro";
5+
function Home() {
6+
const { t } = useLingui();
7+
const user = { name: "John " };
8+
return (
9+
<main>
10+
<button onClick={() => console.log(t\`Hello \${user.name}\`)}>Hello</button>
11+
12+
<button onClick={() => console.log(t\`Bye \${user.name}\`)}>Bye</button>
13+
</main>
14+
);
15+
}
16+
17+
↓ ↓ ↓ ↓ ↓ ↓
18+
19+
import { useLingui as _useLingui } from "@lingui/react";
20+
function Home() {
21+
const { _: _t } = _useLingui();
22+
const user = {
23+
name: "John ",
24+
};
25+
return (
26+
<main>
27+
<button
28+
onClick={() =>
29+
console.log(
30+
_t(
31+
/*i18n*/
32+
{
33+
id: "Y7riaK",
34+
message: "Hello {0}",
35+
values: {
36+
0: user.name,
37+
},
38+
}
39+
)
40+
)
41+
}
42+
>
43+
Hello
44+
</button>
45+
46+
<button
47+
onClick={() =>
48+
console.log(
49+
_t(
50+
/*i18n*/
51+
{
52+
id: "vqOLZ6",
53+
message: "Bye {0}",
54+
values: {
55+
0: user.name,
56+
},
57+
}
58+
)
59+
)
60+
}
61+
>
62+
Bye
63+
</button>
64+
</main>
65+
);
66+
}
67+
68+
`;
69+
370
exports[`does not crash when no params 1`] = `
471
import { useLingui } from "@lingui/react/macro";
572
function MyComponent() {

packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,32 @@ function MyComponent2() {
201201
const { t } = useLingui();
202202
const b = t\`Text\`;
203203
}`,
204+
},
205+
{
206+
name: "correctly process indexed placeholders in few t calls",
207+
code: `
208+
import { useLingui } from '@lingui/react/macro';
209+
210+
function Home() {
211+
const {t} = useLingui();
212+
const user = {name: 'John '}
213+
return (
214+
<main>
215+
<button onClick={() =>
216+
console.log(t\`Hello \${user.name}\`)
217+
}>
218+
Hello
219+
</button>
220+
221+
<button onClick={() =>
222+
console.log(t\`Bye \${user.name}\`)
223+
}>
224+
Bye
225+
</button>
226+
</main>
227+
);
228+
}
229+
`,
204230
},
205231
{
206232
name: "support configuring runtime module import using LinguiConfig.runtimeConfigModule",

0 commit comments

Comments
 (0)