Skip to content

fix: incorrect index for placeholders in useLingui macro #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/babel-plugin-lingui-macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,22 @@ export class MacroJs {
// parent would be an Expression with this identifier which we are interesting in
const currentPath = refPath.parentPath

const _ctx = createMacroJsContext(
ctx.isLinguiIdentifier,
ctx.stripNonEssentialProps,
ctx.stripMessageProp
)

// { t } = useLingui()
// t`Hello!`
if (currentPath.isTaggedTemplateExpression()) {
const tokens = tokenizeTemplateLiteral(currentPath.node, ctx)
const tokens = tokenizeTemplateLiteral(currentPath.node, _ctx)

const descriptor = createMessageDescriptorFromTokens(
tokens,
currentPath.node.loc,
ctx.stripNonEssentialProps,
ctx.stripMessageProp
_ctx.stripNonEssentialProps,
_ctx.stripMessageProp
)

const callExpr = t.callExpression(
Expand All @@ -285,7 +291,7 @@ export class MacroJs {
const descriptor = processDescriptor(
(currentPath.get("arguments")[0] as NodePath<ObjectExpression>)
.node,
ctx
_ctx
)
const callExpr = t.callExpression(
t.identifier(uniqTIdentifier.name),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`correctly process indexed placeholders in few t calls 1`] = `
import { useLingui } from "@lingui/react/macro";
function Home() {
const { t } = useLingui();
const user = { name: "John " };
return (
<main>
<button onClick={() => console.log(t\`Hello \${user.name}\`)}>Hello</button>

<button onClick={() => console.log(t\`Bye \${user.name}\`)}>Bye</button>
</main>
);
}

↓ ↓ ↓ ↓ ↓ ↓

import { useLingui as _useLingui } from "@lingui/react";
function Home() {
const { _: _t } = _useLingui();
const user = {
name: "John ",
};
return (
<main>
<button
onClick={() =>
console.log(
_t(
/*i18n*/
{
id: "Y7riaK",
message: "Hello {0}",
values: {
0: user.name,
},
}
)
)
}
>
Hello
</button>

<button
onClick={() =>
console.log(
_t(
/*i18n*/
{
id: "vqOLZ6",
message: "Bye {0}",
values: {
0: user.name,
},
}
)
)
}
>
Bye
</button>
</main>
);
}

`;

exports[`does not crash when no params 1`] = `
import { useLingui } from "@lingui/react/macro";
function MyComponent() {
Expand Down
26 changes: 26 additions & 0 deletions packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ function MyComponent2() {
const { t } = useLingui();
const b = t\`Text\`;
}`,
},
{
name: "correctly process indexed placeholders in few t calls",
code: `
import { useLingui } from '@lingui/react/macro';

function Home() {
const {t} = useLingui();
const user = {name: 'John '}
return (
<main>
<button onClick={() =>
console.log(t\`Hello \${user.name}\`)
}>
Hello
</button>

<button onClick={() =>
console.log(t\`Bye \${user.name}\`)
}>
Bye
</button>
</main>
);
}
`,
},
{
name: "support configuring runtime module import using LinguiConfig.runtimeConfigModule",
Expand Down