Skip to content

Commit a0403b9

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

4 files changed

Lines changed: 44 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
@@ -232,13 +232,13 @@ export default function ({ types: t }) {
232232
path.node.properties
233233
.filter(({ key }) => copyProps.indexOf(key.name) !== -1)
234234
.forEach(({ key, value }, i) => {
235-
if (key.name === "comment" && !t.isStringLiteral(value)) {
235+
if (key.name === "comment" && !(t.isStringLiteral(value) || t.isTemplateLiteral(value))) {
236236
throw path
237237
.get(`properties.${i}.value`)
238238
.buildCodeFrameError("Only strings are supported as comments.")
239239
}
240240

241-
props[key.name] = value.value
241+
props[key.name] = value.value || t.isTemplateLiteral(value) && value.quasis[0]?.value?.cooked || ``
242242
})
243243

244244
collectMessage(path, file, props)

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

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

1717
exports[`@lingui/babel-plugin-extract-messages should extract all messages from JS files (macros) 1`] = `
1818
Object {
19+
: Object {
20+
extractedComments: Array [],
21+
origin: Array [
22+
Array [
23+
js-with-macros.js,
24+
32,
25+
],
26+
],
27+
},
28+
Backtick: Object {
29+
extractedComments: Array [
30+
description for translators,
31+
],
32+
message: Message with backtick id,
33+
origin: Array [
34+
Array [
35+
js-with-macros.js,
36+
24,
37+
],
38+
],
39+
},
1940
Description: Object {
2041
extractedComments: Array [
2142
description,

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

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

24+
const withTIdBacktick = t({
25+
id: `Backtick`,
26+
comment: `description for translators`,
27+
message: `Message with backtick id`
28+
})
29+
2430
const id = 'message id'
2531

2632
const withUnknownId = t({

packages/macro/test/js-t.ts

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

0 commit comments

Comments
 (0)