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: parse template strings in t and defineMessage macros #862

Merged
merged 1 commit into from
Nov 14, 2020
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
8 changes: 6 additions & 2 deletions packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ export default class MacroJs {

// if there's `message` property, replace macros with formatted message
const node = descriptor.properties[messageIndex]
const tokens = this.tokenizeNode(node.value, true)

// Inside message descriptor the `t` macro in `message` prop is optional.
// Template strings are always processed as if they were wrapped by `t`.
const tokens = this.types.isTemplateLiteral(node.value)
? this.tokenizeTemplateLiteral(node.value)
: this.tokenizeNode(node.value, true)

let messageNode = node.value
if (tokens != null) {
Expand Down Expand Up @@ -351,7 +356,6 @@ export default class MacroJs {
}
}


/**
* Custom matchers
*/
Expand Down
9 changes: 6 additions & 3 deletions packages/macro/test/js-defineMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ export default [
`,
},
{
name: "should left string message intact - template literal",
name: "should transform template literals",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally thiw would be a breaking change, but since the code didn't work as described in documentation, I believe it's just a fix ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope so 🤪

input: `
import { defineMessage } from '@lingui/macro';
const message = defineMessage({
message: \`Message\`
message: \`Message \${name}\`
})
`,
expected: `
import { i18n } from "@lingui/core";
const message =
/*i18n*/
{
id: \`Message\`
id: "Message {name}",
values: {
name: name
}
};
`,
},
Expand Down
17 changes: 17 additions & 0 deletions packages/macro/test/js-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ export default [
i18n._("Multiline\\nstring")
`,
},
{
name: "Support template strings in t macro message",
input: `
import { t } from '@lingui/macro'
const msg = t({ message: \`Hello \${name}\` })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: "Hello {name}",
values: {
name: name,
},
});
`,
},
{
name: "Support id and comment in t macro as callExpression",
input: `
Expand Down