Repository: lingui/js-lingui
Pull request #846 (2020-11-10 11:22:11)

fix: t macro as function not extracting
Will close https://github.com/lingui/js-lingui/issues/844

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/lingui-js/js-lingui/cemkyckqo](https://vercel.com/lingui-js/js-lingui/cemkyckqo)  
✅ Preview: *In Progress*



Patch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 37d66602..0028a2ed 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -66,6 +66,8 @@ jobs:
           token: '${{ secrets.GH_TOKEN }}'
 
       - name: Release packages
+        env:
+          GH_TOKEN: ${{secrets.GH_TOKEN}}
         run: |
           yarn version:${{github.event.inputs.npmTag}}
           yarn release:${{github.event.inputs.npmTag}}
diff --git a/packages/core/src/i18n.ts b/packages/core/src/i18n.ts
index c053ec9c..d5bb3fe1 100644
--- a/packages/core/src/i18n.ts
+++ b/packages/core/src/i18n.ts
@@ -32,7 +32,7 @@ export type MessageDescriptor = {
   id?: string
   comment?: string
   message?: string
-  values?: string
+  values?: Record<string, unknown>
 }
 
 type setupI18nProps = {
diff --git a/packages/macro/src/constants.ts b/packages/macro/src/constants.ts
index 5181ba32..d54853d7 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 1419d00b..e57207db 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


Test patch:
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 9ec92537..f556de87 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 02f9b904..f7c1ed96 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/test/js-t.ts b/packages/macro/test/js-t.ts
index abbfe02f..4f41b649 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,
+          },
+        });
       `,
   },
   {