@lingui/babel-plugin-extract-messages 3.12.0 → 3.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +113 -27
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -38,35 +38,107 @@ function addMessage(path, messages, _ref) {
38
38
  newDefault = _ref.message,
39
39
  origin = _ref.origin,
40
40
  comment = _ref.comment,
41
- props = (0, _objectWithoutProperties2.default)(_ref, ["id", "message", "origin", "comment"]);
41
+ context = _ref.context,
42
+ props = (0, _objectWithoutProperties2.default)(_ref, ["id", "message", "origin", "comment", "context"]);
42
43
  // prevent from adding undefined msgid
43
44
  if (id === undefined) return;
45
+ var extractedComments = comment ? [comment] : [];
44
46
 
45
- if (messages.has(id)) {
46
- var message = messages.get(id); // only set/check default language when it's defined.
47
+ if (context) {
48
+ if (messages.has(context)) {
49
+ var existingContext = messages.get(context);
47
50
 
48
- if (message.message && newDefault && message.message !== newDefault) {
49
- throw path.buildCodeFrameError("Different defaults for the same message ID.");
50
- }
51
+ if (existingContext.has(id)) {
52
+ var message = messages.get(id); // only set/check default language when it's defined.
53
+
54
+ if (message.message && newDefault && message.message !== newDefault) {
55
+ throw path.buildCodeFrameError("Different defaults for the same message ID.");
56
+ }
51
57
 
52
- if (newDefault) {
53
- message.message = newDefault;
58
+ if (newDefault) {
59
+ message.message = newDefault;
60
+ }
61
+
62
+ ;
63
+ [].push.apply(message.origin, origin);
64
+
65
+ if (comment) {
66
+ ;
67
+ [].push.apply(message.extractedComments, [comment]);
68
+ }
69
+ } else {
70
+ existingContext.set(id, _objectSpread(_objectSpread({}, props), {}, {
71
+ message: newDefault,
72
+ origin: origin,
73
+ extractedComments: extractedComments
74
+ }));
75
+ messages.set(context, existingContext);
76
+ }
77
+ } else {
78
+ var newContext = new Map();
79
+ newContext.set(id, _objectSpread(_objectSpread({}, props), {}, {
80
+ message: newDefault,
81
+ origin: origin,
82
+ extractedComments: extractedComments
83
+ }));
84
+ messages.set(context, newContext);
54
85
  }
86
+ } else {
87
+ if (messages.has(id)) {
88
+ var _message = messages.get(id); // only set/check default language when it's defined.
55
89
 
56
- ;
57
- [].push.apply(message.origin, origin);
58
90
 
59
- if (comment) {
91
+ if (_message.message && newDefault && _message.message !== newDefault) {
92
+ throw path.buildCodeFrameError("Different defaults for the same message ID.");
93
+ }
94
+
95
+ if (newDefault) {
96
+ _message.message = newDefault;
97
+ }
98
+
60
99
  ;
61
- [].push.apply(message.extractedComments, [comment]);
100
+ [].push.apply(_message.origin, origin);
101
+
102
+ if (comment) {
103
+ ;
104
+ [].push.apply(_message.extractedComments, [comment]);
105
+ }
106
+ } else {
107
+ messages.set(id, _objectSpread(_objectSpread({}, props), {}, {
108
+ message: newDefault,
109
+ origin: origin,
110
+ extractedComments: extractedComments
111
+ }));
62
112
  }
113
+ }
114
+ }
115
+ /**
116
+ * An ES6 Map type is not possible to encode with JSON.stringify,
117
+ * so we can instead use a replacer function as an argument to
118
+ * tell the JSON parser how to serialize / deserialize the Maps
119
+ * it encounters.
120
+ */
121
+
122
+
123
+ function mapReplacer(key, value) {
124
+ if (value instanceof Map) {
125
+ var object = {};
126
+ value.forEach(function (v, k) {
127
+ return object[k] = v;
128
+ });
129
+ return object;
130
+ }
131
+
132
+ return value;
133
+ }
134
+
135
+ function extractStringContatentation(t, node, error) {
136
+ if (t.isStringLiteral(node)) {
137
+ return node.value;
138
+ } else if (t.isBinaryExpression(node)) {
139
+ return extractStringContatentation(t, node.left, error) + extractStringContatentation(t, node.right, error);
63
140
  } else {
64
- var extractedComments = comment ? [comment] : [];
65
- messages.set(id, _objectSpread(_objectSpread({}, props), {}, {
66
- message: newDefault,
67
- origin: origin,
68
- extractedComments: extractedComments
69
- }));
141
+ throw error;
70
142
  }
71
143
  }
72
144
 
@@ -132,7 +204,7 @@ function _default(_ref2) {
132
204
  var props = attrs.reduce(function (acc, item) {
133
205
  var key = item.name.name;
134
206
 
135
- if (key === "id" || key === "message" || key === "comment") {
207
+ if (key === "id" || key === "message" || key === "comment" || key === "context") {
136
208
  if (item.value.value) {
137
209
  acc[key] = item.value.value;
138
210
  } else if (item.value.expression && t.isStringLiteral(item.value.expression)) {
@@ -180,7 +252,7 @@ function _default(_ref2) {
180
252
  return;
181
253
  }
182
254
 
183
- var copyOptions = ["message", "comment"];
255
+ var copyOptions = ["message", "comment", "context"];
184
256
 
185
257
  if (t.isObjectExpression(path.node.arguments[2])) {
186
258
  path.node.arguments[2].properties.forEach(function (property) {
@@ -230,22 +302,36 @@ function _default(_ref2) {
230
302
 
231
303
  visited.add(path.node);
232
304
  var props = {};
233
- var copyProps = ["id", "message", "comment"];
305
+ var copyProps = ["id", "message", "comment", "context"];
234
306
  path.node.properties.filter(function (_ref8) {
235
307
  var key = _ref8.key;
236
308
  return copyProps.indexOf(key.name) !== -1;
237
309
  }).forEach(function (_ref9, i) {
238
- var _value$quasis$, _value$quasis$$value;
239
-
240
310
  var key = _ref9.key,
241
311
  value = _ref9.value;
312
+ // By default, the value is just the string value of the object property.
313
+ var valueToExtract = value.value;
242
314
 
243
315
  if (key.name === "comment" && !t.isStringLiteral(value)) {
244
- throw path.get("properties.".concat(i, ".value")).buildCodeFrameError("Only strings are supported as comments.");
316
+ // Comments can be single or multi-line strings.
317
+ var errorIfNotAString = path.get("properties.".concat(i, ".value")).buildCodeFrameError("Only strings are supported as comments.");
318
+
319
+ if (t.isBinaryExpression(value)) {
320
+ valueToExtract = extractStringContatentation(t, value, errorIfNotAString);
321
+ } else {
322
+ throw errorIfNotAString;
323
+ }
324
+ } else if (key.name === "id") {
325
+ var isIdLiteral = !value.value && t.isTemplateLiteral(value);
326
+
327
+ if (isIdLiteral) {
328
+ var _value$quasis$, _value$quasis$$value;
329
+
330
+ valueToExtract = value === null || value === void 0 ? void 0 : (_value$quasis$ = value.quasis[0]) === null || _value$quasis$ === void 0 ? void 0 : (_value$quasis$$value = _value$quasis$.value) === null || _value$quasis$$value === void 0 ? void 0 : _value$quasis$$value.cooked;
331
+ }
245
332
  }
246
333
 
247
- var isIdLiteral = !value.value && key.name === "id" && t.isTemplateLiteral(value);
248
- props[key.name] = isIdLiteral ? value === null || value === void 0 ? void 0 : (_value$quasis$ = value.quasis[0]) === null || _value$quasis$ === void 0 ? void 0 : (_value$quasis$$value = _value$quasis$.value) === null || _value$quasis$$value === void 0 ? void 0 : _value$quasis$$value.cooked : value.value;
334
+ props[key.name] = valueToExtract;
249
335
  });
250
336
  collectMessage(path, file, props);
251
337
  }
@@ -305,7 +391,7 @@ function _default(_ref2) {
305
391
  catalog[key] = value;
306
392
  });
307
393
 
308
- _fs.default.writeFileSync(catalogFilename, JSON.stringify(catalog, null, 2));
394
+ _fs.default.writeFileSync(catalogFilename, JSON.stringify(catalog, mapReplacer, 2));
309
395
  }
310
396
  };
311
397
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/babel-plugin-extract-messages",
3
- "version": "3.12.0",
3
+ "version": "3.13.2",
4
4
  "description": "Babel plugin for collecting messages from source code for internationalization",
5
5
  "main": "index.js",
6
6
  "author": {
@@ -30,7 +30,7 @@
30
30
  "dependencies": {
31
31
  "@babel/generator": "^7.11.6",
32
32
  "@babel/runtime": "^7.11.2",
33
- "@lingui/conf": "^3.12.0",
33
+ "@lingui/conf": "^3.13.2",
34
34
  "mkdirp": "^1.0.4"
35
35
  }
36
36
  }