Repository: lingui/js-lingui
Pull request #1143 (2021-10-02 00:02:23)

fix: stripping origin if line numbers already strippped
Given config with `lineNumbers: false`, if you run extract, and then subsequently run extract with the watcher on the entire origin is stripped.

Line 30 in `po.ts` was slicing regardless of the length of the array.

Modified this to destructure the array and remove the unnecessary joinOrigin call.

Added a new test as I wasn't quite sure how to modify the existing one to include this case 🤔

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/8bNPwxuNfFi4gBJW5npFAp7iFypP](https://vercel.com/lingui-js/js-lingui/8bNPwxuNfFi4gBJW5npFAp7iFypP)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui) Report
> Merging [#1143](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui) (31cc14d) into [main](https://codecov.io/gh/lingui/js-lingui/commit/a9bffddf2ae0c9b6ab6a48552d085cfd53140cca?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui) (a9bffdd) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/lingui/js-lingui/pull/1143/graphs/tree.svg?width=650&height=150&src=pr&token=CuJ1VD5BY5&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui)](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui)

```diff
@@           Coverage Diff           @@
##             main    #1143   +/-   ##
=======================================
  Coverage   82.34%   82.34%           
=======================================
  Files          56       56           
  Lines        1705     1705           
  Branches      469      469           
=======================================
  Hits         1404     1404           
  Misses        176      176           
  Partials      125      125           
```


| [Impacted Files](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui) | Coverage Δ | |
|---|---|---|
| [packages/cli/src/api/catalog.ts](https://codecov.io/gh/lingui/js-lingui/pull/1143/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui#diff-cGFja2FnZXMvY2xpL3NyYy9hcGkvY2F0YWxvZy50cw==) | `81.48% <ø> (ø)` | |
| [packages/cli/src/api/formats/po.ts](https://codecov.io/gh/lingui/js-lingui/pull/1143/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui#diff-cGFja2FnZXMvY2xpL3NyYy9hcGkvZm9ybWF0cy9wby50cw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui). Last update [5c5c8a7...31cc14d](https://codecov.io/gh/lingui/js-lingui/pull/1143?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=lingui).


Patch:
diff --git a/packages/cli/src/api/catalog.ts b/packages/cli/src/api/catalog.ts
index 03ed4dd9..ef88d09a 100644
--- a/packages/cli/src/api/catalog.ts
+++ b/packages/cli/src/api/catalog.ts
@@ -21,7 +21,7 @@ const LOCALE = "{locale}"
 const LOCALE_SUFFIX_RE = /\{locale\}.*$/
 const PATHSEP = "/" // force posix everywhere
 
-type MessageOrigin = [string, number]
+type MessageOrigin = [string, number?]
 
 export type ExtractedMessageType = {
   message?: string
diff --git a/packages/cli/src/api/formats/po-gettext.ts b/packages/cli/src/api/formats/po-gettext.ts
index d041b777..a9a6d3fd 100644
--- a/packages/cli/src/api/formats/po-gettext.ts
+++ b/packages/cli/src/api/formats/po-gettext.ts
@@ -65,7 +65,7 @@ const serialize = (items: CatalogType, options) =>
       
       if (options.origins !== false) {
         if (message.origin && options.lineNumbers === false) {
-          item.references = message.origin.map(msg => msg.slice(0, -1)).map(joinOrigin)
+          item.references = message.origin.map(([path]) => path);
         } else {
           item.references = message.origin ? message.origin.map(joinOrigin) : []
         }
diff --git a/packages/cli/src/api/formats/po.ts b/packages/cli/src/api/formats/po.ts
index 7ed55681..1d3bcff6 100644
--- a/packages/cli/src/api/formats/po.ts
+++ b/packages/cli/src/api/formats/po.ts
@@ -27,7 +27,7 @@ const serialize = (items: CatalogType, options) =>
       item.extractedComments = message.extractedComments || []
       if (options.origins !== false) {
         if (message.origin && options.lineNumbers === false) {
-          item.references = message.origin.map(msg => msg.slice(0, -1)).map(joinOrigin)
+          item.references = message.origin.map(([path]) => path);
         } else {
           item.references = message.origin ? message.origin.map(joinOrigin) : []
         }


Test patch:
diff --git a/packages/cli/src/api/formats/po.test.ts b/packages/cli/src/api/formats/po.test.ts
index 9680e5c2..0b762763 100644
--- a/packages/cli/src/api/formats/po.test.ts
+++ b/packages/cli/src/api/formats/po.test.ts
@@ -266,4 +266,60 @@ describe("pofile format", () => {
 
     `)
   })
+
+  it("should not include lineNumbers if lineNumbers option is false and already excluded", () => {
+    mockFs({
+      locale: {
+        en: mockFs.directory(),
+      },
+    })
+
+    const filename = path.join("locale", "en", "messages.po")
+    const catalog: CatalogType = {
+      static: {
+        translation: "Static message",
+      },
+      withOrigin: {
+        translation: "Message with origin",
+        origin: [["src/App.js"]],
+      },
+      withMultipleOrigins: {
+        translation: "Message with multiple origin",
+        origin: [
+          ["src/App.js"],
+          ["src/Component.js"],
+        ],
+      },
+    }
+    format.write(filename, catalog, {
+      origins: true,
+      lineNumbers: false,
+      locale: "en",
+    })
+    const pofile = fs.readFileSync(filename).toString()
+    mockFs.restore()
+    expect(pofile).toMatchInlineSnapshot(`
+      msgid ""
+      msgstr ""
+      "POT-Creation-Date: ${formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx")}\\n"
+      "MIME-Version: 1.0\\n"
+      "Content-Type: text/plain; charset=utf-8\\n"
+      "Content-Transfer-Encoding: 8bit\\n"
+      "X-Generator: @lingui/cli\\n"
+      "Language: en\\n"
+
+      msgid "static"
+      msgstr "Static message"
+
+      #: src/App.js
+      msgid "withOrigin"
+      msgstr "Message with origin"
+
+      #: src/App.js
+      #: src/Component.js
+      msgid "withMultipleOrigins"
+      msgstr "Message with multiple origin"
+
+    `)
+  })
 })