diff --git a/packages/babel-plugin-extract-messages/src/index.ts b/packages/babel-plugin-extract-messages/src/index.ts index 8eaf1bca0..19bb46eb9 100644 --- a/packages/babel-plugin-extract-messages/src/index.ts +++ b/packages/babel-plugin-extract-messages/src/index.ts @@ -232,13 +232,13 @@ export default function ({ types: t }) { path.node.properties .filter(({ key }) => copyProps.indexOf(key.name) !== -1) .forEach(({ key, value }, i) => { - if (key.name === "comment" && !t.isStringLiteral(value)) { + if (key.name === "comment" && !(t.isStringLiteral(value) || t.isTemplateLiteral(value))) { throw path .get(`properties.${i}.value`) .buildCodeFrameError("Only strings are supported as comments.") } - props[key.name] = value.value + props[key.name] = value.value || t.isTemplateLiteral(value) && value.quasis[0]?.value?.cooked || `` }) collectMessage(path, file, props) diff --git a/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap b/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap index 5571ff1c3..77527941d 100644 --- a/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap +++ b/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap @@ -16,6 +16,25 @@ Object { exports[`@lingui/babel-plugin-extract-messages should extract all messages from JS files (macros) 1`] = ` Object { + : Object { + extractedComments: Array [], + origin: Array [ + Array [ + js-with-macros.js, + 31, + ], + ], + }, + Backtick: Object { + extractedComments: Array [], + message: Message with backtick id, + origin: Array [ + Array [ + js-with-macros.js, + 24, + ], + ], + }, Description: Object { extractedComments: Array [ description, diff --git a/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js b/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js index 607eb9c01..76411a18e 100644 --- a/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js +++ b/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js @@ -21,6 +21,11 @@ const withTId = t({ message: "Message with id some" }) +const withTIdBacktick = t({ + id: `Backtick`, + message: `Message with backtick id` +}) + const id = 'message id' const withUnknownId = t({ diff --git a/packages/macro/test/js-t.ts b/packages/macro/test/js-t.ts index a87ca7480..c5d33f597 100644 --- a/packages/macro/test/js-t.ts +++ b/packages/macro/test/js-t.ts @@ -162,6 +162,21 @@ export default [ }); `, }, + { + name: "Support id & comment in template literal", + input: ` + import { t } from '@lingui/macro' + const msg = t({ id: \`msgId\`, comment: \`description for translators\` }) + `, + expected: `import { i18n } from "@lingui/core"; + const msg = + i18n._(/*i18n*/ + { + id: \`msgId\`, + comment: \`description for translators\` + }); + `, + }, { name: "Newlines after continuation character are removed", filename: "js-t-continuation-character.js",