Skip to content

Commit

Permalink
fix: correctly handle a variable passed to the t macro (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Oct 16, 2024
1 parent aaf079a commit ca458bc
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/babel-plugin-lingui-macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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<ObjectExpression>)
Expand Down Expand Up @@ -302,15 +303,15 @@ export class MacroJs {
}

private createI18nCall(
messageDescriptor: ObjectExpression,
messageDescriptor: Expression | undefined,
linguiInstance?: Expression
) {
return t.callExpression(
t.memberExpression(
linguiInstance ?? t.identifier(this.i18nImportName),
t.identifier("_")
),
[messageDescriptor]
messageDescriptor ? [messageDescriptor] : []
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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\`;
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-plugin-lingui-macro/test/js-t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 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 @@ -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();
}
`,
},
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-lingui-macro/test/jsx-plural.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { macroTester } from "./macroTester"

describe.skip("", () => {})

macroTester({
cases: [
{
Expand Down

0 comments on commit ca458bc

Please sign in to comment.