Skip to content

Commit c8d3206

Browse files
committed
fix(babel-plugin-extract-messages): extraction template string id & comment
1 parent c567ea4 commit c8d3206

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

packages/babel-plugin-extract-messages/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ export default function ({ types: t }) {
237237
.get(`properties.${i}.value`)
238238
.buildCodeFrameError("Only strings are supported as comments.")
239239
}
240-
241-
props[key.name] = value.value
240+
const isIdLiteral = !value.value && key.name === "id" && t.isTemplateLiteral(value)
241+
props[key.name] = isIdLiteral ? value?.quasis[0]?.value?.cooked : value.value
242242
})
243243

244244
collectMessage(path, file, props)

packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Object {
1616

1717
exports[`@lingui/babel-plugin-extract-messages should extract all messages from JS files (macros) 1`] = `
1818
Object {
19+
Backtick: Object {
20+
extractedComments: Array [],
21+
origin: Array [
22+
Array [
23+
js-with-macros.js,
24+
24,
25+
],
26+
],
27+
},
1928
Description: Object {
2029
extractedComments: Array [
2130
description,

packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const withTId = t({
2121
message: "Message with id some"
2222
})
2323

24+
const withTIdBacktick = t({
25+
id: `Backtick`
26+
})
27+
2428
const id = 'message id'
2529

2630
const withUnknownId = t({

packages/macro/test/js-t.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ export default [
162162
});
163163
`,
164164
},
165+
{
166+
name: "Support id in template literal",
167+
input: `
168+
import { t } from '@lingui/macro'
169+
const msg = t({ id: \`msgId\` })
170+
`,
171+
expected: `import { i18n } from "@lingui/core";
172+
const msg =
173+
i18n._(/*i18n*/
174+
{
175+
id: \`msgId\`
176+
});
177+
`,
178+
},
165179
{
166180
name: "Newlines after continuation character are removed",
167181
filename: "js-t-continuation-character.js",

0 commit comments

Comments
 (0)