Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: t macro as function not extracting #846

Merged
merged 3 commits into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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