@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/macroJs.js DELETED
@@ -1,384 +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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
-
14
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
-
16
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
-
18
- var R = _interopRequireWildcard(require("ramda"));
19
-
20
- var _icu = _interopRequireDefault(require("./icu"));
21
-
22
- var _utils = require("./utils");
23
-
24
- var _constants = require("./constants");
25
-
26
- 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; } } }; }
27
-
28
- 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); }
29
-
30
- 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; }
31
-
32
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
33
-
34
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
35
-
36
- var keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g;
37
- var keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g;
38
-
39
- function normalizeWhitespace(text) {
40
- return text.replace(keepSpaceRe, " ").replace(keepNewLineRe, "\n").trim();
41
- }
42
-
43
- var MacroJs = /*#__PURE__*/function () {
44
- // Babel Types
45
- // Identifier of i18n object
46
- // Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
47
- function MacroJs(_ref, _ref2) {
48
- var _this = this;
49
-
50
- var types = _ref.types;
51
- var i18nImportName = _ref2.i18nImportName;
52
- (0, _classCallCheck2.default)(this, MacroJs);
53
- (0, _defineProperty2.default)(this, "types", void 0);
54
- (0, _defineProperty2.default)(this, "i18nImportName", void 0);
55
- (0, _defineProperty2.default)(this, "_expressionIndex", void 0);
56
- (0, _defineProperty2.default)(this, "replacePathWithMessage", function (path, _ref3, linguiInstance) {
57
- var id = _ref3.id,
58
- message = _ref3.message,
59
- values = _ref3.values,
60
- comment = _ref3.comment;
61
- var args = [];
62
- var options = [];
63
- var messageNode = isString(message) ? _this.types.stringLiteral(message) : message;
64
-
65
- if (id) {
66
- args.push(_this.types.stringLiteral(id));
67
-
68
- if (process.env.NODE_ENV !== "production") {
69
- options.push(_this.types.objectProperty(_this.types.identifier(_constants.MESSAGE), messageNode));
70
- }
71
- } else {
72
- args.push(messageNode);
73
- }
74
-
75
- if (comment) {
76
- options.push(_this.types.objectProperty(_this.types.identifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
77
- }
78
-
79
- if (Object.keys(values).length || options.length) {
80
- var valuesObject = Object.keys(values).map(function (key) {
81
- return _this.types.objectProperty(_this.types.identifier(key), values[key]);
82
- });
83
- args.push(_this.types.objectExpression(valuesObject));
84
- }
85
-
86
- if (options.length) {
87
- args.push(_this.types.objectExpression(options));
88
- }
89
-
90
- var newNode = _this.types.callExpression(_this.types.memberExpression(linguiInstance !== null && linguiInstance !== void 0 ? linguiInstance : _this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), args); // preserve line number
91
-
92
-
93
- newNode.loc = path.node.loc;
94
- path.addComment("leading", _constants.EXTRACT_MARK); // @ts-ignore
95
-
96
- path.replaceWith(newNode);
97
- });
98
- (0, _defineProperty2.default)(this, "replacePath", function (path) {
99
- // reset the expression counter
100
- _this._expressionIndex = (0, _utils.makeCounter)();
101
-
102
- if (_this.isDefineMessage(path.node)) {
103
- _this.replaceDefineMessage(path);
104
-
105
- return true;
106
- } // t(i18nInstance)`Message` -> i18nInstance._('Message')
107
-
108
-
109
- if (_this.types.isCallExpression(path.node) && _this.types.isTaggedTemplateExpression(path.parentPath.node) && _this.types.isIdentifier(path.node.arguments[0]) && _this.isIdentifier(path.node.callee, "t")) {
110
- // Use the first argument as i18n instance instead of the default i18n instance
111
- var i18nInstance = path.node.arguments[0];
112
-
113
- var _tokens = _this.tokenizeNode(path.parentPath.node);
114
-
115
- var _messageFormat = new _icu.default();
116
-
117
- var _messageFormat$fromTo = _messageFormat.fromTokens(_tokens),
118
- _messageRaw = _messageFormat$fromTo.message,
119
- _values = _messageFormat$fromTo.values,
120
- _id = _messageFormat$fromTo.id,
121
- _comment = _messageFormat$fromTo.comment;
122
-
123
- var _message = normalizeWhitespace(_messageRaw);
124
-
125
- _this.replacePathWithMessage(path.parentPath, {
126
- id: _id,
127
- message: _message,
128
- values: _values,
129
- comment: _comment
130
- }, i18nInstance);
131
-
132
- return false;
133
- } // t(i18nInstance)(messageDescriptor) -> i18nInstance._(messageDescriptor)
134
-
135
-
136
- if (_this.types.isCallExpression(path.node) && _this.types.isCallExpression(path.parentPath.node) && _this.types.isIdentifier(path.node.arguments[0]) && _this.isIdentifier(path.node.callee, "t")) {
137
- var _i18nInstance = path.node.arguments[0];
138
-
139
- _this.replaceTAsFunction(path.parentPath, _i18nInstance);
140
-
141
- return false;
142
- }
143
-
144
- if (_this.types.isCallExpression(path.node) && _this.isIdentifier(path.node.callee, "t")) {
145
- _this.replaceTAsFunction(path);
146
-
147
- return true;
148
- }
149
-
150
- var tokens = _this.tokenizeNode(path.node);
151
-
152
- var messageFormat = new _icu.default();
153
-
154
- var _messageFormat$fromTo2 = messageFormat.fromTokens(tokens),
155
- messageRaw = _messageFormat$fromTo2.message,
156
- values = _messageFormat$fromTo2.values,
157
- id = _messageFormat$fromTo2.id,
158
- comment = _messageFormat$fromTo2.comment;
159
-
160
- var message = normalizeWhitespace(messageRaw);
161
-
162
- _this.replacePathWithMessage(path, {
163
- id: id,
164
- message: message,
165
- values: values,
166
- comment: comment
167
- });
168
-
169
- return true;
170
- });
171
- (0, _defineProperty2.default)(this, "replaceDefineMessage", function (path) {
172
- // reset the expression counter
173
- _this._expressionIndex = (0, _utils.makeCounter)();
174
-
175
- var descriptor = _this.processDescriptor(path.node.arguments[0]);
176
-
177
- path.replaceWith(descriptor);
178
- });
179
- (0, _defineProperty2.default)(this, "replaceTAsFunction", function (path, linguiInstance) {
180
- var descriptor = _this.processDescriptor(path.node.arguments[0]);
181
-
182
- var newNode = _this.types.callExpression(_this.types.memberExpression(linguiInstance !== null && linguiInstance !== void 0 ? linguiInstance : _this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), [descriptor]);
183
-
184
- path.replaceWith(newNode);
185
- });
186
- (0, _defineProperty2.default)(this, "processDescriptor", function (descriptor) {
187
- _this.types.addComment(descriptor, "leading", _constants.EXTRACT_MARK);
188
-
189
- var messageIndex = descriptor.properties.findIndex(function (property) {
190
- return property.key.name === _constants.MESSAGE;
191
- });
192
-
193
- if (messageIndex === -1) {
194
- return descriptor;
195
- } // if there's `message` property, replace macros with formatted message
196
-
197
-
198
- var node = descriptor.properties[messageIndex]; // Inside message descriptor the `t` macro in `message` prop is optional.
199
- // Template strings are always processed as if they were wrapped by `t`.
200
-
201
- var tokens = _this.types.isTemplateLiteral(node.value) ? _this.tokenizeTemplateLiteral(node.value) : _this.tokenizeNode(node.value, true);
202
- var messageNode = node.value;
203
-
204
- if (tokens != null) {
205
- var messageFormat = new _icu.default();
206
-
207
- var _messageFormat$fromTo3 = messageFormat.fromTokens(tokens),
208
- messageRaw = _messageFormat$fromTo3.message,
209
- values = _messageFormat$fromTo3.values;
210
-
211
- var message = normalizeWhitespace(messageRaw);
212
- messageNode = _this.types.stringLiteral(message);
213
-
214
- _this.addValues(descriptor.properties, values);
215
- } // Don't override custom ID
216
-
217
-
218
- var hasId = descriptor.properties.findIndex(function (property) {
219
- return property.key.name === _constants.ID;
220
- }) !== -1;
221
- descriptor.properties[messageIndex] = _this.types.objectProperty(_this.types.identifier(hasId ? _constants.MESSAGE : _constants.ID), messageNode);
222
- return descriptor;
223
- });
224
- (0, _defineProperty2.default)(this, "addValues", function (obj, values) {
225
- var valuesObject = Object.keys(values).map(function (key) {
226
- return _this.types.objectProperty(_this.types.identifier(key), values[key]);
227
- });
228
- if (!valuesObject.length) return;
229
- obj.push(_this.types.objectProperty(_this.types.identifier("values"), _this.types.objectExpression(valuesObject)));
230
- });
231
- (0, _defineProperty2.default)(this, "tokenizeNode", function (node) {
232
- var ignoreExpression = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
233
-
234
- if (_this.isI18nMethod(node)) {
235
- // t
236
- return _this.tokenizeTemplateLiteral(node);
237
- } else if (_this.isChoiceMethod(node)) {
238
- // plural, select and selectOrdinal
239
- return [_this.tokenizeChoiceComponent(node)]; // } else if (isFormatMethod(node.callee)) {
240
- // // date, number
241
- // return transformFormatMethod(node, file, props, root)
242
- } else if (!ignoreExpression) {
243
- return _this.tokenizeExpression(node);
244
- }
245
- });
246
- (0, _defineProperty2.default)(this, "tokenizeTemplateLiteral", function (node) {
247
- var tokenize = R.pipe(R.evolve({
248
- quasis: R.map(function (text) {
249
- // Don't output tokens without text.
250
- // if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
251
- // This regex will detect if a string contains unicode chars, when they're we should interpolate them
252
- // why? because platforms like react native doesn't parse them, just doing a JSON.parse makes them UTF-8 friendly
253
- 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;
254
- if (value === "") return null;
255
- return {
256
- type: "text",
257
- value: _this.clearBackslashes(value)
258
- };
259
- }),
260
- expressions: R.map(function (exp) {
261
- return _this.types.isCallExpression(exp) ? _this.tokenizeNode(exp) : _this.tokenizeExpression(exp);
262
- })
263
- }), function (exp) {
264
- return (0, _utils.zip)(exp.quasis, exp.expressions);
265
- }, R.flatten, R.filter(Boolean));
266
- return tokenize(_this.types.isTaggedTemplateExpression(node) ? node.quasi : node);
267
- });
268
- (0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
269
- var format = node.callee.name.toLowerCase();
270
-
271
- var token = _objectSpread(_objectSpread({}, _this.tokenizeExpression(node.arguments[0])), {}, {
272
- format: format,
273
- options: {
274
- offset: undefined
275
- }
276
- });
277
-
278
- var props = node.arguments[1].properties;
279
-
280
- var _iterator = _createForOfIteratorHelper(props),
281
- _step;
282
-
283
- try {
284
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
285
- var attr = _step.value;
286
- var key = attr.key; // name is either:
287
- // NumericLiteral => convert to `={number}`
288
- // StringLiteral => key.value
289
- // Literal => key.name
290
-
291
- var name = _this.types.isNumericLiteral(key) ? "=".concat(key.value) : key.name || key.value;
292
-
293
- if (format !== "select" && name === "offset") {
294
- token.options.offset = attr.value.value;
295
- } else {
296
- var value = void 0;
297
-
298
- if (_this.types.isTemplateLiteral(attr.value)) {
299
- value = _this.tokenizeTemplateLiteral(attr.value);
300
- } else if (_this.types.isCallExpression(attr.value)) {
301
- value = _this.tokenizeNode(attr.value);
302
- } else {
303
- value = attr.value.value;
304
- }
305
-
306
- token.options[name] = value;
307
- }
308
- }
309
- } catch (err) {
310
- _iterator.e(err);
311
- } finally {
312
- _iterator.f();
313
- }
314
-
315
- return token;
316
- });
317
- (0, _defineProperty2.default)(this, "tokenizeExpression", function (node) {
318
- if (_this.isArg(node)) {
319
- return {
320
- type: "arg",
321
- name: node.arguments[0].value
322
- };
323
- }
324
-
325
- return {
326
- type: "arg",
327
- name: _this.expressionToArgument(node),
328
- value: node
329
- };
330
- });
331
- (0, _defineProperty2.default)(this, "expressionToArgument", function (exp) {
332
- if (_this.types.isIdentifier(exp)) {
333
- return exp.name;
334
- } else if (_this.types.isStringLiteral(exp)) {
335
- return exp.value;
336
- } else {
337
- return _this._expressionIndex();
338
- }
339
- });
340
- (0, _defineProperty2.default)(this, "isIdentifier", function (node, name) {
341
- return _this.types.isIdentifier(node, {
342
- name: name
343
- });
344
- });
345
- (0, _defineProperty2.default)(this, "isDefineMessage", function (node) {
346
- return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "defineMessage");
347
- });
348
- (0, _defineProperty2.default)(this, "isArg", function (node) {
349
- return _this.types.isCallExpression(node) && _this.isIdentifier(node.callee, "arg");
350
- });
351
- (0, _defineProperty2.default)(this, "isI18nMethod", function (node) {
352
- return _this.isIdentifier(node.tag, "t") || _this.types.isCallExpression(node.tag) && _this.isIdentifier(node.tag.callee, "t");
353
- });
354
- (0, _defineProperty2.default)(this, "isChoiceMethod", function (node) {
355
- return _this.types.isCallExpression(node) && (_this.isIdentifier(node.callee, "plural") || _this.isIdentifier(node.callee, "select") || _this.isIdentifier(node.callee, "selectOrdinal"));
356
- });
357
- this.types = types;
358
- this.i18nImportName = i18nImportName;
359
- this._expressionIndex = (0, _utils.makeCounter)();
360
- }
361
-
362
- (0, _createClass2.default)(MacroJs, [{
363
- key: "clearBackslashes",
364
-
365
- /**
366
- * We clean '//\` ' to just '`'
367
- */
368
- value: function clearBackslashes(value) {
369
- // if not we replace the extra scaped literals
370
- return value.replace(/\\`/g, "`");
371
- }
372
- /**
373
- * Custom matchers
374
- */
375
-
376
- }]);
377
- return MacroJs;
378
- }();
379
-
380
- exports.default = MacroJs;
381
-
382
- var isString = function isString(s) {
383
- return typeof s === "string";
384
- };