Repository: i18next/i18next-parser
Pull request #1022 (2024-07-15 13:57:01)

Fix plurals not being detected when count is a CallExpression
### Why am I submitting this PR

Addresses https://github.com/i18next/i18next-parser/issues/1015 where the JavaScript lexer would no longer correctly identify plurals where the count was a CallExpression

### Does it fix an existing ticket?

Yes #1015

### Checklist

- [x] only relevant code is changed (make a diff before you submit the PR)
- [x] tests are included and pass: `yarn test` (see [details here](https://github.com/i18next/i18next-parser/blob/master/docs/development.md#tests))
- [x] ~documentation is changed or added~


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/i18next/i18next-parser/pull/1022?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.35%. Comparing base [(`7e14d85`)](https://app.codecov.io/gh/i18next/i18next-parser/commit/7e14d85c86549bf22cef5fe45851b36aa4d35699?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) to head [(`bf46c56`)](https://app.codecov.io/gh/i18next/i18next-parser/commit/bf46c56b1deabc5b133a826f12bb370195c65743?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next).
> Report is 44 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #1022      +/-   ##
==========================================
+ Coverage   95.16%   95.35%   +0.19%     
==========================================
  Files          11       10       -1     
  Lines        1922     1918       -4     
==========================================
  Hits         1829     1829              
+ Misses         93       89       -4     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/i18next/i18next-parser/pull/1022?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next).


Patch:
diff --git a/src/lexers/javascript-lexer.js b/src/lexers/javascript-lexer.js
index d1f9d0f..907d8e0 100644
--- a/src/lexers/javascript-lexer.js
+++ b/src/lexers/javascript-lexer.js
@@ -323,6 +323,8 @@ export default class JavascriptLexer extends BaseLexer {
               const nestedEntries = this.expressionExtractor(p.initializer)
               if (nestedEntries) {
                 entries.push(...nestedEntries)
+              } else {
+                entries[0][p.name.text] = p.initializer.text || ''
               }
             } else {
               entries[0][p.name.text] = p.initializer.text || ''


Test patch:
diff --git a/test/parser.test.js b/test/parser.test.js
index f06353c..f056f4f 100644
--- a/test/parser.test.js
+++ b/test/parser.test.js
@@ -1638,6 +1638,30 @@ describe('parser', () => {
       i18nextParser.end(fakeFile)
     })
 
+    it('generates plurals when count is an expression', (done) => {
+      let result
+      const i18nextParser = new i18nTransform()
+      const fakeFile = new Vinyl({
+        contents: Buffer.from("t('test {{count}}', { count: expr() })"),
+        path: 'file.js',
+      })
+
+      i18nextParser.on('data', (file) => {
+        if (file.relative.endsWith(enLibraryPath)) {
+          result = JSON.parse(file.contents)
+        }
+      })
+      i18nextParser.once('end', () => {
+        assert.deepEqual(result, {
+          'test {{count}}_one': '',
+          'test {{count}}_other': '',
+        })
+        done()
+      })
+
+      i18nextParser.end(fakeFile)
+    })
+
     it('generates plurals with custom plural separator', (done) => {
       let result
       const i18nextParser = new i18nTransform({