Repository: lingui/js-lingui
Pull request #224 (2018-06-12 04:57:07)

 Correctly extract <Trans id={'message.id'} />
Resolves #189 


Patch:
diff --git a/packages/babel-plugin-extract-messages/src/index.js b/packages/babel-plugin-extract-messages/src/index.js
index f6ec347f..a90370cb 100644
--- a/packages/babel-plugin-extract-messages/src/index.js
+++ b/packages/babel-plugin-extract-messages/src/index.js
@@ -108,7 +108,16 @@ export default function({ types: t }) {
 
         const props = attrs.reduce((acc, item) => {
           const key = item.name.name
-          if (key === "id" || key === "defaults") acc[key] = item.value.value
+          if (key === "id" || key === "defaults") {
+            if (item.value.value) {
+              acc[key] = item.value.value
+            } else if (
+              item.value.expression &&
+              t.isStringLiteral(item.value.expression)
+            ) {
+              acc[key] = item.value.expression.value
+            }
+          }
           return acc
         }, {})
 
diff --git a/yarn.lock b/yarn.lock
index 52c414b8..588ca7cb 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -124,9 +124,9 @@ acorn@^5.0.0, acorn@^5.3.0, acorn@^5.5.0:
   version "5.5.3"
   resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9"
 
-ajv-keywords@^2.1.0:
-  version "2.1.1"
-  resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762"
+ajv-keywords@^3.0.0:
+  version "3.1.0"
+  resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.1.0.tgz#ac2b27939c543e95d2c06e7f7f5c27be4aa543be"
 
 ajv-keywords@^3.1.0:
   version "3.2.0"
@@ -139,7 +139,7 @@ ajv@^4.9.1:
     co "^4.6.0"
     json-stable-stringify "^1.0.1"
 
-ajv@^5.1.0, ajv@^5.2.3, ajv@^5.3.0:
+ajv@^5.1.0, ajv@^5.3.0:
   version "5.5.2"
   resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
   dependencies:
@@ -148,6 +148,14 @@ ajv@^5.1.0, ajv@^5.2.3, ajv@^5.3.0:
     fast-json-stable-stringify "^2.0.0"
     json-schema-traverse "^0.3.0"
 
+ajv@^6.0.1:
+  version "6.2.0"
+  resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.2.0.tgz#afac295bbaa0152449e522742e4547c1ae9328d2"
+  dependencies:
+    fast-deep-equal "^1.0.0"
+    fast-json-stable-stringify "^2.0.0"
+    json-schema-traverse "^0.3.0"
+
 ajv@^6.1.0:
   version "6.5.0"
   resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.5.0.tgz#4c8affdf80887d8f132c9c52ab8a2dc4d0b7b24c"
@@ -1500,10 +1508,9 @@ concat-map@0.0.1:
   resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
 
 concat-stream@^1.6.0:
-  version "1.6.2"
-  resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34"
+  version "1.6.1"
+  resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.1.tgz#261b8f518301f1d834e36342b9fea095d2620a26"
   dependencies:
-    buffer-from "^1.0.0"
     inherits "^2.0.3"
     readable-stream "^2.2.2"
     typedarray "^0.0.6"


Test patch:
diff --git a/packages/babel-plugin-extract-messages/test/fixtures/jsx/all.js b/packages/babel-plugin-extract-messages/test/fixtures/jsx/all.js
index f66f5b9a..2870b36d 100644
--- a/packages/babel-plugin-extract-messages/test/fixtures/jsx/all.js
+++ b/packages/babel-plugin-extract-messages/test/fixtures/jsx/all.js
@@ -3,7 +3,7 @@ import { Trans } from "@lingui/react"
 ;<span id="ignore" />
 ;<Trans /> // this should be ignored
 ;<Trans defaults="Missing ID" /> // this should be ignored
-;<Trans id="msg.hello" />
+;<Trans id={"msg.hello"} />
 ;<Trans id="msg.default" defaults="Hello World" />
 ;<Trans id="msg.default" defaults="Hello World" />
 ;<Trans id="Hi, my name is <0>{name}</0>" values={{ count }} />