chore: attempt to apply fix (PR#2203 on lingui/js-lingui)

This commit is contained in:
izzy
2025-08-25 12:34:39 +02:00
parent 4206870bb7
commit a1aeb40396
3 changed files with 115 additions and 16 deletions
@@ -228,17 +228,17 @@ export class MacroJs {
const _property = t.isObjectPattern(varDec.id)
? varDec.id.properties.find(
(
property
): property is ObjectProperty & {
value: Identifier
key: Identifier
} =>
t.isObjectProperty(property) &&
t.isIdentifier(property.key) &&
t.isIdentifier(property.value) &&
property.key.name == "t"
)
(
property
): property is ObjectProperty & {
value: Identifier
key: Identifier
} =>
t.isObjectProperty(property) &&
t.isIdentifier(property.key) &&
t.isIdentifier(property.value) &&
property.key.name == "t"
)
: null
const newNode = t.callExpression(t.identifier(this.useLinguiImportName), [])
@@ -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(
@@ -285,7 +291,7 @@ export class MacroJs {
let descriptor = processDescriptor(
(currentPath.get("arguments")[0] as NodePath<ObjectExpression>)
.node,
ctx
_ctx
)
const callExpr = t.callExpression(
t.identifier(uniqTIdentifier.name),
@@ -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() {
@@ -1,7 +1,7 @@
import { makeConfig } from "@lingui/conf"
import { macroTester } from "./macroTester"
describe.skip("", () => {})
describe.skip("", () => { })
macroTester({
cases: [
@@ -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",