Skip to content

Commit

Permalink
fix: parse template strings in t and defineMessage macros
Browse files Browse the repository at this point in the history
  • Loading branch information
tricoder42 committed Nov 13, 2020
1 parent 7cdcd61 commit 3e3f03b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
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",
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

0 comments on commit 3e3f03b

Please sign in to comment.