Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macro): fix failure when macro meets function carrying #1884

Merged
merged 2 commits into from
Mar 11, 2024
Merged
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
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
Loading