Repository: lingui/js-lingui
Pull request #365 (2018-10-14 07:54:01)

fix: macros generates invalid ICU message for nested selects
Babel macros generates invalid ICU messages for nested select/plural.

Example source code:
```js
select('id', {
  value: "male",
  male: select({
    value: true,
    true: "He invites guests",
    other: "He does not invite guests"
  }),
  female: `She is ${gender}`,
  other: `They is ${gender}`
});
```

Expected message:
```
{0, select, male {{1, select, true {He invites guests} other {He does not invite guests}}} female {She is {gender}} other {They is {gender}}}
```

Actual message (missing brackets after `male`):
```
{0, select, male {1, select, true {He invites guests} other {He does not invite guests}} female {She is {gender}} other {They is {gender}}}
```

`lingui complie` throws error for this message:
```
Error: Can't parse message. Please check correct syntax: "{0, select, male {1, select, true {He invites guests} other {He does not invite guests}} female {She is {gender}} other {They is {gender}}}"
```

tricoder42: Wow, good catch! I'm gonna release it tomorrow. Thank you very much!
codecov[bot]: # [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=h1) Report
> Merging [#365](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=desc) into [master](https://codecov.io/gh/lingui/js-lingui/commit/a9750ed6925b70839a52251bf35a5b3f2bfb1742?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/lingui/js-lingui/pull/365/graphs/tree.svg?width=650&token=CuJ1VD5BY5&height=150&src=pr)](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #365   +/-   ##
=======================================
  Coverage   95.86%   95.86%           
=======================================
  Files          49       49           
  Lines        1379     1379           
=======================================
  Hits         1322     1322           
  Misses         57       57
```


| [Impacted Files](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/macro/src/js.js](https://codecov.io/gh/lingui/js-lingui/pull/365/diff?src=pr&el=tree#diff-cGFja2FnZXMvbWFjcm8vc3JjL2pzLmpz) | `99.25% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=footer). Last update [a9750ed...8fef269](https://codecov.io/gh/lingui/js-lingui/pull/365?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/macro/src/js.js b/packages/macro/src/js.js
index eebdcc47..c043c0e7 100644
--- a/packages/macro/src/js.js
+++ b/packages/macro/src/js.js
@@ -117,7 +117,7 @@ export default function({ types: t }) {
           value = props.text
         } else if (t.isCallExpression(attr.value)) {
           props = transformMethod(attr.value, file, { ...props, text: "" })
-          value = props.text
+          value = `{${props.text}}`
         } else {
           value = attr.value.value
         }


Test patch:
diff --git a/packages/macro/test/fixtures/select/actual.js b/packages/macro/test/fixtures/select/actual.js
index c014407e..742d928e 100644
--- a/packages/macro/test/fixtures/select/actual.js
+++ b/packages/macro/test/fixtures/select/actual.js
@@ -32,3 +32,14 @@ select('id', {
   female: `She is ${gender}`,
   other: `They is ${gender}`
 });
+
+select('id', {
+  value: "male",
+  "male": select({
+    value: true,
+    true: "He invites guests",
+    other: "He does not invite guests"
+  }),
+  female: `She is ${gender}`,
+  other: `They is ${gender}`
+});
\ No newline at end of file
diff --git a/packages/macro/test/fixtures/select/expected.js b/packages/macro/test/fixtures/select/expected.js
index 61c8eb8d..30869f43 100644
--- a/packages/macro/test/fixtures/select/expected.js
+++ b/packages/macro/test/fixtures/select/expected.js
@@ -1,5 +1,5 @@
 ( /*i18n*/{
-  id: "{gender, select, male {numOfGuests, plural, one {He invites one guest} other {He invites # guests}} female {She is {gender}} other {They is {gender}}}",
+  id: "{gender, select, male {{numOfGuests, plural, one {He invites one guest} other {He invites # guests}}} female {She is {gender}} other {They is {gender}}}",
   values: {
     gender: gender,
     numOfGuests: numOfGuests
@@ -8,7 +8,7 @@
 
 ( /*i18n*/{
   id: "id",
-  defaults: "{0, select, male {1, plural, one {He invites one guest} other {He invites # guests}} female {She is {gender}} other {They is {gender}}}",
+  defaults: "{0, select, male {{1, plural, one {He invites one guest} other {He invites # guests}}} female {She is {gender}} other {They is {gender}}}",
   values: {
     0: "male",
     1: 42,
@@ -18,10 +18,20 @@
 
 /*i18n: description*/({
   id: "id",
-  defaults: "{0, select, male {1, plural, one {He invites one guest} other {He invites # guests}} female {She is {gender}} other {They is {gender}}}",
+  defaults: "{0, select, male {{1, plural, one {He invites one guest} other {He invites # guests}}} female {She is {gender}} other {They is {gender}}}",
   values: {
     0: "male",
     1: 42,
     gender: gender
   }
 });
+
+( /*i18n*/{
+  id: "id",
+  defaults: "{0, select, male {{1, select, true {He invites guests} other {He does not invite guests}}} female {She is {gender}} other {They is {gender}}}",
+  values: {
+    0: "male",
+    1: true,
+    gender: gender
+  }
+});
\ No newline at end of file