Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/babel-plugin-lingui-macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -285,7 +292,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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<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() {
Expand Down
25 changes: 25 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 @@ -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 (
<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",
macroOpts: {
Expand Down