From 08032b869660b445428523170bc1a683ef4de692 Mon Sep 17 00:00:00 2001 From: Timofei Iatsenko Date: Mon, 11 Mar 2024 10:49:51 +0100 Subject: [PATCH] fix(macro): fix failure when macro meets function carrying (#1884) --- packages/macro/src/macroJs.ts | 4 ++-- packages/macro/src/plugin.ts | 6 +++++- packages/macro/test/js-useLingui.ts | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/macro/src/macroJs.ts b/packages/macro/src/macroJs.ts index 7b1e8d1ef..2b4eae976 100644 --- a/packages/macro/src/macroJs.ts +++ b/packages/macro/src/macroJs.ts @@ -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 @@ -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 diff --git a/packages/macro/src/plugin.ts b/packages/macro/src/plugin.ts index 04a91c6be..4bda43dc1 100644 --- a/packages/macro/src/plugin.ts +++ b/packages/macro/src/plugin.ts @@ -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" diff --git a/packages/macro/test/js-useLingui.ts b/packages/macro/test/js-useLingui.ts index 08044fab7..d8daa7f17 100644 --- a/packages/macro/test/js-useLingui.ts +++ b/packages/macro/test/js-useLingui.ts @@ -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) `, }, {