@formatjs/icu-messageformat-parser 2.7.2 → 2.7.3
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/LICENSE.md +1 -1
- package/lib/date-time-pattern-generator.js +6 -10
- package/lib/error.js +2 -5
- package/lib/index.js +15 -19
- package/lib/manipulator.js +8 -12
- package/lib/no-parser.js +3 -8
- package/lib/parser.js +57 -60
- package/lib/printer.js +15 -21
- package/lib/regex.generated.js +2 -5
- package/lib/time-data.generated.js +1 -4
- package/lib/types.js +17 -33
- package/package.json +3 -3
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2023 FormatJS
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getBestPattern = void 0;
|
|
4
|
-
var time_data_generated_1 = require("./time-data.generated");
|
|
1
|
+
import { timeData } from './time-data.generated';
|
|
5
2
|
/**
|
|
6
3
|
* Returns the best matching date time pattern if a date time skeleton
|
|
7
4
|
* pattern is provided with a locale. Follows the Unicode specification:
|
|
@@ -9,7 +6,7 @@ var time_data_generated_1 = require("./time-data.generated");
|
|
|
9
6
|
* @param skeleton date time skeleton pattern that possibly includes j, J or C
|
|
10
7
|
* @param locale
|
|
11
8
|
*/
|
|
12
|
-
function getBestPattern(skeleton, locale) {
|
|
9
|
+
export function getBestPattern(skeleton, locale) {
|
|
13
10
|
var skeletonCopy = '';
|
|
14
11
|
for (var patternPos = 0; patternPos < skeleton.length; patternPos++) {
|
|
15
12
|
var patternChar = skeleton.charAt(patternPos);
|
|
@@ -43,7 +40,6 @@ function getBestPattern(skeleton, locale) {
|
|
|
43
40
|
}
|
|
44
41
|
return skeletonCopy;
|
|
45
42
|
}
|
|
46
|
-
exports.getBestPattern = getBestPattern;
|
|
47
43
|
/**
|
|
48
44
|
* Maps the [hour cycle type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle)
|
|
49
45
|
* of the given `locale` to the corresponding time pattern.
|
|
@@ -79,9 +75,9 @@ function getDefaultHourSymbolFromLocale(locale) {
|
|
|
79
75
|
if (languageTag !== 'root') {
|
|
80
76
|
regionTag = locale.maximize().region;
|
|
81
77
|
}
|
|
82
|
-
var hourCycles =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
var hourCycles = timeData[regionTag || ''] ||
|
|
79
|
+
timeData[languageTag || ''] ||
|
|
80
|
+
timeData["".concat(languageTag, "-001")] ||
|
|
81
|
+
timeData['001'];
|
|
86
82
|
return hourCycles[0];
|
|
87
83
|
}
|
package/lib/error.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ErrorKind = void 0;
|
|
4
|
-
var ErrorKind;
|
|
1
|
+
export var ErrorKind;
|
|
5
2
|
(function (ErrorKind) {
|
|
6
3
|
/** Argument is unclosed (e.g. `{0`) */
|
|
7
4
|
ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
|
|
@@ -63,4 +60,4 @@ var ErrorKind;
|
|
|
63
60
|
ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
|
|
64
61
|
/** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
|
|
65
62
|
ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
|
|
66
|
-
})(ErrorKind || (
|
|
63
|
+
})(ErrorKind || (ErrorKind = {}));
|
package/lib/index.js
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var error_1 = require("./error");
|
|
6
|
-
var parser_1 = require("./parser");
|
|
7
|
-
var types_1 = require("./types");
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { ErrorKind } from './error';
|
|
3
|
+
import { Parser } from './parser';
|
|
4
|
+
import { isDateElement, isDateTimeSkeleton, isNumberElement, isNumberSkeleton, isPluralElement, isSelectElement, isTagElement, isTimeElement, } from './types';
|
|
8
5
|
function pruneLocation(els) {
|
|
9
6
|
els.forEach(function (el) {
|
|
10
7
|
delete el.location;
|
|
11
|
-
if (
|
|
8
|
+
if (isSelectElement(el) || isPluralElement(el)) {
|
|
12
9
|
for (var k in el.options) {
|
|
13
10
|
delete el.options[k].location;
|
|
14
11
|
pruneLocation(el.options[k].value);
|
|
15
12
|
}
|
|
16
13
|
}
|
|
17
|
-
else if (
|
|
14
|
+
else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
|
|
18
15
|
delete el.style.location;
|
|
19
16
|
}
|
|
20
|
-
else if ((
|
|
21
|
-
|
|
17
|
+
else if ((isDateElement(el) || isTimeElement(el)) &&
|
|
18
|
+
isDateTimeSkeleton(el.style)) {
|
|
22
19
|
delete el.style.location;
|
|
23
20
|
}
|
|
24
|
-
else if (
|
|
21
|
+
else if (isTagElement(el)) {
|
|
25
22
|
pruneLocation(el.children);
|
|
26
23
|
}
|
|
27
24
|
});
|
|
28
25
|
}
|
|
29
|
-
function parse(message, opts) {
|
|
26
|
+
export function parse(message, opts) {
|
|
30
27
|
if (opts === void 0) { opts = {}; }
|
|
31
|
-
opts =
|
|
32
|
-
var result = new
|
|
28
|
+
opts = __assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
|
|
29
|
+
var result = new Parser(message, opts).parse();
|
|
33
30
|
if (result.err) {
|
|
34
|
-
var error = SyntaxError(
|
|
31
|
+
var error = SyntaxError(ErrorKind[result.err.kind]);
|
|
35
32
|
// @ts-expect-error Assign to error object
|
|
36
33
|
error.location = result.err.location;
|
|
37
34
|
// @ts-expect-error Assign to error object
|
|
@@ -43,7 +40,6 @@ function parse(message, opts) {
|
|
|
43
40
|
}
|
|
44
41
|
return result.val;
|
|
45
42
|
}
|
|
46
|
-
|
|
47
|
-
tslib_1.__exportStar(require("./types"), exports);
|
|
43
|
+
export * from './types';
|
|
48
44
|
// only for testing
|
|
49
|
-
|
|
45
|
+
export var _Parser = Parser;
|
package/lib/manipulator.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.hoistSelectors = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var types_1 = require("./types");
|
|
1
|
+
import { __spreadArray } from "tslib";
|
|
2
|
+
import { isPluralElement, isSelectElement, isTagElement, } from './types';
|
|
6
3
|
function cloneDeep(obj) {
|
|
7
4
|
if (Array.isArray(obj)) {
|
|
8
5
|
// @ts-expect-error meh
|
|
9
|
-
return
|
|
6
|
+
return __spreadArray([], obj.map(cloneDeep), true);
|
|
10
7
|
}
|
|
11
8
|
if (obj !== null && typeof obj === 'object') {
|
|
12
9
|
// @ts-expect-error meh
|
|
@@ -23,7 +20,7 @@ function hoistPluralOrSelectElement(ast, el, positionToInject) {
|
|
|
23
20
|
var cloned = cloneDeep(el);
|
|
24
21
|
var options = cloned.options;
|
|
25
22
|
cloned.options = Object.keys(options).reduce(function (all, k) {
|
|
26
|
-
var newValue = hoistSelectors(
|
|
23
|
+
var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, positionToInject), true), options[k].value, true), ast.slice(positionToInject + 1), true));
|
|
27
24
|
all[k] = {
|
|
28
25
|
value: newValue,
|
|
29
26
|
};
|
|
@@ -32,14 +29,14 @@ function hoistPluralOrSelectElement(ast, el, positionToInject) {
|
|
|
32
29
|
return cloned;
|
|
33
30
|
}
|
|
34
31
|
function isPluralOrSelectElement(el) {
|
|
35
|
-
return
|
|
32
|
+
return isPluralElement(el) || isSelectElement(el);
|
|
36
33
|
}
|
|
37
34
|
function findPluralOrSelectElement(ast) {
|
|
38
35
|
return !!ast.find(function (el) {
|
|
39
36
|
if (isPluralOrSelectElement(el)) {
|
|
40
37
|
return true;
|
|
41
38
|
}
|
|
42
|
-
if (
|
|
39
|
+
if (isTagElement(el)) {
|
|
43
40
|
return findPluralOrSelectElement(el.children);
|
|
44
41
|
}
|
|
45
42
|
return false;
|
|
@@ -56,16 +53,15 @@ function findPluralOrSelectElement(ast) {
|
|
|
56
53
|
* sentences are not translator-friendly
|
|
57
54
|
* @param ast AST
|
|
58
55
|
*/
|
|
59
|
-
function hoistSelectors(ast) {
|
|
56
|
+
export function hoistSelectors(ast) {
|
|
60
57
|
for (var i = 0; i < ast.length; i++) {
|
|
61
58
|
var el = ast[i];
|
|
62
59
|
if (isPluralOrSelectElement(el)) {
|
|
63
60
|
return [hoistPluralOrSelectElement(ast, el, i)];
|
|
64
61
|
}
|
|
65
|
-
if (
|
|
62
|
+
if (isTagElement(el) && findPluralOrSelectElement([el])) {
|
|
66
63
|
throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option');
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
66
|
return ast;
|
|
70
67
|
}
|
|
71
|
-
exports.hoistSelectors = hoistSelectors;
|
package/lib/no-parser.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._Parser = exports.parse = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
function parse() {
|
|
1
|
+
export function parse() {
|
|
6
2
|
throw new Error("You're trying to format an uncompiled message with react-intl without parser, please import from 'react-intl' instead");
|
|
7
3
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports._Parser = undefined;
|
|
4
|
+
export * from './types';
|
|
5
|
+
export var _Parser = undefined;
|
package/lib/parser.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var _a;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(regex_generated_1.SPACE_SEPARATOR_REGEX.source, "*"));
|
|
12
|
-
var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(regex_generated_1.SPACE_SEPARATOR_REGEX.source, "*$"));
|
|
2
|
+
import { __assign } from "tslib";
|
|
3
|
+
import { ErrorKind } from './error';
|
|
4
|
+
import { SKELETON_TYPE, TYPE, } from './types';
|
|
5
|
+
import { SPACE_SEPARATOR_REGEX } from './regex.generated';
|
|
6
|
+
import { parseNumberSkeleton, parseNumberSkeletonFromString, parseDateTimeSkeleton, } from '@formatjs/icu-skeleton-parser';
|
|
7
|
+
import { getBestPattern } from './date-time-pattern-generator';
|
|
8
|
+
var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(SPACE_SEPARATOR_REGEX.source, "*"));
|
|
9
|
+
var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(SPACE_SEPARATOR_REGEX.source, "*$"));
|
|
13
10
|
function createLocation(start, end) {
|
|
14
11
|
return { start: start, end: end };
|
|
15
12
|
}
|
|
@@ -195,7 +192,7 @@ var Parser = /** @class */ (function () {
|
|
|
195
192
|
var position = this.clonePosition();
|
|
196
193
|
this.bump();
|
|
197
194
|
elements.push({
|
|
198
|
-
type:
|
|
195
|
+
type: TYPE.pound,
|
|
199
196
|
location: createLocation(position, this.clonePosition()),
|
|
200
197
|
});
|
|
201
198
|
}
|
|
@@ -207,7 +204,7 @@ var Parser = /** @class */ (function () {
|
|
|
207
204
|
break;
|
|
208
205
|
}
|
|
209
206
|
else {
|
|
210
|
-
return this.error(
|
|
207
|
+
return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
|
|
211
208
|
}
|
|
212
209
|
}
|
|
213
210
|
else if (char === 60 /* `<` */ &&
|
|
@@ -256,7 +253,7 @@ var Parser = /** @class */ (function () {
|
|
|
256
253
|
// Self closing tag
|
|
257
254
|
return {
|
|
258
255
|
val: {
|
|
259
|
-
type:
|
|
256
|
+
type: TYPE.literal,
|
|
260
257
|
value: "<".concat(tagName, "/>"),
|
|
261
258
|
location: createLocation(startPosition, this.clonePosition()),
|
|
262
259
|
},
|
|
@@ -273,20 +270,20 @@ var Parser = /** @class */ (function () {
|
|
|
273
270
|
var endTagStartPosition = this.clonePosition();
|
|
274
271
|
if (this.bumpIf('</')) {
|
|
275
272
|
if (this.isEOF() || !_isAlpha(this.char())) {
|
|
276
|
-
return this.error(
|
|
273
|
+
return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
|
|
277
274
|
}
|
|
278
275
|
var closingTagNameStartPosition = this.clonePosition();
|
|
279
276
|
var closingTagName = this.parseTagName();
|
|
280
277
|
if (tagName !== closingTagName) {
|
|
281
|
-
return this.error(
|
|
278
|
+
return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
|
|
282
279
|
}
|
|
283
280
|
this.bumpSpace();
|
|
284
281
|
if (!this.bumpIf('>')) {
|
|
285
|
-
return this.error(
|
|
282
|
+
return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
|
|
286
283
|
}
|
|
287
284
|
return {
|
|
288
285
|
val: {
|
|
289
|
-
type:
|
|
286
|
+
type: TYPE.tag,
|
|
290
287
|
value: tagName,
|
|
291
288
|
children: children,
|
|
292
289
|
location: createLocation(startPosition, this.clonePosition()),
|
|
@@ -295,11 +292,11 @@ var Parser = /** @class */ (function () {
|
|
|
295
292
|
};
|
|
296
293
|
}
|
|
297
294
|
else {
|
|
298
|
-
return this.error(
|
|
295
|
+
return this.error(ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
|
|
299
296
|
}
|
|
300
297
|
}
|
|
301
298
|
else {
|
|
302
|
-
return this.error(
|
|
299
|
+
return this.error(ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
|
|
303
300
|
}
|
|
304
301
|
};
|
|
305
302
|
/**
|
|
@@ -336,7 +333,7 @@ var Parser = /** @class */ (function () {
|
|
|
336
333
|
}
|
|
337
334
|
var location = createLocation(start, this.clonePosition());
|
|
338
335
|
return {
|
|
339
|
-
val: { type:
|
|
336
|
+
val: { type: TYPE.literal, value: value, location: location },
|
|
340
337
|
err: null,
|
|
341
338
|
};
|
|
342
339
|
};
|
|
@@ -429,20 +426,20 @@ var Parser = /** @class */ (function () {
|
|
|
429
426
|
this.bump(); // `{`
|
|
430
427
|
this.bumpSpace();
|
|
431
428
|
if (this.isEOF()) {
|
|
432
|
-
return this.error(
|
|
429
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
|
|
433
430
|
}
|
|
434
431
|
if (this.char() === 125 /* `}` */) {
|
|
435
432
|
this.bump();
|
|
436
|
-
return this.error(
|
|
433
|
+
return this.error(ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
|
|
437
434
|
}
|
|
438
435
|
// argument name
|
|
439
436
|
var value = this.parseIdentifierIfPossible().value;
|
|
440
437
|
if (!value) {
|
|
441
|
-
return this.error(
|
|
438
|
+
return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
|
|
442
439
|
}
|
|
443
440
|
this.bumpSpace();
|
|
444
441
|
if (this.isEOF()) {
|
|
445
|
-
return this.error(
|
|
442
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
|
|
446
443
|
}
|
|
447
444
|
switch (this.char()) {
|
|
448
445
|
// Simple argument: `{name}`
|
|
@@ -450,7 +447,7 @@ var Parser = /** @class */ (function () {
|
|
|
450
447
|
this.bump(); // `}`
|
|
451
448
|
return {
|
|
452
449
|
val: {
|
|
453
|
-
type:
|
|
450
|
+
type: TYPE.argument,
|
|
454
451
|
// value does not include the opening and closing braces.
|
|
455
452
|
value: value,
|
|
456
453
|
location: createLocation(openingBracePosition, this.clonePosition()),
|
|
@@ -463,12 +460,12 @@ var Parser = /** @class */ (function () {
|
|
|
463
460
|
this.bump(); // `,`
|
|
464
461
|
this.bumpSpace();
|
|
465
462
|
if (this.isEOF()) {
|
|
466
|
-
return this.error(
|
|
463
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
|
|
467
464
|
}
|
|
468
465
|
return this.parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition);
|
|
469
466
|
}
|
|
470
467
|
default:
|
|
471
|
-
return this.error(
|
|
468
|
+
return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
|
|
472
469
|
}
|
|
473
470
|
};
|
|
474
471
|
/**
|
|
@@ -496,7 +493,7 @@ var Parser = /** @class */ (function () {
|
|
|
496
493
|
switch (argType) {
|
|
497
494
|
case '':
|
|
498
495
|
// Expecting a style string number, date, time, plural, selectordinal, or select.
|
|
499
|
-
return this.error(
|
|
496
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
|
|
500
497
|
case 'number':
|
|
501
498
|
case 'date':
|
|
502
499
|
case 'time': {
|
|
@@ -514,7 +511,7 @@ var Parser = /** @class */ (function () {
|
|
|
514
511
|
}
|
|
515
512
|
var style = trimEnd(result.val);
|
|
516
513
|
if (style.length === 0) {
|
|
517
|
-
return this.error(
|
|
514
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
|
|
518
515
|
}
|
|
519
516
|
var styleLocation = createLocation(styleStartPosition, this.clonePosition());
|
|
520
517
|
styleAndLocation = { style: style, styleLocation: styleLocation };
|
|
@@ -534,30 +531,30 @@ var Parser = /** @class */ (function () {
|
|
|
534
531
|
return result;
|
|
535
532
|
}
|
|
536
533
|
return {
|
|
537
|
-
val: { type:
|
|
534
|
+
val: { type: TYPE.number, value: value, location: location_1, style: result.val },
|
|
538
535
|
err: null,
|
|
539
536
|
};
|
|
540
537
|
}
|
|
541
538
|
else {
|
|
542
539
|
if (skeleton.length === 0) {
|
|
543
|
-
return this.error(
|
|
540
|
+
return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
|
|
544
541
|
}
|
|
545
542
|
var dateTimePattern = skeleton;
|
|
546
543
|
// Get "best match" pattern only if locale is passed, if not, let it
|
|
547
544
|
// pass as-is where `parseDateTimeSkeleton()` will throw an error
|
|
548
545
|
// for unsupported patterns.
|
|
549
546
|
if (this.locale) {
|
|
550
|
-
dateTimePattern =
|
|
547
|
+
dateTimePattern = getBestPattern(skeleton, this.locale);
|
|
551
548
|
}
|
|
552
549
|
var style = {
|
|
553
|
-
type:
|
|
550
|
+
type: SKELETON_TYPE.dateTime,
|
|
554
551
|
pattern: dateTimePattern,
|
|
555
552
|
location: styleAndLocation.styleLocation,
|
|
556
553
|
parsedOptions: this.shouldParseSkeletons
|
|
557
|
-
?
|
|
554
|
+
? parseDateTimeSkeleton(dateTimePattern)
|
|
558
555
|
: {},
|
|
559
556
|
};
|
|
560
|
-
var type = argType === 'date' ?
|
|
557
|
+
var type = argType === 'date' ? TYPE.date : TYPE.time;
|
|
561
558
|
return {
|
|
562
559
|
val: { type: type, value: value, location: location_1, style: style },
|
|
563
560
|
err: null,
|
|
@@ -568,10 +565,10 @@ var Parser = /** @class */ (function () {
|
|
|
568
565
|
return {
|
|
569
566
|
val: {
|
|
570
567
|
type: argType === 'number'
|
|
571
|
-
?
|
|
568
|
+
? TYPE.number
|
|
572
569
|
: argType === 'date'
|
|
573
|
-
?
|
|
574
|
-
:
|
|
570
|
+
? TYPE.date
|
|
571
|
+
: TYPE.time,
|
|
575
572
|
value: value,
|
|
576
573
|
location: location_1,
|
|
577
574
|
style: (_a = styleAndLocation === null || styleAndLocation === void 0 ? void 0 : styleAndLocation.style) !== null && _a !== void 0 ? _a : null,
|
|
@@ -588,7 +585,7 @@ var Parser = /** @class */ (function () {
|
|
|
588
585
|
var typeEndPosition_1 = this.clonePosition();
|
|
589
586
|
this.bumpSpace();
|
|
590
587
|
if (!this.bumpIf(',')) {
|
|
591
|
-
return this.error(
|
|
588
|
+
return this.error(ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition_1, __assign({}, typeEndPosition_1)));
|
|
592
589
|
}
|
|
593
590
|
this.bumpSpace();
|
|
594
591
|
// Parse offset:
|
|
@@ -603,10 +600,10 @@ var Parser = /** @class */ (function () {
|
|
|
603
600
|
var pluralOffset = 0;
|
|
604
601
|
if (argType !== 'select' && identifierAndLocation.value === 'offset') {
|
|
605
602
|
if (!this.bumpIf(':')) {
|
|
606
|
-
return this.error(
|
|
603
|
+
return this.error(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
|
|
607
604
|
}
|
|
608
605
|
this.bumpSpace();
|
|
609
|
-
var result = this.tryParseDecimalInteger(
|
|
606
|
+
var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
|
|
610
607
|
if (result.err) {
|
|
611
608
|
return result;
|
|
612
609
|
}
|
|
@@ -627,7 +624,7 @@ var Parser = /** @class */ (function () {
|
|
|
627
624
|
if (argType === 'select') {
|
|
628
625
|
return {
|
|
629
626
|
val: {
|
|
630
|
-
type:
|
|
627
|
+
type: TYPE.select,
|
|
631
628
|
value: value,
|
|
632
629
|
options: fromEntries(optionsResult.val),
|
|
633
630
|
location: location_2,
|
|
@@ -638,7 +635,7 @@ var Parser = /** @class */ (function () {
|
|
|
638
635
|
else {
|
|
639
636
|
return {
|
|
640
637
|
val: {
|
|
641
|
-
type:
|
|
638
|
+
type: TYPE.plural,
|
|
642
639
|
value: value,
|
|
643
640
|
options: fromEntries(optionsResult.val),
|
|
644
641
|
offset: pluralOffset,
|
|
@@ -650,14 +647,14 @@ var Parser = /** @class */ (function () {
|
|
|
650
647
|
}
|
|
651
648
|
}
|
|
652
649
|
default:
|
|
653
|
-
return this.error(
|
|
650
|
+
return this.error(ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
|
|
654
651
|
}
|
|
655
652
|
};
|
|
656
653
|
Parser.prototype.tryParseArgumentClose = function (openingBracePosition) {
|
|
657
654
|
// Parse: {value, number, ::currency/GBP }
|
|
658
655
|
//
|
|
659
656
|
if (this.isEOF() || this.char() !== 125 /* `}` */) {
|
|
660
|
-
return this.error(
|
|
657
|
+
return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
|
|
661
658
|
}
|
|
662
659
|
this.bump(); // `}`
|
|
663
660
|
return { val: true, err: null };
|
|
@@ -677,7 +674,7 @@ var Parser = /** @class */ (function () {
|
|
|
677
674
|
this.bump();
|
|
678
675
|
var apostrophePosition = this.clonePosition();
|
|
679
676
|
if (!this.bumpUntil("'")) {
|
|
680
|
-
return this.error(
|
|
677
|
+
return this.error(ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
|
|
681
678
|
}
|
|
682
679
|
this.bump();
|
|
683
680
|
break;
|
|
@@ -712,18 +709,18 @@ var Parser = /** @class */ (function () {
|
|
|
712
709
|
Parser.prototype.parseNumberSkeletonFromString = function (skeleton, location) {
|
|
713
710
|
var tokens = [];
|
|
714
711
|
try {
|
|
715
|
-
tokens =
|
|
712
|
+
tokens = parseNumberSkeletonFromString(skeleton);
|
|
716
713
|
}
|
|
717
714
|
catch (e) {
|
|
718
|
-
return this.error(
|
|
715
|
+
return this.error(ErrorKind.INVALID_NUMBER_SKELETON, location);
|
|
719
716
|
}
|
|
720
717
|
return {
|
|
721
718
|
val: {
|
|
722
|
-
type:
|
|
719
|
+
type: SKELETON_TYPE.number,
|
|
723
720
|
tokens: tokens,
|
|
724
721
|
location: location,
|
|
725
722
|
parsedOptions: this.shouldParseSkeletons
|
|
726
|
-
?
|
|
723
|
+
? parseNumberSkeleton(tokens)
|
|
727
724
|
: {},
|
|
728
725
|
},
|
|
729
726
|
err: null,
|
|
@@ -753,7 +750,7 @@ var Parser = /** @class */ (function () {
|
|
|
753
750
|
var startPosition = this.clonePosition();
|
|
754
751
|
if (parentArgType !== 'select' && this.bumpIf('=')) {
|
|
755
752
|
// Try parse `={number}` selector
|
|
756
|
-
var result = this.tryParseDecimalInteger(
|
|
753
|
+
var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
|
|
757
754
|
if (result.err) {
|
|
758
755
|
return result;
|
|
759
756
|
}
|
|
@@ -767,8 +764,8 @@ var Parser = /** @class */ (function () {
|
|
|
767
764
|
// Duplicate selector clauses
|
|
768
765
|
if (parsedSelectors.has(selector)) {
|
|
769
766
|
return this.error(parentArgType === 'select'
|
|
770
|
-
?
|
|
771
|
-
:
|
|
767
|
+
? ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR
|
|
768
|
+
: ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
|
|
772
769
|
}
|
|
773
770
|
if (selector === 'other') {
|
|
774
771
|
hasOtherClause = true;
|
|
@@ -780,8 +777,8 @@ var Parser = /** @class */ (function () {
|
|
|
780
777
|
var openingBracePosition = this.clonePosition();
|
|
781
778
|
if (!this.bumpIf('{')) {
|
|
782
779
|
return this.error(parentArgType === 'select'
|
|
783
|
-
?
|
|
784
|
-
:
|
|
780
|
+
? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT
|
|
781
|
+
: ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
|
|
785
782
|
}
|
|
786
783
|
var fragmentResult = this.parseMessage(nestingLevel + 1, parentArgType, expectCloseTag);
|
|
787
784
|
if (fragmentResult.err) {
|
|
@@ -806,11 +803,11 @@ var Parser = /** @class */ (function () {
|
|
|
806
803
|
}
|
|
807
804
|
if (options.length === 0) {
|
|
808
805
|
return this.error(parentArgType === 'select'
|
|
809
|
-
?
|
|
810
|
-
:
|
|
806
|
+
? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR
|
|
807
|
+
: ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
|
|
811
808
|
}
|
|
812
809
|
if (this.requiresOtherClause && !hasOtherClause) {
|
|
813
|
-
return this.error(
|
|
810
|
+
return this.error(ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
|
|
814
811
|
}
|
|
815
812
|
return { val: options, err: null };
|
|
816
813
|
};
|
|
@@ -976,7 +973,7 @@ var Parser = /** @class */ (function () {
|
|
|
976
973
|
};
|
|
977
974
|
return Parser;
|
|
978
975
|
}());
|
|
979
|
-
|
|
976
|
+
export { Parser };
|
|
980
977
|
/**
|
|
981
978
|
* This check if codepoint is alphabet (lower & uppercase)
|
|
982
979
|
* @param codepoint
|
package/lib/printer.js
CHANGED
|
@@ -1,39 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var types_1 = require("./types");
|
|
6
|
-
function printAST(ast) {
|
|
1
|
+
import { __spreadArray } from "tslib";
|
|
2
|
+
import { isLiteralElement, isTagElement, isSelectElement, isArgumentElement, isDateElement, isTimeElement, isNumberElement, isPluralElement, TYPE, SKELETON_TYPE, isPoundElement, } from './types';
|
|
3
|
+
export function printAST(ast) {
|
|
7
4
|
return doPrintAST(ast, false);
|
|
8
5
|
}
|
|
9
|
-
|
|
10
|
-
function doPrintAST(ast, isInPlural) {
|
|
6
|
+
export function doPrintAST(ast, isInPlural) {
|
|
11
7
|
var printedNodes = ast.map(function (el, i) {
|
|
12
|
-
if (
|
|
8
|
+
if (isLiteralElement(el)) {
|
|
13
9
|
return printLiteralElement(el, isInPlural, i === 0, i === ast.length - 1);
|
|
14
10
|
}
|
|
15
|
-
if (
|
|
11
|
+
if (isArgumentElement(el)) {
|
|
16
12
|
return printArgumentElement(el);
|
|
17
13
|
}
|
|
18
|
-
if (
|
|
14
|
+
if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
|
|
19
15
|
return printSimpleFormatElement(el);
|
|
20
16
|
}
|
|
21
|
-
if (
|
|
17
|
+
if (isPluralElement(el)) {
|
|
22
18
|
return printPluralElement(el);
|
|
23
19
|
}
|
|
24
|
-
if (
|
|
20
|
+
if (isSelectElement(el)) {
|
|
25
21
|
return printSelectElement(el);
|
|
26
22
|
}
|
|
27
|
-
if (
|
|
23
|
+
if (isPoundElement(el)) {
|
|
28
24
|
return '#';
|
|
29
25
|
}
|
|
30
|
-
if (
|
|
26
|
+
if (isTagElement(el)) {
|
|
31
27
|
return printTagElement(el);
|
|
32
28
|
}
|
|
33
29
|
});
|
|
34
30
|
return printedNodes.join('');
|
|
35
31
|
}
|
|
36
|
-
exports.doPrintAST = doPrintAST;
|
|
37
32
|
function printTagElement(el) {
|
|
38
33
|
return "<".concat(el.value, ">").concat(printAST(el.children), "</").concat(el.value, ">");
|
|
39
34
|
}
|
|
@@ -60,7 +55,7 @@ function printArgumentElement(_a) {
|
|
|
60
55
|
return "{".concat(value, "}");
|
|
61
56
|
}
|
|
62
57
|
function printSimpleFormatElement(el) {
|
|
63
|
-
return "{".concat(el.value, ", ").concat(
|
|
58
|
+
return "{".concat(el.value, ", ").concat(TYPE[el.type]).concat(el.style ? ", ".concat(printArgumentStyle(el.style)) : '', "}");
|
|
64
59
|
}
|
|
65
60
|
function printNumberSkeletonToken(token) {
|
|
66
61
|
var stem = token.stem, options = token.options;
|
|
@@ -72,17 +67,16 @@ function printArgumentStyle(style) {
|
|
|
72
67
|
if (typeof style === 'string') {
|
|
73
68
|
return printEscapedMessage(style);
|
|
74
69
|
}
|
|
75
|
-
else if (style.type ===
|
|
70
|
+
else if (style.type === SKELETON_TYPE.dateTime) {
|
|
76
71
|
return "::".concat(printDateTimeSkeleton(style));
|
|
77
72
|
}
|
|
78
73
|
else {
|
|
79
74
|
return "::".concat(style.tokens.map(printNumberSkeletonToken).join(' '));
|
|
80
75
|
}
|
|
81
76
|
}
|
|
82
|
-
function printDateTimeSkeleton(style) {
|
|
77
|
+
export function printDateTimeSkeleton(style) {
|
|
83
78
|
return style.pattern;
|
|
84
79
|
}
|
|
85
|
-
exports.printDateTimeSkeleton = printDateTimeSkeleton;
|
|
86
80
|
function printSelectElement(el) {
|
|
87
81
|
var msg = [
|
|
88
82
|
el.value,
|
|
@@ -98,7 +92,7 @@ function printPluralElement(el) {
|
|
|
98
92
|
var msg = [
|
|
99
93
|
el.value,
|
|
100
94
|
type,
|
|
101
|
-
|
|
95
|
+
__spreadArray([
|
|
102
96
|
el.offset ? "offset:".concat(el.offset) : ''
|
|
103
97
|
], Object.keys(el.options).map(function (id) { return "".concat(id, "{").concat(doPrintAST(el.options[id].value, true), "}"); }), true).filter(Boolean)
|
|
104
98
|
.join(' '),
|
package/lib/regex.generated.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WHITE_SPACE_REGEX = exports.SPACE_SEPARATOR_REGEX = void 0;
|
|
4
1
|
// @generated from regex-gen.ts
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export var SPACE_SEPARATOR_REGEX = /[ \xA0\u1680\u2000-\u200A\u202F\u205F\u3000]/;
|
|
3
|
+
export var WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/;
|
package/lib/types.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createNumberElement = exports.createLiteralElement = exports.isDateTimeSkeleton = exports.isNumberSkeleton = exports.isTagElement = exports.isPoundElement = exports.isPluralElement = exports.isSelectElement = exports.isTimeElement = exports.isDateElement = exports.isNumberElement = exports.isArgumentElement = exports.isLiteralElement = exports.SKELETON_TYPE = exports.TYPE = void 0;
|
|
4
|
-
var TYPE;
|
|
1
|
+
export var TYPE;
|
|
5
2
|
(function (TYPE) {
|
|
6
3
|
/**
|
|
7
4
|
* Raw text
|
|
@@ -40,71 +37,58 @@ var TYPE;
|
|
|
40
37
|
* XML-like tag
|
|
41
38
|
*/
|
|
42
39
|
TYPE[TYPE["tag"] = 8] = "tag";
|
|
43
|
-
})(TYPE || (
|
|
44
|
-
var SKELETON_TYPE;
|
|
40
|
+
})(TYPE || (TYPE = {}));
|
|
41
|
+
export var SKELETON_TYPE;
|
|
45
42
|
(function (SKELETON_TYPE) {
|
|
46
43
|
SKELETON_TYPE[SKELETON_TYPE["number"] = 0] = "number";
|
|
47
44
|
SKELETON_TYPE[SKELETON_TYPE["dateTime"] = 1] = "dateTime";
|
|
48
|
-
})(SKELETON_TYPE || (
|
|
45
|
+
})(SKELETON_TYPE || (SKELETON_TYPE = {}));
|
|
49
46
|
/**
|
|
50
47
|
* Type Guards
|
|
51
48
|
*/
|
|
52
|
-
function isLiteralElement(el) {
|
|
49
|
+
export function isLiteralElement(el) {
|
|
53
50
|
return el.type === TYPE.literal;
|
|
54
51
|
}
|
|
55
|
-
|
|
56
|
-
function isArgumentElement(el) {
|
|
52
|
+
export function isArgumentElement(el) {
|
|
57
53
|
return el.type === TYPE.argument;
|
|
58
54
|
}
|
|
59
|
-
|
|
60
|
-
function isNumberElement(el) {
|
|
55
|
+
export function isNumberElement(el) {
|
|
61
56
|
return el.type === TYPE.number;
|
|
62
57
|
}
|
|
63
|
-
|
|
64
|
-
function isDateElement(el) {
|
|
58
|
+
export function isDateElement(el) {
|
|
65
59
|
return el.type === TYPE.date;
|
|
66
60
|
}
|
|
67
|
-
|
|
68
|
-
function isTimeElement(el) {
|
|
61
|
+
export function isTimeElement(el) {
|
|
69
62
|
return el.type === TYPE.time;
|
|
70
63
|
}
|
|
71
|
-
|
|
72
|
-
function isSelectElement(el) {
|
|
64
|
+
export function isSelectElement(el) {
|
|
73
65
|
return el.type === TYPE.select;
|
|
74
66
|
}
|
|
75
|
-
|
|
76
|
-
function isPluralElement(el) {
|
|
67
|
+
export function isPluralElement(el) {
|
|
77
68
|
return el.type === TYPE.plural;
|
|
78
69
|
}
|
|
79
|
-
|
|
80
|
-
function isPoundElement(el) {
|
|
70
|
+
export function isPoundElement(el) {
|
|
81
71
|
return el.type === TYPE.pound;
|
|
82
72
|
}
|
|
83
|
-
|
|
84
|
-
function isTagElement(el) {
|
|
73
|
+
export function isTagElement(el) {
|
|
85
74
|
return el.type === TYPE.tag;
|
|
86
75
|
}
|
|
87
|
-
|
|
88
|
-
function isNumberSkeleton(el) {
|
|
76
|
+
export function isNumberSkeleton(el) {
|
|
89
77
|
return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.number);
|
|
90
78
|
}
|
|
91
|
-
|
|
92
|
-
function isDateTimeSkeleton(el) {
|
|
79
|
+
export function isDateTimeSkeleton(el) {
|
|
93
80
|
return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.dateTime);
|
|
94
81
|
}
|
|
95
|
-
|
|
96
|
-
function createLiteralElement(value) {
|
|
82
|
+
export function createLiteralElement(value) {
|
|
97
83
|
return {
|
|
98
84
|
type: TYPE.literal,
|
|
99
85
|
value: value,
|
|
100
86
|
};
|
|
101
87
|
}
|
|
102
|
-
|
|
103
|
-
function createNumberElement(value, style) {
|
|
88
|
+
export function createNumberElement(value, style) {
|
|
104
89
|
return {
|
|
105
90
|
type: TYPE.number,
|
|
106
91
|
value: value,
|
|
107
92
|
style: style,
|
|
108
93
|
};
|
|
109
94
|
}
|
|
110
|
-
exports.createNumberElement = createNumberElement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/icu-messageformat-parser",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.3",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"module": "lib/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"tslib": "^2.4.0",
|
|
15
|
-
"@formatjs/ecma402-abstract": "1.
|
|
16
|
-
"@formatjs/icu-skeleton-parser": "1.
|
|
15
|
+
"@formatjs/ecma402-abstract": "1.18.0",
|
|
16
|
+
"@formatjs/icu-skeleton-parser": "1.7.0"
|
|
17
17
|
}
|
|
18
18
|
}
|