From e8976c418bd789fe7f976c7c44a97666bce2d639 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Tue, 18 Mar 2025 13:16:28 +0100 Subject: [PATCH] fix: incorrect index for placeholders in useLingui macro --- .../babel-plugin-lingui-macro/src/macroJs.ts | 14 ++-- .../__snapshots__/js-useLingui.test.ts.snap | 67 +++++++++++++++++++ .../test/js-useLingui.test.ts | 26 +++++++ 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-lingui-macro/src/macroJs.ts b/packages/babel-plugin-lingui-macro/src/macroJs.ts index 3fceaebb1..4256445ce 100644 --- a/packages/babel-plugin-lingui-macro/src/macroJs.ts +++ b/packages/babel-plugin-lingui-macro/src/macroJs.ts @@ -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 { const 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 6fb9cb82c..d64326c60 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,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 ( +
+ + + +
+ ); +} + +↓ ↓ ↓ ↓ ↓ ↓ + +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 6e96764ea..ee0b017b3 100644 --- a/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts +++ b/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts @@ -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 ( +
+ + + +
+ ); +} +`, }, { name: "support configuring runtime module import using LinguiConfig.runtimeConfigModule",