Skip to content

Commit

Permalink
add the ability to extract concatenated comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlyons authored and Tanner Lyons committed Oct 20, 2021
1 parent 8ee42cb commit 8ea8445
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
37 changes: 34 additions & 3 deletions packages/babel-plugin-extract-messages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ function mapReplacer(key, value) {
return value;
}

function extractStringContatentation(t, node, error): string {
if (t.isStringLiteral(node)) {
return node.value
} else if (t.isBinaryExpression(node)) {
return (
extractStringContatentation(t, node.left, error) +
extractStringContatentation(t, node.right, error)
)
} else {
throw error
}
}

export default function ({ types: t }) {
let localTransComponentName

Expand Down Expand Up @@ -282,13 +295,31 @@ export default function ({ types: t }) {
path.node.properties
.filter(({ key }) => copyProps.indexOf(key.name) !== -1)
.forEach(({ key, value }, i) => {
// By default, the value is just the string value of the object property.
let valueToExtract = value.value;

if (key.name === "comment" && !t.isStringLiteral(value)) {
throw path
// Comments can be single or multi-line strings.
const errorIfNotAString = path
.get(`properties.${i}.value`)
.buildCodeFrameError("Only strings are supported as comments.")

if (t.isBinaryExpression(value)) {
valueToExtract = extractStringContatentation(
t,
value,
errorIfNotAString
)
} else {
throw errorIfNotAString
}
} else if (key.name === "id") {
const isIdLiteral = !value.value && t.isTemplateLiteral(value)
if (isIdLiteral) {
valueToExtract = value?.quasis[0]?.value?.cooked;
}
}
const isIdLiteral = !value.value && key.name === "id" && t.isTemplateLiteral(value)
props[key.name] = isIdLiteral ? value?.quasis[0]?.value?.cooked : value.value
props[key.name] = valueToExtract;
})

collectMessage(path, file, props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Object {
origin: Array [
Array [
js-without-macros.js,
11,
13,
],
],
},
Expand All @@ -153,7 +153,7 @@ Object {
origin: Array [
Array [
js-without-macros.js,
7,
9,
],
],
},
Expand All @@ -166,12 +166,23 @@ Object {
],
],
},
Multiline: Object {
extractedComments: Array [
this is 2 lines long,
],
origin: Array [
Array [
js-without-macros.js,
7,
],
],
},
Values {param}: Object {
extractedComments: Array [],
origin: Array [
Array [
js-without-macros.js,
9,
11,
],
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const msg = /*i18n*/{id: 'Message'}

const withDescription = /*i18n*/{id: 'Description', comment: "description"}

const withMultilineComment = /*i18n*/{id: 'Multiline', comment: "this is " + "2 lines long"}

const withId = /*i18n*/{id: 'ID', message: 'Message with id'}

const withValues = /*i18n*/{id: 'Values {param}', values: { param: param }}
Expand Down

0 comments on commit 8ea8445

Please sign in to comment.