From ca458bc14ed5a1dd0f7eca37f814639eedbd5bf0 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Wed, 16 Oct 2024 12:45:25 +0200 Subject: [PATCH] fix: correctly handle a variable passed to the `t` macro (#2048) --- .../babel-plugin-lingui-macro/src/macroJs.ts | 17 +++++----- .../test/__snapshots__/js-t.test.ts.snap | 22 ++++++++++++ .../__snapshots__/js-useLingui.test.ts.snap | 34 +++++++++++++++++++ .../test/js-t.test.ts | 14 ++++++++ .../test/js-useLingui.test.ts | 22 ++++++++++++ .../test/jsx-plural.test.ts | 2 ++ 6 files changed, 103 insertions(+), 8 deletions(-) diff --git a/packages/babel-plugin-lingui-macro/src/macroJs.ts b/packages/babel-plugin-lingui-macro/src/macroJs.ts index 302494ec8..a114c277e 100644 --- a/packages/babel-plugin-lingui-macro/src/macroJs.ts +++ b/packages/babel-plugin-lingui-macro/src/macroJs.ts @@ -165,12 +165,13 @@ export class MacroJs { ctx: MacroJsContext, linguiInstance?: babelTypes.Expression ): babelTypes.CallExpression => { - const descriptor = processDescriptor( - node.arguments[0] as ObjectExpression, - ctx - ) + let arg: Expression = node.arguments[0] as Expression + + if (t.isObjectExpression(arg)) { + arg = processDescriptor(arg, ctx) + } - return this.createI18nCall(descriptor, linguiInstance) + return this.createI18nCall(arg, linguiInstance) } /** @@ -274,7 +275,7 @@ export class MacroJs { // t(messageDescriptor) if ( currentPath.isCallExpression() && - currentPath.get("arguments")[0].isObjectExpression() + currentPath.get("arguments")[0]?.isObjectExpression() ) { let descriptor = processDescriptor( (currentPath.get("arguments")[0] as NodePath) @@ -302,7 +303,7 @@ export class MacroJs { } private createI18nCall( - messageDescriptor: ObjectExpression, + messageDescriptor: Expression | undefined, linguiInstance?: Expression ) { return t.callExpression( @@ -310,7 +311,7 @@ export class MacroJs { linguiInstance ?? t.identifier(this.i18nImportName), t.identifier("_") ), - [messageDescriptor] + messageDescriptor ? [messageDescriptor] : [] ) } } diff --git a/packages/babel-plugin-lingui-macro/test/__snapshots__/js-t.test.ts.snap b/packages/babel-plugin-lingui-macro/test/__snapshots__/js-t.test.ts.snap index 5a0781f59..ee22a3056 100644 --- a/packages/babel-plugin-lingui-macro/test/__snapshots__/js-t.test.ts.snap +++ b/packages/babel-plugin-lingui-macro/test/__snapshots__/js-t.test.ts.snap @@ -271,6 +271,17 @@ _i18n._( `; +exports[`Should not crash when a variable passed 1`] = ` +import { t } from "@lingui/core/macro"; +const msg = t(msg); + +↓ ↓ ↓ ↓ ↓ ↓ + +import { i18n as _i18n } from "@lingui/core"; +const msg = _i18n._(msg); + +`; + exports[`Support id and comment in t macro as callExpression 1`] = ` import { t, plural } from "@lingui/core/macro"; const msg = t({ @@ -548,6 +559,17 @@ _i18n._( `; +exports[`should not crash when no params passed 1`] = ` +import { t } from "@lingui/core/macro"; +const msg = t(); + +↓ ↓ ↓ ↓ ↓ ↓ + +import { i18n as _i18n } from "@lingui/core"; +const msg = _i18n._(); + +`; + exports[`t\`\` macro could be renamed 1`] = ` import { t as t2 } from "@lingui/core/macro"; const a = t2\`Expression assignment\`; 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 3574184a1..6fb9cb82c 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,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`does not crash when no params 1`] = ` +import { useLingui } from "@lingui/react/macro"; +function MyComponent() { + const { t } = useLingui(); + const a = t(); +} + +↓ ↓ ↓ ↓ ↓ ↓ + +import { useLingui as _useLingui } from "@lingui/react"; +function MyComponent() { + const { _: _t } = _useLingui(); + const a = _t(); +} + +`; + exports[`inserted statement should not clash with existing variables 1`] = ` import { useLingui } from "@lingui/react/macro"; function MyComponent() { @@ -85,6 +102,23 @@ function MyComponent() { `; +exports[`support a variable 1`] = ` +import { useLingui } from "@lingui/react/macro"; +function MyComponent() { + const { t } = useLingui(); + const a = t(msg); +} + +↓ ↓ ↓ ↓ ↓ ↓ + +import { useLingui as _useLingui } from "@lingui/react"; +function MyComponent() { + const { _: _t } = _useLingui(); + const a = _t(msg); +} + +`; + exports[`support configuring runtime module import using LinguiConfig.runtimeConfigModule 1`] = ` import { useLingui } from "@lingui/react/macro"; function MyComponent() { diff --git a/packages/babel-plugin-lingui-macro/test/js-t.test.ts b/packages/babel-plugin-lingui-macro/test/js-t.test.ts index 131b971c7..b11afc7cc 100644 --- a/packages/babel-plugin-lingui-macro/test/js-t.test.ts +++ b/packages/babel-plugin-lingui-macro/test/js-t.test.ts @@ -142,6 +142,20 @@ macroTester({ const msg = t({ id: \`msgId\` }) `, }, + { + name: "Should not crash when a variable passed", + code: ` + import { t } from '@lingui/core/macro' + const msg = t(msg) + `, + }, + { + name: "should not crash when no params passed", + code: ` + import { t } from '@lingui/core/macro' + const msg = t() + `, + }, { name: "Production - only essential props are kept", production: true, 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 4615fe428..6e96764ea 100644 --- a/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts +++ b/packages/babel-plugin-lingui-macro/test/js-useLingui.test.ts @@ -86,6 +86,28 @@ import { useLingui } from '@lingui/react/macro'; function MyComponent() { const { t } = useLingui(); const a = t({ message: "Hello", context: "my custom" }); +} + `, + }, + { + name: "support a variable", + code: ` +import { useLingui } from '@lingui/react/macro'; + +function MyComponent() { + const { t } = useLingui(); + const a = t(msg); +} + `, + }, + { + name: "does not crash when no params", + code: ` +import { useLingui } from '@lingui/react/macro'; + +function MyComponent() { + const { t } = useLingui(); + const a = t(); } `, }, diff --git a/packages/babel-plugin-lingui-macro/test/jsx-plural.test.ts b/packages/babel-plugin-lingui-macro/test/jsx-plural.test.ts index ce53b1515..e01faeb0d 100644 --- a/packages/babel-plugin-lingui-macro/test/jsx-plural.test.ts +++ b/packages/babel-plugin-lingui-macro/test/jsx-plural.test.ts @@ -1,5 +1,7 @@ import { macroTester } from "./macroTester" +describe.skip("", () => {}) + macroTester({ cases: [ {