Skip to content

Commit

Permalink
fix: extract works with template string id's (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
sultan99 authored Apr 5, 2021
1 parent c567ea4 commit a17d629
Show file tree
Hide file tree
Showing 4 changed files with 29 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 @@ -237,8 +237,8 @@ export default function ({ types: t }) {
.get(`properties.${i}.value`)
.buildCodeFrameError("Only strings are supported as comments.")
}

props[key.name] = value.value
const isIdLiteral = !value.value && key.name === "id" && t.isTemplateLiteral(value)
props[key.name] = isIdLiteral ? value?.quasis[0]?.value?.cooked : value.value
})

collectMessage(path, file, props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ Object {

exports[`@lingui/babel-plugin-extract-messages should extract all messages from JS files (macros) 1`] = `
Object {
Backtick: Object {
extractedComments: Array [],
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,10 @@ const withTId = t({
message: "Message with id some"
})

const withTIdBacktick = t({
id: `Backtick`
})

const id = 'message id'

const withUnknownId = t({
Expand Down
14 changes: 14 additions & 0 deletions packages/macro/test/js-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ export default [
});
`,
},
{
name: "Support id in template literal",
input: `
import { t } from '@lingui/macro'
const msg = t({ id: \`msgId\` })
`,
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: \`msgId\`
});
`,
},
{
name: "Newlines after continuation character are removed",
filename: "js-t-continuation-character.js",
Expand Down

1 comment on commit a17d629

@vercel
Copy link

@vercel vercel bot commented on a17d629 Apr 5, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.