Repository: i18next/i18next-parser
Pull request #420 (2021-09-16 23:42:16)

Add template literal support for `defaultValue` as a second argument (#419)
### Why am I submitting this PR

It adds template literal support for `defaultValue` as a second argument.

### Does it fix an existing ticket?

Yes #419 

### Checklist

- [x] only relevant code is changed (make a diff before you submit the PR)
- [x] 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
- [x] I ran `yarn build` to compile and prettify the code


codecov-commenter: # [Codecov](https://codecov.io/gh/i18next/i18next-parser/pull/420?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) Report
> Merging [#420](https://codecov.io/gh/i18next/i18next-parser/pull/420?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) (0f84644) into [master](https://codecov.io/gh/i18next/i18next-parser/commit/fa3d69f312d353a78c8a9626a890843488423cfe?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) (fa3d69f) will **increase** coverage by `0.02%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/i18next/i18next-parser/pull/420/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/420?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next)

```diff
@@            Coverage Diff             @@
##           master     #420      +/-   ##
==========================================
+ Coverage   93.10%   93.12%   +0.02%     
==========================================
  Files           9        9              
  Lines         609      611       +2     
==========================================
+ Hits          567      569       +2     
  Misses         42       42              
```


| [Impacted Files](https://codecov.io/gh/i18next/i18next-parser/pull/420?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/420/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL2xleGVycy9qYXZhc2NyaXB0LWxleGVyLmpz) | `94.78% <ø> (ø)` | |
| [src/transform.js](https://codecov.io/gh/i18next/i18next-parser/pull/420/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL3RyYW5zZm9ybS5qcw==) | `81.43% <0.00%> (+0.22%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/i18next/i18next-parser/pull/420?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/420?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next). Last update [fa3d69f...0f84644](https://codecov.io/gh/i18next/i18next-parser/pull/420?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).


Patch:
diff --git a/dist/lexers/javascript-lexer.js b/dist/lexers/javascript-lexer.js
index 565249c..0c5d87e 100644
--- a/dist/lexers/javascript-lexer.js
+++ b/dist/lexers/javascript-lexer.js
@@ -163,8 +163,9 @@ JavascriptLexer = /*#__PURE__*/function (_BaseLexer) {(0, _inherits2["default"])
 
         // Second argument could be a string default value
         if (
-        optionsArgument &&
-        optionsArgument.kind === ts.SyntaxKind.StringLiteral)
+        optionsArgument && (
+        optionsArgument.kind === ts.SyntaxKind.StringLiteral ||
+        optionsArgument.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral))
         {
           entry.defaultValue = optionsArgument.text;
           optionsArgument = node.arguments.shift();
diff --git a/src/lexers/javascript-lexer.js b/src/lexers/javascript-lexer.js
index 690c2c7..357bab8 100644
--- a/src/lexers/javascript-lexer.js
+++ b/src/lexers/javascript-lexer.js
@@ -164,7 +164,8 @@ export default class JavascriptLexer extends BaseLexer {
       // Second argument could be a string default value
       if (
         optionsArgument &&
-        optionsArgument.kind === ts.SyntaxKind.StringLiteral
+        (optionsArgument.kind === ts.SyntaxKind.StringLiteral ||
+          optionsArgument.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral)
       ) {
         entry.defaultValue = optionsArgument.text
         optionsArgument = node.arguments.shift()


Test patch:
diff --git a/test/lexers/javascript-lexer.test.js b/test/lexers/javascript-lexer.test.js
index 6a869e3..adddef1 100644
--- a/test/lexers/javascript-lexer.test.js
+++ b/test/lexers/javascript-lexer.test.js
@@ -10,7 +10,7 @@ describe('JavascriptLexer', () => {
     done()
   })
 
-  it('extracts the second argument as defaultValue', (done) => {
+  it('extracts the second argument string literal as defaultValue', (done) => {
     const Lexer = new JavascriptLexer()
     const content = 'i18n.t("first", "bla")'
     assert.deepEqual(Lexer.extract(content), [
@@ -19,6 +19,15 @@ describe('JavascriptLexer', () => {
     done()
   })
 
+  it('extracts the second argument template literal as defaultValue', (done) => {
+    const Lexer = new JavascriptLexer()
+    const content = 'i18n.t("first", `bla`)'
+    assert.deepEqual(Lexer.extract(content), [
+      { key: 'first', defaultValue: 'bla' },
+    ])
+    done()
+  })
+
   it('extracts the defaultValue/context options', (done) => {
     const Lexer = new JavascriptLexer()
     const content = 'i18n.t("first", {defaultValue: "foo", context: \'bar\'})'