Skip to content

Commit

Permalink
fix(babel-plugin-extract-messages): extraction template string id & c…
Browse files Browse the repository at this point in the history
…omment
  • Loading branch information
sultan99 committed Apr 4, 2021
1 parent c567ea4 commit 1d6043a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/babel-plugin-extract-messages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
15 changes: 15 additions & 0 deletions packages/macro/test/js-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1d6043a

Please sign in to comment.