Skip to content

Commit

Permalink
fix: t macro as function not extracting (lingui#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal authored and Bertg committed Nov 11, 2020
1 parent 63cae72 commit 3d22d2f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
Expand Up @@ -33,6 +33,15 @@ Object {
],
],
},
ID Some: Object {
message: Message with id some,
origin: Array [
Array [
js-with-macros.js,
19,
],
],
},
Message: Object {
origin: Array [
Array [
Expand Down
Expand Up @@ -15,3 +15,8 @@ const withId = defineMessage({
})

const withValues = t`Values ${param}`

const withTId = t({
id: "ID Some",
message: "Message with id some"
})
1 change: 1 addition & 0 deletions packages/macro/src/constants.ts
@@ -1,3 +1,4 @@
export const ID = "id"
export const MESSAGE = "message"
export const COMMENT = "comment"
export const EXTRACT_MARK = "i18n"
23 changes: 7 additions & 16 deletions packages/macro/src/macroJs.ts
Expand Up @@ -4,7 +4,7 @@ import { NodePath } from "@babel/traverse"

import ICUMessageFormat from "./icu"
import { zip, makeCounter } from "./utils"
import { COMMENT, ID, MESSAGE } from "./constants"
import { COMMENT, ID, MESSAGE, EXTRACT_MARK } from "./constants"

const keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g
const keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class MacroJs {
// preserve line number
newNode.loc = path.node.loc

this.addExtractMark(path)
path.addComment("leading", EXTRACT_MARK)
// @ts-ignore
path.replaceWith(newNode)
}
Expand All @@ -98,7 +98,10 @@ export default class MacroJs {
return
}

if (this.types.isCallExpression(path.node) && this.isIdentifier(path.node.callee, "t")) {
if (
this.types.isCallExpression(path.node) &&
this.isIdentifier(path.node.callee, "t")
) {
this.replaceTAsFunction(path)
return
}
Expand Down Expand Up @@ -143,7 +146,6 @@ export default class MacroJs {
this._expressionIndex = makeCounter()

const descriptor = this.processDescriptor(path.node.arguments[0])
this.addExtractMark(path)
path.replaceWith(descriptor)
}

Expand All @@ -160,10 +162,6 @@ export default class MacroJs {
),
[descriptor]
)

this.addExtractMark(path)

// @ts-ignore
path.replaceWith(newNode)
}

Expand All @@ -185,6 +183,7 @@ export default class MacroJs {
*
*/
processDescriptor = (descriptor) => {
this.types.addComment(descriptor, "leading", EXTRACT_MARK)
const messageIndex = descriptor.properties.findIndex(
(property) => property.key.name === MESSAGE
)
Expand Down Expand Up @@ -352,14 +351,6 @@ export default class MacroJs {
}
}

/**
* addExtractMark - add comment which marks the string/object
* for extraction.
* @lingui/babel-extract-messages looks for this comment
*/
addExtractMark = (path) => {
path.addComment("leading", "i18n")
}

/**
* Custom matchers
Expand Down
27 changes: 12 additions & 15 deletions packages/macro/test/js-t.ts
Expand Up @@ -86,22 +86,19 @@ export default [
name: "Support id and comment in t macro as callExpression",
input: `
import { t } from '@lingui/macro'
t({
id: 'msgId_2',
message: 'text',
comment: 'description for translators'
})
t({ id: 'msgId', comment: 'description for translators', message: plural(val, { one: '...', other: '...' }) })
const msg = t({ id: 'msgId', comment: 'description for translators', message: plural(val, { one: '...', other: '...' }) })
`,
expected: `
import { i18n } from "@lingui/core"
/*i18n*/
i18n._({ id: "msgId_2", message: 'text', comment: 'description for translators' })
/*i18n*/
i18n._({ id: "msgId", comment: 'description for translators', message: '{val, plural, one {...} other {...}}', values: {
val: val,
} })
expected: `import { i18n } from "@lingui/core";
const msg =
i18n._(/*i18n*/
{
id: "msgId",
comment: "description for translators",
message: "{val, plural, one {...} other {...}}",
values: {
val: val,
},
});
`,
},
{
Expand Down

0 comments on commit 3d22d2f

Please sign in to comment.