diff --git a/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap b/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap index 9ec92537d..f556de872 100644 --- a/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap +++ b/packages/babel-plugin-extract-messages/test/__snapshots__/index.ts.snap @@ -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 [ diff --git a/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js b/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js index 02f9b9043..f7c1ed960 100644 --- a/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js +++ b/packages/babel-plugin-extract-messages/test/fixtures/js-with-macros.js @@ -15,3 +15,8 @@ const withId = defineMessage({ }) const withValues = t`Values ${param}` + +const withTId = t({ + id: "ID Some", + message: "Message with id some" +}) diff --git a/packages/macro/src/constants.ts b/packages/macro/src/constants.ts index 5181ba322..d54853d7d 100644 --- a/packages/macro/src/constants.ts +++ b/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" diff --git a/packages/macro/src/macroJs.ts b/packages/macro/src/macroJs.ts index 1419d00b2..e57207db7 100644 --- a/packages/macro/src/macroJs.ts +++ b/packages/macro/src/macroJs.ts @@ -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 @@ -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) } @@ -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 } @@ -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) } @@ -160,10 +162,6 @@ export default class MacroJs { ), [descriptor] ) - - this.addExtractMark(path) - - // @ts-ignore path.replaceWith(newNode) } @@ -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 ) @@ -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 diff --git a/packages/macro/test/js-t.ts b/packages/macro/test/js-t.ts index abbfe02f3..4f41b6495 100644 --- a/packages/macro/test/js-t.ts +++ b/packages/macro/test/js-t.ts @@ -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, + }, + }); `, }, {