@formatjs/icu-messageformat-parser 2.11.3 → 3.0.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/date-time-pattern-generator.js +6 -9
- package/error.d.ts +1 -1
- package/error.js +2 -5
- package/index.d.ts +4 -4
- package/index.js +16 -21
- package/manipulator.d.ts +1 -1
- package/manipulator.js +16 -20
- package/no-parser.d.ts +2 -2
- package/no-parser.js +4 -10
- package/package.json +10 -5
- package/parser.d.ts +2 -2
- package/parser.js +59 -62
- package/printer.d.ts +1 -1
- package/printer.js +15 -20
- package/regex.generated.js +2 -5
- package/time-data.generated.js +1 -4
- package/types.js +17 -33
- package/lib/date-time-pattern-generator.d.ts +0 -8
- package/lib/date-time-pattern-generator.js +0 -83
- package/lib/error.d.ts +0 -68
- package/lib/error.js +0 -63
- package/lib/index.d.ts +0 -7
- package/lib/index.js +0 -46
- package/lib/manipulator.d.ts +0 -26
- package/lib/manipulator.js +0 -135
- package/lib/no-parser.d.ts +0 -4
- package/lib/no-parser.js +0 -6
- package/lib/parser.d.ts +0 -147
- package/lib/parser.js +0 -1276
- package/lib/printer.d.ts +0 -4
- package/lib/printer.js +0 -101
- package/lib/regex.generated.d.ts +0 -2
- package/lib/regex.generated.js +0 -3
- package/lib/time-data.generated.d.ts +0 -1
- package/lib/time-data.generated.js +0 -1417
- package/lib/types.d.ts +0 -120
- package/lib/types.js +0 -94
package/lib/error.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { Location } from './types';
|
|
2
|
-
export interface ParserError {
|
|
3
|
-
kind: ErrorKind;
|
|
4
|
-
message: string;
|
|
5
|
-
location: Location;
|
|
6
|
-
}
|
|
7
|
-
export declare enum ErrorKind {
|
|
8
|
-
/** Argument is unclosed (e.g. `{0`) */
|
|
9
|
-
EXPECT_ARGUMENT_CLOSING_BRACE = 1,
|
|
10
|
-
/** Argument is empty (e.g. `{}`). */
|
|
11
|
-
EMPTY_ARGUMENT = 2,
|
|
12
|
-
/** Argument is malformed (e.g. `{foo!}``) */
|
|
13
|
-
MALFORMED_ARGUMENT = 3,
|
|
14
|
-
/** Expect an argument type (e.g. `{foo,}`) */
|
|
15
|
-
EXPECT_ARGUMENT_TYPE = 4,
|
|
16
|
-
/** Unsupported argument type (e.g. `{foo,foo}`) */
|
|
17
|
-
INVALID_ARGUMENT_TYPE = 5,
|
|
18
|
-
/** Expect an argument style (e.g. `{foo, number, }`) */
|
|
19
|
-
EXPECT_ARGUMENT_STYLE = 6,
|
|
20
|
-
/** The number skeleton is invalid. */
|
|
21
|
-
INVALID_NUMBER_SKELETON = 7,
|
|
22
|
-
/** The date time skeleton is invalid. */
|
|
23
|
-
INVALID_DATE_TIME_SKELETON = 8,
|
|
24
|
-
/** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
|
|
25
|
-
EXPECT_NUMBER_SKELETON = 9,
|
|
26
|
-
/** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
|
|
27
|
-
EXPECT_DATE_TIME_SKELETON = 10,
|
|
28
|
-
/** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
|
|
29
|
-
UNCLOSED_QUOTE_IN_ARGUMENT_STYLE = 11,
|
|
30
|
-
/** Missing select argument options (e.g. `{foo, select}`) */
|
|
31
|
-
EXPECT_SELECT_ARGUMENT_OPTIONS = 12,
|
|
32
|
-
/** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
|
|
33
|
-
EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE = 13,
|
|
34
|
-
/** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
|
|
35
|
-
INVALID_PLURAL_ARGUMENT_OFFSET_VALUE = 14,
|
|
36
|
-
/** Expecting a selector in `select` argument (e.g `{foo, select}`) */
|
|
37
|
-
EXPECT_SELECT_ARGUMENT_SELECTOR = 15,
|
|
38
|
-
/** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
|
|
39
|
-
EXPECT_PLURAL_ARGUMENT_SELECTOR = 16,
|
|
40
|
-
/** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
|
|
41
|
-
EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT = 17,
|
|
42
|
-
/**
|
|
43
|
-
* Expecting a message fragment after the `plural` or `selectordinal` selector
|
|
44
|
-
* (e.g. `{foo, plural, one}`)
|
|
45
|
-
*/
|
|
46
|
-
EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT = 18,
|
|
47
|
-
/** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
|
|
48
|
-
INVALID_PLURAL_ARGUMENT_SELECTOR = 19,
|
|
49
|
-
/**
|
|
50
|
-
* Duplicate selectors in `plural` or `selectordinal` argument.
|
|
51
|
-
* (e.g. {foo, plural, one {#} one {#}})
|
|
52
|
-
*/
|
|
53
|
-
DUPLICATE_PLURAL_ARGUMENT_SELECTOR = 20,
|
|
54
|
-
/** Duplicate selectors in `select` argument.
|
|
55
|
-
* (e.g. {foo, select, apple {apple} apple {apple}})
|
|
56
|
-
*/
|
|
57
|
-
DUPLICATE_SELECT_ARGUMENT_SELECTOR = 21,
|
|
58
|
-
/** Plural or select argument option must have `other` clause. */
|
|
59
|
-
MISSING_OTHER_CLAUSE = 22,
|
|
60
|
-
/** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
|
|
61
|
-
INVALID_TAG = 23,
|
|
62
|
-
/** The tag name is invalid. (e.g. `<123>foo</123>`) */
|
|
63
|
-
INVALID_TAG_NAME = 25,
|
|
64
|
-
/** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
|
|
65
|
-
UNMATCHED_CLOSING_TAG = 26,
|
|
66
|
-
/** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
|
|
67
|
-
UNCLOSED_TAG = 27
|
|
68
|
-
}
|
package/lib/error.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
export var ErrorKind;
|
|
2
|
-
(function (ErrorKind) {
|
|
3
|
-
/** Argument is unclosed (e.g. `{0`) */
|
|
4
|
-
ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
|
|
5
|
-
/** Argument is empty (e.g. `{}`). */
|
|
6
|
-
ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
|
|
7
|
-
/** Argument is malformed (e.g. `{foo!}``) */
|
|
8
|
-
ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
|
|
9
|
-
/** Expect an argument type (e.g. `{foo,}`) */
|
|
10
|
-
ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
|
|
11
|
-
/** Unsupported argument type (e.g. `{foo,foo}`) */
|
|
12
|
-
ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
|
|
13
|
-
/** Expect an argument style (e.g. `{foo, number, }`) */
|
|
14
|
-
ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
|
|
15
|
-
/** The number skeleton is invalid. */
|
|
16
|
-
ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
|
|
17
|
-
/** The date time skeleton is invalid. */
|
|
18
|
-
ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
|
|
19
|
-
/** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
|
|
20
|
-
ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
|
|
21
|
-
/** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
|
|
22
|
-
ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
|
|
23
|
-
/** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
|
|
24
|
-
ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
|
|
25
|
-
/** Missing select argument options (e.g. `{foo, select}`) */
|
|
26
|
-
ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
|
|
27
|
-
/** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
|
|
28
|
-
ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
|
|
29
|
-
/** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
|
|
30
|
-
ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
|
|
31
|
-
/** Expecting a selector in `select` argument (e.g `{foo, select}`) */
|
|
32
|
-
ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
|
|
33
|
-
/** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
|
|
34
|
-
ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
|
|
35
|
-
/** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
|
|
36
|
-
ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
|
|
37
|
-
/**
|
|
38
|
-
* Expecting a message fragment after the `plural` or `selectordinal` selector
|
|
39
|
-
* (e.g. `{foo, plural, one}`)
|
|
40
|
-
*/
|
|
41
|
-
ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
|
|
42
|
-
/** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
|
|
43
|
-
ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
|
|
44
|
-
/**
|
|
45
|
-
* Duplicate selectors in `plural` or `selectordinal` argument.
|
|
46
|
-
* (e.g. {foo, plural, one {#} one {#}})
|
|
47
|
-
*/
|
|
48
|
-
ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
|
|
49
|
-
/** Duplicate selectors in `select` argument.
|
|
50
|
-
* (e.g. {foo, select, apple {apple} apple {apple}})
|
|
51
|
-
*/
|
|
52
|
-
ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
|
|
53
|
-
/** Plural or select argument option must have `other` clause. */
|
|
54
|
-
ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
|
|
55
|
-
/** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
|
|
56
|
-
ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
|
|
57
|
-
/** The tag name is invalid. (e.g. `<123>foo</123>`) */
|
|
58
|
-
ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
|
|
59
|
-
/** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
|
|
60
|
-
ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
|
|
61
|
-
/** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
|
|
62
|
-
ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
|
|
63
|
-
})(ErrorKind || (ErrorKind = {}));
|
package/lib/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Parser, ParserOptions } from './parser';
|
|
2
|
-
import { MessageFormatElement } from './types';
|
|
3
|
-
export declare function parse(message: string, opts?: ParserOptions): MessageFormatElement[];
|
|
4
|
-
export * from './types';
|
|
5
|
-
export type { ParserOptions };
|
|
6
|
-
export declare const _Parser: typeof Parser;
|
|
7
|
-
export { isStructurallySame } from './manipulator';
|
package/lib/index.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
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';
|
|
5
|
-
function pruneLocation(els) {
|
|
6
|
-
els.forEach(function (el) {
|
|
7
|
-
delete el.location;
|
|
8
|
-
if (isSelectElement(el) || isPluralElement(el)) {
|
|
9
|
-
for (var k in el.options) {
|
|
10
|
-
delete el.options[k].location;
|
|
11
|
-
pruneLocation(el.options[k].value);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
|
|
15
|
-
delete el.style.location;
|
|
16
|
-
}
|
|
17
|
-
else if ((isDateElement(el) || isTimeElement(el)) &&
|
|
18
|
-
isDateTimeSkeleton(el.style)) {
|
|
19
|
-
delete el.style.location;
|
|
20
|
-
}
|
|
21
|
-
else if (isTagElement(el)) {
|
|
22
|
-
pruneLocation(el.children);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export function parse(message, opts) {
|
|
27
|
-
if (opts === void 0) { opts = {}; }
|
|
28
|
-
opts = __assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
|
|
29
|
-
var result = new Parser(message, opts).parse();
|
|
30
|
-
if (result.err) {
|
|
31
|
-
var error = SyntaxError(ErrorKind[result.err.kind]);
|
|
32
|
-
// @ts-expect-error Assign to error object
|
|
33
|
-
error.location = result.err.location;
|
|
34
|
-
// @ts-expect-error Assign to error object
|
|
35
|
-
error.originalMessage = result.err.message;
|
|
36
|
-
throw error;
|
|
37
|
-
}
|
|
38
|
-
if (!(opts === null || opts === void 0 ? void 0 : opts.captureLocation)) {
|
|
39
|
-
pruneLocation(result.val);
|
|
40
|
-
}
|
|
41
|
-
return result.val;
|
|
42
|
-
}
|
|
43
|
-
export * from './types';
|
|
44
|
-
// only for testing
|
|
45
|
-
export var _Parser = Parser;
|
|
46
|
-
export { isStructurallySame } from './manipulator';
|
package/lib/manipulator.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { MessageFormatElement } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Hoist all selectors to the beginning of the AST & flatten the
|
|
4
|
-
* resulting options. E.g:
|
|
5
|
-
* "I have {count, plural, one{a dog} other{many dogs}}"
|
|
6
|
-
* becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
|
|
7
|
-
* If there are multiple selectors, the order of which one is hoisted 1st
|
|
8
|
-
* is non-deterministic.
|
|
9
|
-
* The goal is to provide as many full sentences as possible since fragmented
|
|
10
|
-
* sentences are not translator-friendly
|
|
11
|
-
* @param ast AST
|
|
12
|
-
*/
|
|
13
|
-
export declare function hoistSelectors(ast: MessageFormatElement[]): MessageFormatElement[];
|
|
14
|
-
interface IsStructurallySameResult {
|
|
15
|
-
error?: Error;
|
|
16
|
-
success: boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Check if 2 ASTs are structurally the same. This primarily means that
|
|
20
|
-
* they have the same variables with the same type
|
|
21
|
-
* @param a
|
|
22
|
-
* @param b
|
|
23
|
-
* @returns
|
|
24
|
-
*/
|
|
25
|
-
export declare function isStructurallySame(a: MessageFormatElement[], b: MessageFormatElement[]): IsStructurallySameResult;
|
|
26
|
-
export {};
|
package/lib/manipulator.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { __spreadArray } from "tslib";
|
|
2
|
-
import { isArgumentElement, isDateElement, isNumberElement, isPluralElement, isSelectElement, isTagElement, isTimeElement, TYPE, } from './types';
|
|
3
|
-
function cloneDeep(obj) {
|
|
4
|
-
if (Array.isArray(obj)) {
|
|
5
|
-
// @ts-expect-error meh
|
|
6
|
-
return __spreadArray([], obj.map(cloneDeep), true);
|
|
7
|
-
}
|
|
8
|
-
if (obj !== null && typeof obj === 'object') {
|
|
9
|
-
// @ts-expect-error meh
|
|
10
|
-
return Object.keys(obj).reduce(function (cloned, k) {
|
|
11
|
-
// @ts-expect-error meh
|
|
12
|
-
cloned[k] = cloneDeep(obj[k]);
|
|
13
|
-
return cloned;
|
|
14
|
-
}, {});
|
|
15
|
-
}
|
|
16
|
-
return obj;
|
|
17
|
-
}
|
|
18
|
-
function hoistPluralOrSelectElement(ast, el, positionToInject) {
|
|
19
|
-
// pull this out of the ast and move it to the top
|
|
20
|
-
var cloned = cloneDeep(el);
|
|
21
|
-
var options = cloned.options;
|
|
22
|
-
cloned.options = Object.keys(options).reduce(function (all, k) {
|
|
23
|
-
var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, positionToInject), true), options[k].value, true), ast.slice(positionToInject + 1), true));
|
|
24
|
-
all[k] = {
|
|
25
|
-
value: newValue,
|
|
26
|
-
};
|
|
27
|
-
return all;
|
|
28
|
-
}, {});
|
|
29
|
-
return cloned;
|
|
30
|
-
}
|
|
31
|
-
function isPluralOrSelectElement(el) {
|
|
32
|
-
return isPluralElement(el) || isSelectElement(el);
|
|
33
|
-
}
|
|
34
|
-
function findPluralOrSelectElement(ast) {
|
|
35
|
-
return !!ast.find(function (el) {
|
|
36
|
-
if (isPluralOrSelectElement(el)) {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
if (isTagElement(el)) {
|
|
40
|
-
return findPluralOrSelectElement(el.children);
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Hoist all selectors to the beginning of the AST & flatten the
|
|
47
|
-
* resulting options. E.g:
|
|
48
|
-
* "I have {count, plural, one{a dog} other{many dogs}}"
|
|
49
|
-
* becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
|
|
50
|
-
* If there are multiple selectors, the order of which one is hoisted 1st
|
|
51
|
-
* is non-deterministic.
|
|
52
|
-
* The goal is to provide as many full sentences as possible since fragmented
|
|
53
|
-
* sentences are not translator-friendly
|
|
54
|
-
* @param ast AST
|
|
55
|
-
*/
|
|
56
|
-
export function hoistSelectors(ast) {
|
|
57
|
-
for (var i = 0; i < ast.length; i++) {
|
|
58
|
-
var el = ast[i];
|
|
59
|
-
if (isPluralOrSelectElement(el)) {
|
|
60
|
-
return [hoistPluralOrSelectElement(ast, el, i)];
|
|
61
|
-
}
|
|
62
|
-
if (isTagElement(el) && findPluralOrSelectElement([el])) {
|
|
63
|
-
throw new Error('Cannot hoist plural/select within a tag element. Please put the tag element inside each plural/select option');
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return ast;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Collect all variables in an AST to Record<string, TYPE>
|
|
70
|
-
* @param ast AST to collect variables from
|
|
71
|
-
* @param vars Record of variable name to variable type
|
|
72
|
-
*/
|
|
73
|
-
function collectVariables(ast, vars) {
|
|
74
|
-
if (vars === void 0) { vars = new Map(); }
|
|
75
|
-
ast.forEach(function (el) {
|
|
76
|
-
if (isArgumentElement(el) ||
|
|
77
|
-
isDateElement(el) ||
|
|
78
|
-
isTimeElement(el) ||
|
|
79
|
-
isNumberElement(el)) {
|
|
80
|
-
if (el.value in vars && vars.get(el.value) !== el.type) {
|
|
81
|
-
throw new Error("Variable ".concat(el.value, " has conflicting types"));
|
|
82
|
-
}
|
|
83
|
-
vars.set(el.value, el.type);
|
|
84
|
-
}
|
|
85
|
-
if (isPluralElement(el) || isSelectElement(el)) {
|
|
86
|
-
vars.set(el.value, el.type);
|
|
87
|
-
Object.keys(el.options).forEach(function (k) {
|
|
88
|
-
collectVariables(el.options[k].value, vars);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (isTagElement(el)) {
|
|
92
|
-
vars.set(el.value, el.type);
|
|
93
|
-
collectVariables(el.children, vars);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Check if 2 ASTs are structurally the same. This primarily means that
|
|
99
|
-
* they have the same variables with the same type
|
|
100
|
-
* @param a
|
|
101
|
-
* @param b
|
|
102
|
-
* @returns
|
|
103
|
-
*/
|
|
104
|
-
export function isStructurallySame(a, b) {
|
|
105
|
-
var aVars = new Map();
|
|
106
|
-
var bVars = new Map();
|
|
107
|
-
collectVariables(a, aVars);
|
|
108
|
-
collectVariables(b, bVars);
|
|
109
|
-
if (aVars.size !== bVars.size) {
|
|
110
|
-
return {
|
|
111
|
-
success: false,
|
|
112
|
-
error: new Error("Different number of variables: [".concat(Array.from(aVars.keys()).join(', '), "] vs [").concat(Array.from(bVars.keys()).join(', '), "]")),
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
return Array.from(aVars.entries()).reduce(function (result, _a) {
|
|
116
|
-
var key = _a[0], type = _a[1];
|
|
117
|
-
if (!result.success) {
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
var bType = bVars.get(key);
|
|
121
|
-
if (bType == null) {
|
|
122
|
-
return {
|
|
123
|
-
success: false,
|
|
124
|
-
error: new Error("Missing variable ".concat(key, " in message")),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
if (bType !== type) {
|
|
128
|
-
return {
|
|
129
|
-
success: false,
|
|
130
|
-
error: new Error("Variable ".concat(key, " has conflicting types: ").concat(TYPE[type], " vs ").concat(TYPE[bType])),
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
return result;
|
|
134
|
-
}, { success: true });
|
|
135
|
-
}
|
package/lib/no-parser.d.ts
DELETED
package/lib/no-parser.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export function parse() {
|
|
2
|
-
throw new Error("You're trying to format an uncompiled message with react-intl without parser, please import from 'react-intl' instead");
|
|
3
|
-
}
|
|
4
|
-
export * from './types';
|
|
5
|
-
export var _Parser = undefined;
|
|
6
|
-
export { isStructurallySame } from './manipulator';
|
package/lib/parser.d.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { ParserError } from './error';
|
|
2
|
-
import { MessageFormatElement } from './types';
|
|
3
|
-
export interface Position {
|
|
4
|
-
/** Offset in terms of UTF-16 *code unit*. */
|
|
5
|
-
offset: number;
|
|
6
|
-
line: number;
|
|
7
|
-
/** Column offset in terms of unicode *code point*. */
|
|
8
|
-
column: number;
|
|
9
|
-
}
|
|
10
|
-
export interface ParserOptions {
|
|
11
|
-
/**
|
|
12
|
-
* Whether to treat HTML/XML tags as string literal
|
|
13
|
-
* instead of parsing them as tag token.
|
|
14
|
-
* When this is false we only allow simple tags without
|
|
15
|
-
* any attributes
|
|
16
|
-
*/
|
|
17
|
-
ignoreTag?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Should `select`, `selectordinal`, and `plural` arguments always include
|
|
20
|
-
* the `other` case clause.
|
|
21
|
-
*/
|
|
22
|
-
requiresOtherClause?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Whether to parse number/datetime skeleton
|
|
25
|
-
* into Intl.NumberFormatOptions and Intl.DateTimeFormatOptions, respectively.
|
|
26
|
-
*/
|
|
27
|
-
shouldParseSkeletons?: boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Capture location info in AST
|
|
30
|
-
* Default is false
|
|
31
|
-
*/
|
|
32
|
-
captureLocation?: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Instance of Intl.Locale to resolve locale-dependent skeleton
|
|
35
|
-
*/
|
|
36
|
-
locale?: Intl.Locale;
|
|
37
|
-
}
|
|
38
|
-
export type Result<T, E> = {
|
|
39
|
-
val: T;
|
|
40
|
-
err: null;
|
|
41
|
-
} | {
|
|
42
|
-
val: null;
|
|
43
|
-
err: E;
|
|
44
|
-
};
|
|
45
|
-
export declare class Parser {
|
|
46
|
-
private message;
|
|
47
|
-
private position;
|
|
48
|
-
private locale?;
|
|
49
|
-
private ignoreTag;
|
|
50
|
-
private requiresOtherClause;
|
|
51
|
-
private shouldParseSkeletons?;
|
|
52
|
-
constructor(message: string, options?: ParserOptions);
|
|
53
|
-
parse(): Result<MessageFormatElement[], ParserError>;
|
|
54
|
-
private parseMessage;
|
|
55
|
-
/**
|
|
56
|
-
* A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
|
|
57
|
-
* [custom element name][] except that a dash is NOT always mandatory and uppercase letters
|
|
58
|
-
* are accepted:
|
|
59
|
-
*
|
|
60
|
-
* ```
|
|
61
|
-
* tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
|
|
62
|
-
* tagName ::= [a-z] (PENChar)*
|
|
63
|
-
* PENChar ::=
|
|
64
|
-
* "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
|
|
65
|
-
* [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
|
|
66
|
-
* [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
|
|
70
|
-
* NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
|
|
71
|
-
* since other tag-based engines like React allow it
|
|
72
|
-
*/
|
|
73
|
-
private parseTag;
|
|
74
|
-
/**
|
|
75
|
-
* This method assumes that the caller has peeked ahead for the first tag character.
|
|
76
|
-
*/
|
|
77
|
-
private parseTagName;
|
|
78
|
-
private parseLiteral;
|
|
79
|
-
tryParseLeftAngleBracket(): string | null;
|
|
80
|
-
/**
|
|
81
|
-
* Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
|
|
82
|
-
* a character that requires quoting (that is, "only where needed"), and works the same in
|
|
83
|
-
* nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
|
|
84
|
-
*/
|
|
85
|
-
private tryParseQuote;
|
|
86
|
-
private tryParseUnquoted;
|
|
87
|
-
private parseArgument;
|
|
88
|
-
/**
|
|
89
|
-
* Advance the parser until the end of the identifier, if it is currently on
|
|
90
|
-
* an identifier character. Return an empty string otherwise.
|
|
91
|
-
*/
|
|
92
|
-
private parseIdentifierIfPossible;
|
|
93
|
-
private parseArgumentOptions;
|
|
94
|
-
private tryParseArgumentClose;
|
|
95
|
-
/**
|
|
96
|
-
* See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
|
|
97
|
-
*/
|
|
98
|
-
private parseSimpleArgStyleIfPossible;
|
|
99
|
-
private parseNumberSkeletonFromString;
|
|
100
|
-
/**
|
|
101
|
-
* @param nesting_level The current nesting level of messages.
|
|
102
|
-
* This can be positive when parsing message fragment in select or plural argument options.
|
|
103
|
-
* @param parent_arg_type The parent argument's type.
|
|
104
|
-
* @param parsed_first_identifier If provided, this is the first identifier-like selector of
|
|
105
|
-
* the argument. It is a by-product of a previous parsing attempt.
|
|
106
|
-
* @param expecting_close_tag If true, this message is directly or indirectly nested inside
|
|
107
|
-
* between a pair of opening and closing tags. The nested message will not parse beyond
|
|
108
|
-
* the closing tag boundary.
|
|
109
|
-
*/
|
|
110
|
-
private tryParsePluralOrSelectOptions;
|
|
111
|
-
private tryParseDecimalInteger;
|
|
112
|
-
private offset;
|
|
113
|
-
private isEOF;
|
|
114
|
-
private clonePosition;
|
|
115
|
-
/**
|
|
116
|
-
* Return the code point at the current position of the parser.
|
|
117
|
-
* Throws if the index is out of bound.
|
|
118
|
-
*/
|
|
119
|
-
private char;
|
|
120
|
-
private error;
|
|
121
|
-
/** Bump the parser to the next UTF-16 code unit. */
|
|
122
|
-
private bump;
|
|
123
|
-
/**
|
|
124
|
-
* If the substring starting at the current position of the parser has
|
|
125
|
-
* the given prefix, then bump the parser to the character immediately
|
|
126
|
-
* following the prefix and return true. Otherwise, don't bump the parser
|
|
127
|
-
* and return false.
|
|
128
|
-
*/
|
|
129
|
-
private bumpIf;
|
|
130
|
-
/**
|
|
131
|
-
* Bump the parser until the pattern character is found and return `true`.
|
|
132
|
-
* Otherwise bump to the end of the file and return `false`.
|
|
133
|
-
*/
|
|
134
|
-
private bumpUntil;
|
|
135
|
-
/**
|
|
136
|
-
* Bump the parser to the target offset.
|
|
137
|
-
* If target offset is beyond the end of the input, bump the parser to the end of the input.
|
|
138
|
-
*/
|
|
139
|
-
private bumpTo;
|
|
140
|
-
/** advance the parser through all whitespace to the next non-whitespace code unit. */
|
|
141
|
-
private bumpSpace;
|
|
142
|
-
/**
|
|
143
|
-
* Peek at the *next* Unicode codepoint in the input without advancing the parser.
|
|
144
|
-
* If the input has been exhausted, then this returns null.
|
|
145
|
-
*/
|
|
146
|
-
private peek;
|
|
147
|
-
}
|