Skip to content

Commit

Permalink
fix(macro): fix failure when macro meets function carrying (#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored and andrii-bodnar committed Mar 18, 2024
1 parent 10e6119 commit 08032b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class MacroJs {
// t(i18nInstance)`Message` -> i18nInstance._(messageDescriptor)
if (
tag.isCallExpression() &&
tag.get("arguments")[0].isExpression() &&
tag.get("arguments")[0]?.isExpression() &&
this.isLinguiIdentifier(tag.get("callee"), JsMacroName.t)
) {
// Use the first argument as i18n instance instead of the default i18n instance
Expand All @@ -133,7 +133,7 @@ export default class MacroJs {

if (
callee.isCallExpression() &&
callee.get("arguments")[0].isExpression() &&
callee.get("arguments")[0]?.isExpression() &&
this.isLinguiIdentifier(callee.get("callee"), JsMacroName.t)
) {
const i18nInstance = callee.node.arguments[0] as Expression
Expand Down
6 changes: 5 additions & 1 deletion packages/macro/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ function getConfig(_config?: LinguiConfigNormalized) {
}

function reportUnsupportedSyntax(path: NodePath, e: Error) {
throw path.buildCodeFrameError(
const codeFrameError = path.buildCodeFrameError(
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
If you think this is a bug, fill in an issue at https://github.com/lingui/js-lingui/issues
Error: ${e.message}`
)

// show stack trace where error originally happened
codeFrameError.stack = e.stack
throw codeFrameError
}

type LinguiSymbol = "Trans" | "useLingui" | "i18n"
Expand Down
13 changes: 13 additions & 0 deletions packages/macro/test/js-useLingui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ function MyComponent() {
}
);
}
`,
},
{
name: "should not break on function currying",
input: `
import { useLingui } from '@lingui/macro';
const result = curryingFoo()()
console.log('curryingFoo', result)
`,
expected: `
const result = curryingFoo()()
console.log('curryingFoo', result)
`,
},
{
Expand Down

0 comments on commit 08032b8

Please sign in to comment.