@lingui/macro 3.14.0 → 3.16.0

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.
package/macroJsx.js DELETED
@@ -1,360 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
-
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- exports.default = void 0;
11
-
12
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
-
14
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
-
16
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
-
18
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
19
-
20
- var R = _interopRequireWildcard(require("ramda"));
21
-
22
- var _icu = _interopRequireDefault(require("./icu"));
23
-
24
- var _utils = require("./utils");
25
-
26
- var _constants = require("./constants");
27
-
28
- function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
29
-
30
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
31
-
32
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
33
-
34
- var pluralRuleRe = /(_[\d\w]+|zero|one|two|few|many|other)/;
35
-
36
- var jsx2icuExactChoice = function jsx2icuExactChoice(value) {
37
- return value.replace(/_(\d+)/, "=$1").replace(/_(\w+)/, "$1");
38
- }; // replace whitespace before/after newline with single space
39
-
40
-
41
- var keepSpaceRe = /\s*(?:\r\n|\r|\n)+\s*/g; // remove whitespace before/after tag or expression
42
-
43
- var stripAroundTagsRe = /(?:([>}])(?:\r\n|\r|\n)+\s*|(?:\r\n|\r|\n)+\s*(?=[<{]))/g;
44
-
45
- function maybeNodeValue(node) {
46
- if (!node) return null;
47
- if (node.type === "StringLiteral") return node.value;
48
- if (node.type === "JSXAttribute") return maybeNodeValue(node.value);
49
- if (node.type === "JSXExpressionContainer") return maybeNodeValue(node.expression);
50
- if (node.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis[0].value.raw;
51
- return null;
52
- }
53
-
54
- function normalizeWhitespace(text) {
55
- return text.replace(stripAroundTagsRe, "$1").replace(keepSpaceRe, " ") // keep escaped newlines
56
- .replace(/\\n/g, "\n").replace(/\\s/g, " ") // we remove trailing whitespace inside Plural
57
- .replace(/(\s+})/gm, "}").trim();
58
- }
59
-
60
- var MacroJSX = /*#__PURE__*/function () {
61
- function MacroJSX(_ref) {
62
- var _this = this;
63
-
64
- var types = _ref.types;
65
- (0, _classCallCheck2.default)(this, MacroJSX);
66
- (0, _defineProperty2.default)(this, "types", void 0);
67
- (0, _defineProperty2.default)(this, "expressionIndex", void 0);
68
- (0, _defineProperty2.default)(this, "elementIndex", void 0);
69
- (0, _defineProperty2.default)(this, "safeJsxAttribute", function (name, value) {
70
- // This handles quoted JSX attributes and html entities.
71
- return _this.types.jsxAttribute(_this.types.jsxIdentifier(name), _this.types.jsxExpressionContainer(_this.types.stringLiteral(value)));
72
- });
73
- (0, _defineProperty2.default)(this, "replacePath", function (path) {
74
- var tokens = _this.tokenizeNode(path.node);
75
-
76
- var messageFormat = new _icu.default();
77
-
78
- var _messageFormat$fromTo = messageFormat.fromTokens(tokens),
79
- messageRaw = _messageFormat$fromTo.message,
80
- values = _messageFormat$fromTo.values,
81
- jsxElements = _messageFormat$fromTo.jsxElements;
82
-
83
- var message = normalizeWhitespace(messageRaw);
84
-
85
- var _this$stripMacroAttri = _this.stripMacroAttributes(path.node),
86
- attributes = _this$stripMacroAttri.attributes,
87
- id = _this$stripMacroAttri.id,
88
- comment = _this$stripMacroAttri.comment,
89
- context = _this$stripMacroAttri.context;
90
-
91
- if (!id && !message) {
92
- return;
93
- } else if (id && id !== message) {
94
- // If `id` prop already exists and generated ID is different,
95
- // add it as a `default` prop
96
- attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.ID), _this.types.stringLiteral(id)));
97
-
98
- if (process.env.NODE_ENV !== "production") {
99
- if (message) {
100
- attributes.push(_this.safeJsxAttribute(_constants.MESSAGE, message));
101
- }
102
- }
103
- } else {
104
- attributes.push(_this.safeJsxAttribute(_constants.ID, message));
105
- }
106
-
107
- if (process.env.NODE_ENV !== "production") {
108
- if (comment) {
109
- attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
110
- }
111
- }
112
-
113
- if (context) {
114
- attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.CONTEXT), _this.types.stringLiteral(context)));
115
- } // Parameters for variable substitution
116
-
117
-
118
- var valuesObject = Object.keys(values).map(function (key) {
119
- return _this.types.objectProperty(_this.types.identifier(key), values[key]);
120
- });
121
-
122
- if (valuesObject.length) {
123
- attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("values"), _this.types.jsxExpressionContainer(_this.types.objectExpression(valuesObject))));
124
- } // Inline elements
125
-
126
-
127
- if (Object.keys(jsxElements).length) {
128
- attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier("components"), _this.types.jsxExpressionContainer(_this.types.objectExpression(Object.keys(jsxElements).map(function (key) {
129
- return _this.types.objectProperty(_this.types.identifier(key), jsxElements[key]);
130
- })))));
131
- }
132
-
133
- var newNode = _this.types.jsxElement(_this.types.jsxOpeningElement(_this.types.jsxIdentifier("Trans"), attributes,
134
- /*selfClosing*/
135
- true),
136
- /*closingElement*/
137
- null,
138
- /*children*/
139
- [],
140
- /*selfClosing*/
141
- true);
142
-
143
- newNode.loc = path.node.loc; // @ts-ignore
144
-
145
- path.replaceWith(newNode);
146
- });
147
- (0, _defineProperty2.default)(this, "attrName", function (names) {
148
- var exclude = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
149
- var namesRe = new RegExp("^(" + names.join("|") + ")$");
150
- return function (attr) {
151
- return exclude ? !namesRe.test(attr.name.name) : namesRe.test(attr.name.name);
152
- };
153
- });
154
- (0, _defineProperty2.default)(this, "stripMacroAttributes", function (node) {
155
- var attributes = node.openingElement.attributes;
156
- var id = attributes.filter(_this.attrName([_constants.ID]))[0];
157
- var message = attributes.filter(_this.attrName([_constants.MESSAGE]))[0];
158
- var comment = attributes.filter(_this.attrName([_constants.COMMENT]))[0];
159
- var context = attributes.filter(_this.attrName([_constants.CONTEXT]))[0];
160
- var reserved = [_constants.ID, _constants.MESSAGE, _constants.COMMENT, _constants.CONTEXT];
161
-
162
- if (_this.isI18nComponent(node)) {// no reserved prop names
163
- } else if (_this.isChoiceComponent(node)) {
164
- reserved = [].concat((0, _toConsumableArray2.default)(reserved), ["_\\w+", "_\\d+", "zero", "one", "two", "few", "many", "other", "value", "offset"]);
165
- }
166
-
167
- return {
168
- id: maybeNodeValue(id),
169
- message: maybeNodeValue(message),
170
- comment: maybeNodeValue(comment),
171
- context: maybeNodeValue(context),
172
- attributes: attributes.filter(_this.attrName(reserved, true))
173
- };
174
- });
175
- (0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
176
- if (_this.isI18nComponent(node)) {
177
- // t
178
- return _this.tokenizeTrans(node);
179
- } else if (_this.isChoiceComponent(node)) {
180
- // plural, select and selectOrdinal
181
- return _this.tokenizeChoiceComponent(node);
182
- } else if (_this.types.isJSXElement(node)) {
183
- return _this.tokenizeElement(node);
184
- } else {
185
- return _this.tokenizeExpression(node);
186
- }
187
- });
188
- (0, _defineProperty2.default)(this, "tokenizeTrans", function (node) {
189
- return R.flatten(node.children.map(function (child) {
190
- return _this.tokenizeChildren(child);
191
- }).filter(Boolean));
192
- });
193
- (0, _defineProperty2.default)(this, "tokenizeChildren", function (node) {
194
- if (_this.types.isJSXExpressionContainer(node)) {
195
- var exp = node.expression;
196
-
197
- if (_this.types.isStringLiteral(exp)) {
198
- // Escape forced newlines to keep them in message.
199
- return {
200
- type: "text",
201
- value: exp.value.replace(/\n/g, "\\n")
202
- };
203
- } else if (_this.types.isTemplateLiteral(exp)) {
204
- var tokenize = R.pipe( // Don"t output tokens without text.
205
- R.evolve({
206
- quasis: R.map(function (text) {
207
- // if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
208
- // This regex will detect if a string contains unicode chars, when they're we should interpolate them
209
- // why? because platforms like react native doesn't parse them, just doing a JSON.parse makes them UTF-8 friendly
210
- var value = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g.test(text.value.raw) ? text.value.cooked : text.value.raw;
211
- if (value === "") return null;
212
- return _this.tokenizeText(_this.clearBackslashes(value));
213
- }),
214
- expressions: R.map(function (exp) {
215
- return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
216
- })
217
- }), function (exp) {
218
- return (0, _utils.zip)(exp.quasis, exp.expressions);
219
- }, // @ts-ignore
220
- R.flatten, R.filter(Boolean));
221
- return tokenize(exp);
222
- } else if (_this.types.isJSXElement(exp)) {
223
- return _this.tokenizeNode(exp);
224
- } else {
225
- return _this.tokenizeExpression(exp);
226
- }
227
- } else if (_this.types.isJSXElement(node)) {
228
- return _this.tokenizeNode(node);
229
- } else if (_this.types.isJSXSpreadChild(node)) {// just do nothing
230
- } else if (_this.types.isJSXText(node)) {
231
- return _this.tokenizeText(node.value);
232
- } else {
233
- return _this.tokenizeText(node.value);
234
- }
235
- });
236
- (0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
237
- var element = node.openingElement;
238
- var format = element.name.name.toLowerCase();
239
- var props = element.attributes.filter(_this.attrName([_constants.ID, _constants.COMMENT, _constants.MESSAGE, _constants.CONTEXT, "key", // we remove <Trans /> react props that are not useful for translation
240
- "render", "component", "components"], true));
241
- var token = {
242
- type: "arg",
243
- format: format,
244
- name: null,
245
- value: undefined,
246
- options: {
247
- offset: undefined
248
- }
249
- };
250
-
251
- var _iterator = _createForOfIteratorHelper(props),
252
- _step;
253
-
254
- try {
255
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
256
- var attr = _step.value;
257
- var name = attr.name.name;
258
-
259
- if (name === "value") {
260
- var exp = _this.types.isLiteral(attr.value) ? attr.value : attr.value.expression;
261
- token.name = _this.expressionToArgument(exp);
262
- token.value = exp;
263
- } else if (format !== "select" && name === "offset") {
264
- // offset is static parameter, so it must be either string or number
265
- token.options.offset = _this.types.isStringLiteral(attr.value) ? attr.value.value : attr.value.expression.value;
266
- } else {
267
- var value = void 0;
268
-
269
- if (_this.types.isStringLiteral(attr.value)) {
270
- value = attr.value.extra.raw.replace(/(["'])(.*)\1/, "$2");
271
- } else {
272
- value = _this.tokenizeChildren(attr.value);
273
- }
274
-
275
- if (pluralRuleRe.test(name)) {
276
- token.options[jsx2icuExactChoice(name)] = value;
277
- } else {
278
- token.options[name] = value;
279
- }
280
- }
281
- }
282
- } catch (err) {
283
- _iterator.e(err);
284
- } finally {
285
- _iterator.f();
286
- }
287
-
288
- return token;
289
- });
290
- (0, _defineProperty2.default)(this, "tokenizeElement", function (node) {
291
- // !!! Important: Calculate element index before traversing children.
292
- // That way outside elements are numbered before inner elements. (...and it looks pretty).
293
- var name = _this.elementIndex();
294
-
295
- var children = node.children.map(function (child) {
296
- return _this.tokenizeChildren(child);
297
- }).filter(Boolean);
298
- node.children = [];
299
- node.openingElement.selfClosing = true;
300
- return {
301
- type: "element",
302
- name: name,
303
- value: node,
304
- children: children
305
- };
306
- });
307
- (0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
308
- return {
309
- type: "arg",
310
- name: _this.expressionToArgument(node),
311
- value: node
312
- };
313
- });
314
- (0, _defineProperty2.default)(this, "tokenizeText", function (value) {
315
- return {
316
- type: "text",
317
- value: value
318
- };
319
- });
320
- (0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
321
- return _this.types.isIdentifier(exp) ? exp.name : _this.expressionIndex();
322
- });
323
- (0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
324
- return _this.types.isIdentifier(node, {
325
- name: name
326
- });
327
- });
328
- (0, _defineProperty2.default)(this, "isI18nComponent", function (node) {
329
- var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Trans";
330
- return _this.types.isJSXElement(node) && _this.types.isJSXIdentifier(node.openingElement.name, {
331
- name: name
332
- });
333
- });
334
- (0, _defineProperty2.default)(this, "isChoiceComponent", function (node) {
335
- return _this.isI18nComponent(node, "Plural") || _this.isI18nComponent(node, "Select") || _this.isI18nComponent(node, "SelectOrdinal");
336
- });
337
- this.types = types;
338
- this.expressionIndex = (0, _utils.makeCounter)();
339
- this.elementIndex = (0, _utils.makeCounter)();
340
- }
341
-
342
- (0, _createClass2.default)(MacroJSX, [{
343
- key: "clearBackslashes",
344
-
345
- /**
346
- * We clean '//\` ' to just '`'
347
- * */
348
- value: function clearBackslashes(value) {
349
- // if not we replace the extra scaped literals
350
- return value.replace(/\\`/g, "`");
351
- }
352
- /**
353
- * Custom matchers
354
- */
355
-
356
- }]);
357
- return MacroJSX;
358
- }();
359
-
360
- exports.default = MacroJSX;
package/utils.js DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.zip = zip;
9
- exports.makeCounter = void 0;
10
-
11
- var R = _interopRequireWildcard(require("ramda"));
12
-
13
- /**
14
- * Custom zip method which takes length of the larger array
15
- * (usually zip functions use the `smaller` length, discarding values in larger array)
16
- */
17
- function zip(a, b) {
18
- return R.range(0, Math.max(a.length, b.length)).map(function (index) {
19
- return [a[index], b[index]];
20
- });
21
- }
22
-
23
- var makeCounter = function makeCounter() {
24
- var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
25
- return function () {
26
- return index++;
27
- };
28
- };
29
-
30
- exports.makeCounter = makeCounter;