From ee4f4692cb40f305ead57af1b594e969ddc458a0 Mon Sep 17 00:00:00 2001 From: SupertigerDev Date: Fri, 10 Oct 2025 21:47:06 +0100 Subject: [PATCH] fix: incorrect index for placeholders in useLingui macro --- .../babel-plugin-lingui-macro/src/macroJs.ts | 15 +++-- .../__snapshots__/js-useLingui.test.ts.snap | 65 +++++++++++++++++++ .../test/js-useLingui.test.ts | 25 +++++++ 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-lingui-macro/src/macroJs.ts b/packages/babel-plugin-lingui-macro/src/macroJs.ts index 3d35d98..59f18f2 100644 --- a/packages/babel-plugin-lingui-macro/src/macroJs.ts +++ b/packages/babel-plugin-lingui-macro/src/macroJs.ts @@ -256,16 +256,23 @@ 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 +292,7 @@ export class MacroJs { let descriptor = processDescriptor( (currentPath.get("arguments")[0] as NodePath) .node, - ctx + _ctx ) const callExpr = t.callExpression( t.identifier(uniqTIdentifier.name), diff --git a/packages/babel-plugin-lingui-macro/test/__snapshots__/js-useLingui.test.ts.snap b/packages/babel-plugin-lingui-macro/test/__snapshots__/js-useLingui.test.ts.snap index 6fb9cb8..dd288d0 100644 --- a/packages/babel-plugin-lingui-macro/test/__snapshots__/js-useLingui.test.ts.snap +++ b/packages/babel-plugin-lingui-macro/test/__snapshots__/js-useLingui.test.ts.snap @@ -1,5 +1,70 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`correctly process indexed placeholders in few t calls 1`] = ` +import { useLingui } from "@lingui-solid/solid/macro"; +function Home() { + const { t } = useLingui(); + const user = { name: "John " }; + return ( +
+ + +
+ ); +} + +↓ ↓ ↓ ↓ ↓ ↓ + +import { useLingui as _useLingui } from "@lingui/react"; +function Home() { + const { _: _t } = _useLingui(); + const user = { + name: "John ", + }; + return ( +
+ + +
+ ); +} + +`; + exports[`does not crash when no params 1`] = ` import { useLingui } from "@lingui/react/macro"; function MyComponent() { diff --git a/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts b/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts index 6e96764..4ceb2fd 100644 --- a/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts +++ b/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts @@ -202,6 +202,31 @@ function MyComponent2() { const b = t\`Text\`; }`, }, + { + name: "correctly process indexed placeholders in few t calls", + code: ` +import { useLingui } from '@lingui-solid/solid/macro'; +function Home() { + const {t} = useLingui(); + const user = {name: 'John '} + return ( +
+ + +
+ ); +} +`, + }, + { name: "support configuring runtime module import using LinguiConfig.runtimeConfigModule", macroOpts: {