Repository: i18next/i18next-parser
Pull request #389 (2021-08-04 11:54:20)

fix: added support for string arrays as an argument to the useTranslation
### Why am I submitting this PR

added support for string arrays as an argument to the useTranslation

### Does it fix an existing ticket?

Yes/No #305

### Checklist

- [ ] only relevant code is changed (make a diff before you submit the PR)
- [ ] tests are included and pass (see [details here](https://github.com/i18next/i18next-parser/blob/master/docs/development.md#tests))
- [ ] documentation is changed or added
- [ ] I ran `yarn build` to compile and prettify the code


CarrotIronfoundersson: This fix indeed solves the issue with array of namespaces as argument for useTranslation hook. I applied the change manually and tested it my project environment!
AlphaByte02: This resolve also the #220 that was lost.

You probably need to add the test case for this PR to be valid and also is a good thing for the future.
karellm: @sarakusha Thanks for the PR, can you please add a test to document this change? It is a requirement for me to merge a PR. 
simonasdev: would also love this feature
sarakusha: > @sarakusha Thanks for the PR, can you please add a test to document this change? It is a requirement for me to merge a PR.

Actually, I practically didn’t do anything new. When I wanted to add functionality, it turned out that everything was implemented for `withTranslation`. I just changed the condition so that it all worked for `useTranslation`
karellm: @sarakusha I understand but maybe the feature wasn't properly tested before. It doesn't hurt to add a test, see it as a way to document the change.
sarakusha: I looked at how the parser works and found that it does not know how to handle constructs like `t("namespace:key")`, but it recognizes `t("key", { ns: namespace })`
I will create a new PR
sarakusha: I had to add the passing of `options` from the `Parser` to `Lexer`, since the namespace separator is set in the `Transform` and passed to the `Parser`, but there was no way to get it in the `Lexer`
codecov-commenter: # [Codecov](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) Report
> Merging [#389](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) (a33f762) into [master](https://codecov.io/gh/i18next/i18next-parser/commit/618708a13a3b685091a2b5fb07f71bfddce051b1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) (618708a) will **increase** coverage by `0.05%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master     #389      +/-   ##
==========================================
+ Coverage   93.09%   93.14%   +0.05%     
==========================================
  Files           9        9              
  Lines         608      613       +5     
==========================================
+ Hits          566      571       +5     
  Misses         42       42              
```


| [Impacted Files](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) | Coverage Δ | |
|---|---|---|
| [src/lexers/javascript-lexer.js](https://codecov.io/gh/i18next/i18next-parser/pull/389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL2xleGVycy9qYXZhc2NyaXB0LWxleGVyLmpz) | `95.08% <100.00%> (+0.21%)` | :arrow_up: |
| [src/parser.js](https://codecov.io/gh/i18next/i18next-parser/pull/389/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL3BhcnNlci5qcw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next).
> **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=i18next)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next). Last update [618708a...a33f762](https://codecov.io/gh/i18next/i18next-parser/pull/389?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next). 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=i18next).

karellm: @sarakusha I don't think this logic belongs in the Lexer. The Lexer is only extracting keys (whether it contains a namespace or not). Then the transform is in charge of making sense what is in the key. You can see the code here: https://github.com/i18next/i18next-parser/blob/master/src/transform.js#L103

That's the reason why Lexers don't need to know about the `namespaceSeparator`, it's not their concern. If you want to test for namespace handling, it should happen at the parser level, not the lexer level.

Patch:
diff --git a/src/lexers/javascript-lexer.js b/src/lexers/javascript-lexer.js
index 480e2de..6c83323 100644
--- a/src/lexers/javascript-lexer.js
+++ b/src/lexers/javascript-lexer.js
@@ -110,14 +110,8 @@ export default class JavascriptLexer extends BaseLexer {
     const entry = {}
 
     if (
-      node.expression.escapedText === 'useTranslation' &&
-      node.arguments.length
-    ) {
-      this.defaultNamespace = node.arguments[0].text
-    }
-
-    if (
-      node.expression.escapedText === 'withTranslation' &&
+      (node.expression.escapedText === 'useTranslation' ||
+      node.expression.escapedText === 'withTranslation') &&
       node.arguments.length
     ) {
       const { text, elements } = node.arguments[0]


Test patch:
diff --git a/test/lexers/javascript-lexer.test.js b/test/lexers/javascript-lexer.test.js
index 057c34f..6a869e3 100644
--- a/test/lexers/javascript-lexer.test.js
+++ b/test/lexers/javascript-lexer.test.js
@@ -189,6 +189,15 @@ describe('JavascriptLexer', () => {
       ])
     })
 
+    it('extracts first namespace when it is an array', () => {
+      const Lexer = new JavascriptLexer()
+      const content =
+        'const ExtendedComponent = useTranslation(["foo", "baz"]); t("bar");'
+      assert.deepEqual(Lexer.extract(content), [
+        { namespace: 'foo', key: 'bar' },
+      ])
+    })
+
     it('uses namespace from t function with priority', () => {
       const Lexer = new JavascriptLexer()
       const content =