@intlify/core 9.3.0-beta.26 → 9.3.0-beta.27
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/dist/core.cjs +2 -2
- package/dist/core.esm-browser.js +93 -77
- package/dist/core.esm-browser.prod.js +2 -2
- package/dist/core.global.js +83 -77
- package/dist/core.global.prod.js +2 -2
- package/dist/core.mjs +4 -1
- package/dist/core.prod.cjs +2 -2
- package/dist/core.runtime.esm-browser.js +66 -1520
- package/dist/core.runtime.esm-browser.prod.js +2 -2
- package/dist/core.runtime.global.js +57 -1521
- package/dist/core.runtime.global.prod.js +2 -2
- package/dist/core.runtime.mjs +4 -1
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* core v9.3.0-beta.
|
|
2
|
+
* core v9.3.0-beta.27
|
|
3
3
|
* (c) 2023 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -53,15 +53,6 @@ var IntlifyCore = (function (exports) {
|
|
|
53
53
|
const isDate = (val) => toTypeString(val) === '[object Date]';
|
|
54
54
|
const isRegExp = (val) => toTypeString(val) === '[object RegExp]';
|
|
55
55
|
const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
|
|
56
|
-
function warn(msg, err) {
|
|
57
|
-
if (typeof console !== 'undefined') {
|
|
58
|
-
console.warn(`[intlify] ` + msg);
|
|
59
|
-
/* istanbul ignore if */
|
|
60
|
-
if (err) {
|
|
61
|
-
console.warn(err.stack);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
56
|
const assign = Object.assign;
|
|
66
57
|
function escapeHtml(rawText) {
|
|
67
58
|
return rawText
|
|
@@ -136,16 +127,19 @@ var IntlifyCore = (function (exports) {
|
|
|
136
127
|
}
|
|
137
128
|
return res.join('\n');
|
|
138
129
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return
|
|
130
|
+
function incrementer(code) {
|
|
131
|
+
let current = code;
|
|
132
|
+
return () => ++current;
|
|
142
133
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (
|
|
146
|
-
|
|
134
|
+
|
|
135
|
+
function warn(msg, err) {
|
|
136
|
+
if (typeof console !== 'undefined') {
|
|
137
|
+
console.warn(`[intlify] ` + msg);
|
|
138
|
+
/* istanbul ignore if */
|
|
139
|
+
if (err) {
|
|
140
|
+
console.warn(err.stack);
|
|
141
|
+
}
|
|
147
142
|
}
|
|
148
|
-
return loc;
|
|
149
143
|
}
|
|
150
144
|
|
|
151
145
|
const CompileErrorCodes = {
|
|
@@ -165,10 +159,14 @@ var IntlifyCore = (function (exports) {
|
|
|
165
159
|
UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
|
|
166
160
|
UNEXPECTED_EMPTY_LINKED_KEY: 13,
|
|
167
161
|
UNEXPECTED_LEXICAL_ANALYSIS: 14,
|
|
162
|
+
// generator error codes
|
|
163
|
+
UNHANDLED_CODEGEN_NODE_TYPE: 15,
|
|
164
|
+
// minifier error codes
|
|
165
|
+
UNHANDLED_MINIFIER_NODE_TYPE: 16,
|
|
168
166
|
// Special value for higher-order compilers to pick up the last code
|
|
169
167
|
// to avoid collision of error codes. This should always be kept as the last
|
|
170
168
|
// item.
|
|
171
|
-
__EXTEND_POINT__:
|
|
169
|
+
__EXTEND_POINT__: 17
|
|
172
170
|
};
|
|
173
171
|
/** @internal */
|
|
174
172
|
const errorMessages$1 = {
|
|
@@ -187,7 +185,11 @@ var IntlifyCore = (function (exports) {
|
|
|
187
185
|
[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
|
|
188
186
|
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
|
|
189
187
|
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
|
|
190
|
-
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'
|
|
188
|
+
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`,
|
|
189
|
+
// generator error messages
|
|
190
|
+
[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
|
|
191
|
+
// minimizer error messages
|
|
192
|
+
[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
|
|
191
193
|
};
|
|
192
194
|
function createCompileError(code, loc, options = {}) {
|
|
193
195
|
const { domain, messages, args } = options;
|
|
@@ -201,1437 +203,6 @@ var IntlifyCore = (function (exports) {
|
|
|
201
203
|
error.domain = domain;
|
|
202
204
|
return error;
|
|
203
205
|
}
|
|
204
|
-
/** @internal */
|
|
205
|
-
function defaultOnError(error) {
|
|
206
|
-
throw error;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
|
|
210
|
-
const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
|
|
211
|
-
|
|
212
|
-
const CHAR_SP = ' ';
|
|
213
|
-
const CHAR_CR = '\r';
|
|
214
|
-
const CHAR_LF = '\n';
|
|
215
|
-
const CHAR_LS = String.fromCharCode(0x2028);
|
|
216
|
-
const CHAR_PS = String.fromCharCode(0x2029);
|
|
217
|
-
function createScanner(str) {
|
|
218
|
-
const _buf = str;
|
|
219
|
-
let _index = 0;
|
|
220
|
-
let _line = 1;
|
|
221
|
-
let _column = 1;
|
|
222
|
-
let _peekOffset = 0;
|
|
223
|
-
const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
|
|
224
|
-
const isLF = (index) => _buf[index] === CHAR_LF;
|
|
225
|
-
const isPS = (index) => _buf[index] === CHAR_PS;
|
|
226
|
-
const isLS = (index) => _buf[index] === CHAR_LS;
|
|
227
|
-
const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
|
|
228
|
-
const index = () => _index;
|
|
229
|
-
const line = () => _line;
|
|
230
|
-
const column = () => _column;
|
|
231
|
-
const peekOffset = () => _peekOffset;
|
|
232
|
-
const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
|
|
233
|
-
const currentChar = () => charAt(_index);
|
|
234
|
-
const currentPeek = () => charAt(_index + _peekOffset);
|
|
235
|
-
function next() {
|
|
236
|
-
_peekOffset = 0;
|
|
237
|
-
if (isLineEnd(_index)) {
|
|
238
|
-
_line++;
|
|
239
|
-
_column = 0;
|
|
240
|
-
}
|
|
241
|
-
if (isCRLF(_index)) {
|
|
242
|
-
_index++;
|
|
243
|
-
}
|
|
244
|
-
_index++;
|
|
245
|
-
_column++;
|
|
246
|
-
return _buf[_index];
|
|
247
|
-
}
|
|
248
|
-
function peek() {
|
|
249
|
-
if (isCRLF(_index + _peekOffset)) {
|
|
250
|
-
_peekOffset++;
|
|
251
|
-
}
|
|
252
|
-
_peekOffset++;
|
|
253
|
-
return _buf[_index + _peekOffset];
|
|
254
|
-
}
|
|
255
|
-
function reset() {
|
|
256
|
-
_index = 0;
|
|
257
|
-
_line = 1;
|
|
258
|
-
_column = 1;
|
|
259
|
-
_peekOffset = 0;
|
|
260
|
-
}
|
|
261
|
-
function resetPeek(offset = 0) {
|
|
262
|
-
_peekOffset = offset;
|
|
263
|
-
}
|
|
264
|
-
function skipToPeek() {
|
|
265
|
-
const target = _index + _peekOffset;
|
|
266
|
-
// eslint-disable-next-line no-unmodified-loop-condition
|
|
267
|
-
while (target !== _index) {
|
|
268
|
-
next();
|
|
269
|
-
}
|
|
270
|
-
_peekOffset = 0;
|
|
271
|
-
}
|
|
272
|
-
return {
|
|
273
|
-
index,
|
|
274
|
-
line,
|
|
275
|
-
column,
|
|
276
|
-
peekOffset,
|
|
277
|
-
charAt,
|
|
278
|
-
currentChar,
|
|
279
|
-
currentPeek,
|
|
280
|
-
next,
|
|
281
|
-
peek,
|
|
282
|
-
reset,
|
|
283
|
-
resetPeek,
|
|
284
|
-
skipToPeek
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const EOF = undefined;
|
|
289
|
-
const LITERAL_DELIMITER = "'";
|
|
290
|
-
const ERROR_DOMAIN$1 = 'tokenizer';
|
|
291
|
-
function createTokenizer(source, options = {}) {
|
|
292
|
-
const location = options.location !== false;
|
|
293
|
-
const _scnr = createScanner(source);
|
|
294
|
-
const currentOffset = () => _scnr.index();
|
|
295
|
-
const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
|
|
296
|
-
const _initLoc = currentPosition();
|
|
297
|
-
const _initOffset = currentOffset();
|
|
298
|
-
const _context = {
|
|
299
|
-
currentType: 14 /* TokenTypes.EOF */,
|
|
300
|
-
offset: _initOffset,
|
|
301
|
-
startLoc: _initLoc,
|
|
302
|
-
endLoc: _initLoc,
|
|
303
|
-
lastType: 14 /* TokenTypes.EOF */,
|
|
304
|
-
lastOffset: _initOffset,
|
|
305
|
-
lastStartLoc: _initLoc,
|
|
306
|
-
lastEndLoc: _initLoc,
|
|
307
|
-
braceNest: 0,
|
|
308
|
-
inLinked: false,
|
|
309
|
-
text: ''
|
|
310
|
-
};
|
|
311
|
-
const context = () => _context;
|
|
312
|
-
const { onError } = options;
|
|
313
|
-
function emitError(code, pos, offset, ...args) {
|
|
314
|
-
const ctx = context();
|
|
315
|
-
pos.column += offset;
|
|
316
|
-
pos.offset += offset;
|
|
317
|
-
if (onError) {
|
|
318
|
-
const loc = location ? createLocation(ctx.startLoc, pos) : null;
|
|
319
|
-
const err = createCompileError(code, loc, {
|
|
320
|
-
domain: ERROR_DOMAIN$1,
|
|
321
|
-
args
|
|
322
|
-
});
|
|
323
|
-
onError(err);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
function getToken(context, type, value) {
|
|
327
|
-
context.endLoc = currentPosition();
|
|
328
|
-
context.currentType = type;
|
|
329
|
-
const token = { type };
|
|
330
|
-
if (location) {
|
|
331
|
-
token.loc = createLocation(context.startLoc, context.endLoc);
|
|
332
|
-
}
|
|
333
|
-
if (value != null) {
|
|
334
|
-
token.value = value;
|
|
335
|
-
}
|
|
336
|
-
return token;
|
|
337
|
-
}
|
|
338
|
-
const getEndToken = (context) => getToken(context, 14 /* TokenTypes.EOF */);
|
|
339
|
-
function eat(scnr, ch) {
|
|
340
|
-
if (scnr.currentChar() === ch) {
|
|
341
|
-
scnr.next();
|
|
342
|
-
return ch;
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
|
|
346
|
-
return '';
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
function peekSpaces(scnr) {
|
|
350
|
-
let buf = '';
|
|
351
|
-
while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
|
|
352
|
-
buf += scnr.currentPeek();
|
|
353
|
-
scnr.peek();
|
|
354
|
-
}
|
|
355
|
-
return buf;
|
|
356
|
-
}
|
|
357
|
-
function skipSpaces(scnr) {
|
|
358
|
-
const buf = peekSpaces(scnr);
|
|
359
|
-
scnr.skipToPeek();
|
|
360
|
-
return buf;
|
|
361
|
-
}
|
|
362
|
-
function isIdentifierStart(ch) {
|
|
363
|
-
if (ch === EOF) {
|
|
364
|
-
return false;
|
|
365
|
-
}
|
|
366
|
-
const cc = ch.charCodeAt(0);
|
|
367
|
-
return ((cc >= 97 && cc <= 122) || // a-z
|
|
368
|
-
(cc >= 65 && cc <= 90) || // A-Z
|
|
369
|
-
cc === 95 // _
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
function isNumberStart(ch) {
|
|
373
|
-
if (ch === EOF) {
|
|
374
|
-
return false;
|
|
375
|
-
}
|
|
376
|
-
const cc = ch.charCodeAt(0);
|
|
377
|
-
return cc >= 48 && cc <= 57; // 0-9
|
|
378
|
-
}
|
|
379
|
-
function isNamedIdentifierStart(scnr, context) {
|
|
380
|
-
const { currentType } = context;
|
|
381
|
-
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
384
|
-
peekSpaces(scnr);
|
|
385
|
-
const ret = isIdentifierStart(scnr.currentPeek());
|
|
386
|
-
scnr.resetPeek();
|
|
387
|
-
return ret;
|
|
388
|
-
}
|
|
389
|
-
function isListIdentifierStart(scnr, context) {
|
|
390
|
-
const { currentType } = context;
|
|
391
|
-
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
392
|
-
return false;
|
|
393
|
-
}
|
|
394
|
-
peekSpaces(scnr);
|
|
395
|
-
const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
|
|
396
|
-
const ret = isNumberStart(ch);
|
|
397
|
-
scnr.resetPeek();
|
|
398
|
-
return ret;
|
|
399
|
-
}
|
|
400
|
-
function isLiteralStart(scnr, context) {
|
|
401
|
-
const { currentType } = context;
|
|
402
|
-
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
403
|
-
return false;
|
|
404
|
-
}
|
|
405
|
-
peekSpaces(scnr);
|
|
406
|
-
const ret = scnr.currentPeek() === LITERAL_DELIMITER;
|
|
407
|
-
scnr.resetPeek();
|
|
408
|
-
return ret;
|
|
409
|
-
}
|
|
410
|
-
function isLinkedDotStart(scnr, context) {
|
|
411
|
-
const { currentType } = context;
|
|
412
|
-
if (currentType !== 8 /* TokenTypes.LinkedAlias */) {
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
415
|
-
peekSpaces(scnr);
|
|
416
|
-
const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
|
|
417
|
-
scnr.resetPeek();
|
|
418
|
-
return ret;
|
|
419
|
-
}
|
|
420
|
-
function isLinkedModifierStart(scnr, context) {
|
|
421
|
-
const { currentType } = context;
|
|
422
|
-
if (currentType !== 9 /* TokenTypes.LinkedDot */) {
|
|
423
|
-
return false;
|
|
424
|
-
}
|
|
425
|
-
peekSpaces(scnr);
|
|
426
|
-
const ret = isIdentifierStart(scnr.currentPeek());
|
|
427
|
-
scnr.resetPeek();
|
|
428
|
-
return ret;
|
|
429
|
-
}
|
|
430
|
-
function isLinkedDelimiterStart(scnr, context) {
|
|
431
|
-
const { currentType } = context;
|
|
432
|
-
if (!(currentType === 8 /* TokenTypes.LinkedAlias */ ||
|
|
433
|
-
currentType === 12 /* TokenTypes.LinkedModifier */)) {
|
|
434
|
-
return false;
|
|
435
|
-
}
|
|
436
|
-
peekSpaces(scnr);
|
|
437
|
-
const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
|
|
438
|
-
scnr.resetPeek();
|
|
439
|
-
return ret;
|
|
440
|
-
}
|
|
441
|
-
function isLinkedReferStart(scnr, context) {
|
|
442
|
-
const { currentType } = context;
|
|
443
|
-
if (currentType !== 10 /* TokenTypes.LinkedDelimiter */) {
|
|
444
|
-
return false;
|
|
445
|
-
}
|
|
446
|
-
const fn = () => {
|
|
447
|
-
const ch = scnr.currentPeek();
|
|
448
|
-
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
449
|
-
return isIdentifierStart(scnr.peek());
|
|
450
|
-
}
|
|
451
|
-
else if (ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
452
|
-
ch === "%" /* TokenChars.Modulo */ ||
|
|
453
|
-
ch === "|" /* TokenChars.Pipe */ ||
|
|
454
|
-
ch === ":" /* TokenChars.LinkedDelimiter */ ||
|
|
455
|
-
ch === "." /* TokenChars.LinkedDot */ ||
|
|
456
|
-
ch === CHAR_SP ||
|
|
457
|
-
!ch) {
|
|
458
|
-
return false;
|
|
459
|
-
}
|
|
460
|
-
else if (ch === CHAR_LF) {
|
|
461
|
-
scnr.peek();
|
|
462
|
-
return fn();
|
|
463
|
-
}
|
|
464
|
-
else {
|
|
465
|
-
// other characters
|
|
466
|
-
return isIdentifierStart(ch);
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
const ret = fn();
|
|
470
|
-
scnr.resetPeek();
|
|
471
|
-
return ret;
|
|
472
|
-
}
|
|
473
|
-
function isPluralStart(scnr) {
|
|
474
|
-
peekSpaces(scnr);
|
|
475
|
-
const ret = scnr.currentPeek() === "|" /* TokenChars.Pipe */;
|
|
476
|
-
scnr.resetPeek();
|
|
477
|
-
return ret;
|
|
478
|
-
}
|
|
479
|
-
function detectModuloStart(scnr) {
|
|
480
|
-
const spaces = peekSpaces(scnr);
|
|
481
|
-
const ret = scnr.currentPeek() === "%" /* TokenChars.Modulo */ &&
|
|
482
|
-
scnr.peek() === "{" /* TokenChars.BraceLeft */;
|
|
483
|
-
scnr.resetPeek();
|
|
484
|
-
return {
|
|
485
|
-
isModulo: ret,
|
|
486
|
-
hasSpace: spaces.length > 0
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
function isTextStart(scnr, reset = true) {
|
|
490
|
-
const fn = (hasSpace = false, prev = '', detectModulo = false) => {
|
|
491
|
-
const ch = scnr.currentPeek();
|
|
492
|
-
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
493
|
-
return prev === "%" /* TokenChars.Modulo */ ? false : hasSpace;
|
|
494
|
-
}
|
|
495
|
-
else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
|
|
496
|
-
return prev === "%" /* TokenChars.Modulo */ ? true : hasSpace;
|
|
497
|
-
}
|
|
498
|
-
else if (ch === "%" /* TokenChars.Modulo */) {
|
|
499
|
-
scnr.peek();
|
|
500
|
-
return fn(hasSpace, "%" /* TokenChars.Modulo */, true);
|
|
501
|
-
}
|
|
502
|
-
else if (ch === "|" /* TokenChars.Pipe */) {
|
|
503
|
-
return prev === "%" /* TokenChars.Modulo */ || detectModulo
|
|
504
|
-
? true
|
|
505
|
-
: !(prev === CHAR_SP || prev === CHAR_LF);
|
|
506
|
-
}
|
|
507
|
-
else if (ch === CHAR_SP) {
|
|
508
|
-
scnr.peek();
|
|
509
|
-
return fn(true, CHAR_SP, detectModulo);
|
|
510
|
-
}
|
|
511
|
-
else if (ch === CHAR_LF) {
|
|
512
|
-
scnr.peek();
|
|
513
|
-
return fn(true, CHAR_LF, detectModulo);
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
return true;
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
const ret = fn();
|
|
520
|
-
reset && scnr.resetPeek();
|
|
521
|
-
return ret;
|
|
522
|
-
}
|
|
523
|
-
function takeChar(scnr, fn) {
|
|
524
|
-
const ch = scnr.currentChar();
|
|
525
|
-
if (ch === EOF) {
|
|
526
|
-
return EOF;
|
|
527
|
-
}
|
|
528
|
-
if (fn(ch)) {
|
|
529
|
-
scnr.next();
|
|
530
|
-
return ch;
|
|
531
|
-
}
|
|
532
|
-
return null;
|
|
533
|
-
}
|
|
534
|
-
function takeIdentifierChar(scnr) {
|
|
535
|
-
const closure = (ch) => {
|
|
536
|
-
const cc = ch.charCodeAt(0);
|
|
537
|
-
return ((cc >= 97 && cc <= 122) || // a-z
|
|
538
|
-
(cc >= 65 && cc <= 90) || // A-Z
|
|
539
|
-
(cc >= 48 && cc <= 57) || // 0-9
|
|
540
|
-
cc === 95 || // _
|
|
541
|
-
cc === 36 // $
|
|
542
|
-
);
|
|
543
|
-
};
|
|
544
|
-
return takeChar(scnr, closure);
|
|
545
|
-
}
|
|
546
|
-
function takeDigit(scnr) {
|
|
547
|
-
const closure = (ch) => {
|
|
548
|
-
const cc = ch.charCodeAt(0);
|
|
549
|
-
return cc >= 48 && cc <= 57; // 0-9
|
|
550
|
-
};
|
|
551
|
-
return takeChar(scnr, closure);
|
|
552
|
-
}
|
|
553
|
-
function takeHexDigit(scnr) {
|
|
554
|
-
const closure = (ch) => {
|
|
555
|
-
const cc = ch.charCodeAt(0);
|
|
556
|
-
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
557
|
-
(cc >= 65 && cc <= 70) || // A-F
|
|
558
|
-
(cc >= 97 && cc <= 102)); // a-f
|
|
559
|
-
};
|
|
560
|
-
return takeChar(scnr, closure);
|
|
561
|
-
}
|
|
562
|
-
function getDigits(scnr) {
|
|
563
|
-
let ch = '';
|
|
564
|
-
let num = '';
|
|
565
|
-
while ((ch = takeDigit(scnr))) {
|
|
566
|
-
num += ch;
|
|
567
|
-
}
|
|
568
|
-
return num;
|
|
569
|
-
}
|
|
570
|
-
function readModulo(scnr) {
|
|
571
|
-
skipSpaces(scnr);
|
|
572
|
-
const ch = scnr.currentChar();
|
|
573
|
-
if (ch !== "%" /* TokenChars.Modulo */) {
|
|
574
|
-
emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
|
|
575
|
-
}
|
|
576
|
-
scnr.next();
|
|
577
|
-
return "%" /* TokenChars.Modulo */;
|
|
578
|
-
}
|
|
579
|
-
function readText(scnr) {
|
|
580
|
-
let buf = '';
|
|
581
|
-
while (true) {
|
|
582
|
-
const ch = scnr.currentChar();
|
|
583
|
-
if (ch === "{" /* TokenChars.BraceLeft */ ||
|
|
584
|
-
ch === "}" /* TokenChars.BraceRight */ ||
|
|
585
|
-
ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
586
|
-
ch === "|" /* TokenChars.Pipe */ ||
|
|
587
|
-
!ch) {
|
|
588
|
-
break;
|
|
589
|
-
}
|
|
590
|
-
else if (ch === "%" /* TokenChars.Modulo */) {
|
|
591
|
-
if (isTextStart(scnr)) {
|
|
592
|
-
buf += ch;
|
|
593
|
-
scnr.next();
|
|
594
|
-
}
|
|
595
|
-
else {
|
|
596
|
-
break;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
else if (ch === CHAR_SP || ch === CHAR_LF) {
|
|
600
|
-
if (isTextStart(scnr)) {
|
|
601
|
-
buf += ch;
|
|
602
|
-
scnr.next();
|
|
603
|
-
}
|
|
604
|
-
else if (isPluralStart(scnr)) {
|
|
605
|
-
break;
|
|
606
|
-
}
|
|
607
|
-
else {
|
|
608
|
-
buf += ch;
|
|
609
|
-
scnr.next();
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
buf += ch;
|
|
614
|
-
scnr.next();
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return buf;
|
|
618
|
-
}
|
|
619
|
-
function readNamedIdentifier(scnr) {
|
|
620
|
-
skipSpaces(scnr);
|
|
621
|
-
let ch = '';
|
|
622
|
-
let name = '';
|
|
623
|
-
while ((ch = takeIdentifierChar(scnr))) {
|
|
624
|
-
name += ch;
|
|
625
|
-
}
|
|
626
|
-
if (scnr.currentChar() === EOF) {
|
|
627
|
-
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
628
|
-
}
|
|
629
|
-
return name;
|
|
630
|
-
}
|
|
631
|
-
function readListIdentifier(scnr) {
|
|
632
|
-
skipSpaces(scnr);
|
|
633
|
-
let value = '';
|
|
634
|
-
if (scnr.currentChar() === '-') {
|
|
635
|
-
scnr.next();
|
|
636
|
-
value += `-${getDigits(scnr)}`;
|
|
637
|
-
}
|
|
638
|
-
else {
|
|
639
|
-
value += getDigits(scnr);
|
|
640
|
-
}
|
|
641
|
-
if (scnr.currentChar() === EOF) {
|
|
642
|
-
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
643
|
-
}
|
|
644
|
-
return value;
|
|
645
|
-
}
|
|
646
|
-
function readLiteral(scnr) {
|
|
647
|
-
skipSpaces(scnr);
|
|
648
|
-
eat(scnr, `\'`);
|
|
649
|
-
let ch = '';
|
|
650
|
-
let literal = '';
|
|
651
|
-
const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
|
|
652
|
-
while ((ch = takeChar(scnr, fn))) {
|
|
653
|
-
if (ch === '\\') {
|
|
654
|
-
literal += readEscapeSequence(scnr);
|
|
655
|
-
}
|
|
656
|
-
else {
|
|
657
|
-
literal += ch;
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
const current = scnr.currentChar();
|
|
661
|
-
if (current === CHAR_LF || current === EOF) {
|
|
662
|
-
emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
|
|
663
|
-
// TODO: Is it correct really?
|
|
664
|
-
if (current === CHAR_LF) {
|
|
665
|
-
scnr.next();
|
|
666
|
-
eat(scnr, `\'`);
|
|
667
|
-
}
|
|
668
|
-
return literal;
|
|
669
|
-
}
|
|
670
|
-
eat(scnr, `\'`);
|
|
671
|
-
return literal;
|
|
672
|
-
}
|
|
673
|
-
function readEscapeSequence(scnr) {
|
|
674
|
-
const ch = scnr.currentChar();
|
|
675
|
-
switch (ch) {
|
|
676
|
-
case '\\':
|
|
677
|
-
case `\'`:
|
|
678
|
-
scnr.next();
|
|
679
|
-
return `\\${ch}`;
|
|
680
|
-
case 'u':
|
|
681
|
-
return readUnicodeEscapeSequence(scnr, ch, 4);
|
|
682
|
-
case 'U':
|
|
683
|
-
return readUnicodeEscapeSequence(scnr, ch, 6);
|
|
684
|
-
default:
|
|
685
|
-
emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
|
|
686
|
-
return '';
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
function readUnicodeEscapeSequence(scnr, unicode, digits) {
|
|
690
|
-
eat(scnr, unicode);
|
|
691
|
-
let sequence = '';
|
|
692
|
-
for (let i = 0; i < digits; i++) {
|
|
693
|
-
const ch = takeHexDigit(scnr);
|
|
694
|
-
if (!ch) {
|
|
695
|
-
emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
|
|
696
|
-
break;
|
|
697
|
-
}
|
|
698
|
-
sequence += ch;
|
|
699
|
-
}
|
|
700
|
-
return `\\${unicode}${sequence}`;
|
|
701
|
-
}
|
|
702
|
-
function readInvalidIdentifier(scnr) {
|
|
703
|
-
skipSpaces(scnr);
|
|
704
|
-
let ch = '';
|
|
705
|
-
let identifiers = '';
|
|
706
|
-
const closure = (ch) => ch !== "{" /* TokenChars.BraceLeft */ &&
|
|
707
|
-
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
708
|
-
ch !== CHAR_SP &&
|
|
709
|
-
ch !== CHAR_LF;
|
|
710
|
-
while ((ch = takeChar(scnr, closure))) {
|
|
711
|
-
identifiers += ch;
|
|
712
|
-
}
|
|
713
|
-
return identifiers;
|
|
714
|
-
}
|
|
715
|
-
function readLinkedModifier(scnr) {
|
|
716
|
-
let ch = '';
|
|
717
|
-
let name = '';
|
|
718
|
-
while ((ch = takeIdentifierChar(scnr))) {
|
|
719
|
-
name += ch;
|
|
720
|
-
}
|
|
721
|
-
return name;
|
|
722
|
-
}
|
|
723
|
-
function readLinkedRefer(scnr) {
|
|
724
|
-
const fn = (detect = false, buf) => {
|
|
725
|
-
const ch = scnr.currentChar();
|
|
726
|
-
if (ch === "{" /* TokenChars.BraceLeft */ ||
|
|
727
|
-
ch === "%" /* TokenChars.Modulo */ ||
|
|
728
|
-
ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
729
|
-
ch === "|" /* TokenChars.Pipe */ ||
|
|
730
|
-
!ch) {
|
|
731
|
-
return buf;
|
|
732
|
-
}
|
|
733
|
-
else if (ch === CHAR_SP) {
|
|
734
|
-
return buf;
|
|
735
|
-
}
|
|
736
|
-
else if (ch === CHAR_LF) {
|
|
737
|
-
buf += ch;
|
|
738
|
-
scnr.next();
|
|
739
|
-
return fn(detect, buf);
|
|
740
|
-
}
|
|
741
|
-
else {
|
|
742
|
-
buf += ch;
|
|
743
|
-
scnr.next();
|
|
744
|
-
return fn(true, buf);
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
return fn(false, '');
|
|
748
|
-
}
|
|
749
|
-
function readPlural(scnr) {
|
|
750
|
-
skipSpaces(scnr);
|
|
751
|
-
const plural = eat(scnr, "|" /* TokenChars.Pipe */);
|
|
752
|
-
skipSpaces(scnr);
|
|
753
|
-
return plural;
|
|
754
|
-
}
|
|
755
|
-
// TODO: We need refactoring of token parsing ...
|
|
756
|
-
function readTokenInPlaceholder(scnr, context) {
|
|
757
|
-
let token = null;
|
|
758
|
-
const ch = scnr.currentChar();
|
|
759
|
-
switch (ch) {
|
|
760
|
-
case "{" /* TokenChars.BraceLeft */:
|
|
761
|
-
if (context.braceNest >= 1) {
|
|
762
|
-
emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
|
|
763
|
-
}
|
|
764
|
-
scnr.next();
|
|
765
|
-
token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
|
|
766
|
-
skipSpaces(scnr);
|
|
767
|
-
context.braceNest++;
|
|
768
|
-
return token;
|
|
769
|
-
case "}" /* TokenChars.BraceRight */:
|
|
770
|
-
if (context.braceNest > 0 &&
|
|
771
|
-
context.currentType === 2 /* TokenTypes.BraceLeft */) {
|
|
772
|
-
emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
|
|
773
|
-
}
|
|
774
|
-
scnr.next();
|
|
775
|
-
token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
776
|
-
context.braceNest--;
|
|
777
|
-
context.braceNest > 0 && skipSpaces(scnr);
|
|
778
|
-
if (context.inLinked && context.braceNest === 0) {
|
|
779
|
-
context.inLinked = false;
|
|
780
|
-
}
|
|
781
|
-
return token;
|
|
782
|
-
case "@" /* TokenChars.LinkedAlias */:
|
|
783
|
-
if (context.braceNest > 0) {
|
|
784
|
-
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
785
|
-
}
|
|
786
|
-
token = readTokenInLinked(scnr, context) || getEndToken(context);
|
|
787
|
-
context.braceNest = 0;
|
|
788
|
-
return token;
|
|
789
|
-
default:
|
|
790
|
-
let validNamedIdentifier = true;
|
|
791
|
-
let validListIdentifier = true;
|
|
792
|
-
let validLiteral = true;
|
|
793
|
-
if (isPluralStart(scnr)) {
|
|
794
|
-
if (context.braceNest > 0) {
|
|
795
|
-
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
796
|
-
}
|
|
797
|
-
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
798
|
-
// reset
|
|
799
|
-
context.braceNest = 0;
|
|
800
|
-
context.inLinked = false;
|
|
801
|
-
return token;
|
|
802
|
-
}
|
|
803
|
-
if (context.braceNest > 0 &&
|
|
804
|
-
(context.currentType === 5 /* TokenTypes.Named */ ||
|
|
805
|
-
context.currentType === 6 /* TokenTypes.List */ ||
|
|
806
|
-
context.currentType === 7 /* TokenTypes.Literal */)) {
|
|
807
|
-
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
808
|
-
context.braceNest = 0;
|
|
809
|
-
return readToken(scnr, context);
|
|
810
|
-
}
|
|
811
|
-
if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
|
|
812
|
-
token = getToken(context, 5 /* TokenTypes.Named */, readNamedIdentifier(scnr));
|
|
813
|
-
skipSpaces(scnr);
|
|
814
|
-
return token;
|
|
815
|
-
}
|
|
816
|
-
if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
|
|
817
|
-
token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
|
|
818
|
-
skipSpaces(scnr);
|
|
819
|
-
return token;
|
|
820
|
-
}
|
|
821
|
-
if ((validLiteral = isLiteralStart(scnr, context))) {
|
|
822
|
-
token = getToken(context, 7 /* TokenTypes.Literal */, readLiteral(scnr));
|
|
823
|
-
skipSpaces(scnr);
|
|
824
|
-
return token;
|
|
825
|
-
}
|
|
826
|
-
if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
|
|
827
|
-
// TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
|
|
828
|
-
token = getToken(context, 13 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
|
|
829
|
-
emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
|
|
830
|
-
skipSpaces(scnr);
|
|
831
|
-
return token;
|
|
832
|
-
}
|
|
833
|
-
break;
|
|
834
|
-
}
|
|
835
|
-
return token;
|
|
836
|
-
}
|
|
837
|
-
// TODO: We need refactoring of token parsing ...
|
|
838
|
-
function readTokenInLinked(scnr, context) {
|
|
839
|
-
const { currentType } = context;
|
|
840
|
-
let token = null;
|
|
841
|
-
const ch = scnr.currentChar();
|
|
842
|
-
if ((currentType === 8 /* TokenTypes.LinkedAlias */ ||
|
|
843
|
-
currentType === 9 /* TokenTypes.LinkedDot */ ||
|
|
844
|
-
currentType === 12 /* TokenTypes.LinkedModifier */ ||
|
|
845
|
-
currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
|
|
846
|
-
(ch === CHAR_LF || ch === CHAR_SP)) {
|
|
847
|
-
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
848
|
-
}
|
|
849
|
-
switch (ch) {
|
|
850
|
-
case "@" /* TokenChars.LinkedAlias */:
|
|
851
|
-
scnr.next();
|
|
852
|
-
token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
|
|
853
|
-
context.inLinked = true;
|
|
854
|
-
return token;
|
|
855
|
-
case "." /* TokenChars.LinkedDot */:
|
|
856
|
-
skipSpaces(scnr);
|
|
857
|
-
scnr.next();
|
|
858
|
-
return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
|
|
859
|
-
case ":" /* TokenChars.LinkedDelimiter */:
|
|
860
|
-
skipSpaces(scnr);
|
|
861
|
-
scnr.next();
|
|
862
|
-
return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
|
|
863
|
-
default:
|
|
864
|
-
if (isPluralStart(scnr)) {
|
|
865
|
-
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
866
|
-
// reset
|
|
867
|
-
context.braceNest = 0;
|
|
868
|
-
context.inLinked = false;
|
|
869
|
-
return token;
|
|
870
|
-
}
|
|
871
|
-
if (isLinkedDotStart(scnr, context) ||
|
|
872
|
-
isLinkedDelimiterStart(scnr, context)) {
|
|
873
|
-
skipSpaces(scnr);
|
|
874
|
-
return readTokenInLinked(scnr, context);
|
|
875
|
-
}
|
|
876
|
-
if (isLinkedModifierStart(scnr, context)) {
|
|
877
|
-
skipSpaces(scnr);
|
|
878
|
-
return getToken(context, 12 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
|
|
879
|
-
}
|
|
880
|
-
if (isLinkedReferStart(scnr, context)) {
|
|
881
|
-
skipSpaces(scnr);
|
|
882
|
-
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
883
|
-
// scan the placeholder
|
|
884
|
-
return readTokenInPlaceholder(scnr, context) || token;
|
|
885
|
-
}
|
|
886
|
-
else {
|
|
887
|
-
return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
if (currentType === 8 /* TokenTypes.LinkedAlias */) {
|
|
891
|
-
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
892
|
-
}
|
|
893
|
-
context.braceNest = 0;
|
|
894
|
-
context.inLinked = false;
|
|
895
|
-
return readToken(scnr, context);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
// TODO: We need refactoring of token parsing ...
|
|
899
|
-
function readToken(scnr, context) {
|
|
900
|
-
let token = { type: 14 /* TokenTypes.EOF */ };
|
|
901
|
-
if (context.braceNest > 0) {
|
|
902
|
-
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
903
|
-
}
|
|
904
|
-
if (context.inLinked) {
|
|
905
|
-
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
906
|
-
}
|
|
907
|
-
const ch = scnr.currentChar();
|
|
908
|
-
switch (ch) {
|
|
909
|
-
case "{" /* TokenChars.BraceLeft */:
|
|
910
|
-
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
911
|
-
case "}" /* TokenChars.BraceRight */:
|
|
912
|
-
emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
|
|
913
|
-
scnr.next();
|
|
914
|
-
return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
915
|
-
case "@" /* TokenChars.LinkedAlias */:
|
|
916
|
-
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
917
|
-
default:
|
|
918
|
-
if (isPluralStart(scnr)) {
|
|
919
|
-
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
920
|
-
// reset
|
|
921
|
-
context.braceNest = 0;
|
|
922
|
-
context.inLinked = false;
|
|
923
|
-
return token;
|
|
924
|
-
}
|
|
925
|
-
const { isModulo, hasSpace } = detectModuloStart(scnr);
|
|
926
|
-
if (isModulo) {
|
|
927
|
-
return hasSpace
|
|
928
|
-
? getToken(context, 0 /* TokenTypes.Text */, readText(scnr))
|
|
929
|
-
: getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
|
|
930
|
-
}
|
|
931
|
-
if (isTextStart(scnr)) {
|
|
932
|
-
return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
|
|
933
|
-
}
|
|
934
|
-
break;
|
|
935
|
-
}
|
|
936
|
-
return token;
|
|
937
|
-
}
|
|
938
|
-
function nextToken() {
|
|
939
|
-
const { currentType, offset, startLoc, endLoc } = _context;
|
|
940
|
-
_context.lastType = currentType;
|
|
941
|
-
_context.lastOffset = offset;
|
|
942
|
-
_context.lastStartLoc = startLoc;
|
|
943
|
-
_context.lastEndLoc = endLoc;
|
|
944
|
-
_context.offset = currentOffset();
|
|
945
|
-
_context.startLoc = currentPosition();
|
|
946
|
-
if (_scnr.currentChar() === EOF) {
|
|
947
|
-
return getToken(_context, 14 /* TokenTypes.EOF */);
|
|
948
|
-
}
|
|
949
|
-
return readToken(_scnr, _context);
|
|
950
|
-
}
|
|
951
|
-
return {
|
|
952
|
-
nextToken,
|
|
953
|
-
currentOffset,
|
|
954
|
-
currentPosition,
|
|
955
|
-
context
|
|
956
|
-
};
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
const ERROR_DOMAIN = 'parser';
|
|
960
|
-
// Backslash backslash, backslash quote, uHHHH, UHHHHHH.
|
|
961
|
-
const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
|
|
962
|
-
function fromEscapeSequence(match, codePoint4, codePoint6) {
|
|
963
|
-
switch (match) {
|
|
964
|
-
case `\\\\`:
|
|
965
|
-
return `\\`;
|
|
966
|
-
case `\\\'`:
|
|
967
|
-
return `\'`;
|
|
968
|
-
default: {
|
|
969
|
-
const codePoint = parseInt(codePoint4 || codePoint6, 16);
|
|
970
|
-
if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
|
|
971
|
-
return String.fromCodePoint(codePoint);
|
|
972
|
-
}
|
|
973
|
-
// invalid ...
|
|
974
|
-
// Replace them with U+FFFD REPLACEMENT CHARACTER.
|
|
975
|
-
return '�';
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
function createParser(options = {}) {
|
|
980
|
-
const location = options.location !== false;
|
|
981
|
-
const { onError } = options;
|
|
982
|
-
function emitError(tokenzer, code, start, offset, ...args) {
|
|
983
|
-
const end = tokenzer.currentPosition();
|
|
984
|
-
end.offset += offset;
|
|
985
|
-
end.column += offset;
|
|
986
|
-
if (onError) {
|
|
987
|
-
const loc = location ? createLocation(start, end) : null;
|
|
988
|
-
const err = createCompileError(code, loc, {
|
|
989
|
-
domain: ERROR_DOMAIN,
|
|
990
|
-
args
|
|
991
|
-
});
|
|
992
|
-
onError(err);
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
function startNode(type, offset, loc) {
|
|
996
|
-
const node = { type };
|
|
997
|
-
if (location) {
|
|
998
|
-
node.start = offset;
|
|
999
|
-
node.end = offset;
|
|
1000
|
-
node.loc = { start: loc, end: loc };
|
|
1001
|
-
}
|
|
1002
|
-
return node;
|
|
1003
|
-
}
|
|
1004
|
-
function endNode(node, offset, pos, type) {
|
|
1005
|
-
if (type) {
|
|
1006
|
-
node.type = type;
|
|
1007
|
-
}
|
|
1008
|
-
if (location) {
|
|
1009
|
-
node.end = offset;
|
|
1010
|
-
if (node.loc) {
|
|
1011
|
-
node.loc.end = pos;
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
function parseText(tokenizer, value) {
|
|
1016
|
-
const context = tokenizer.context();
|
|
1017
|
-
const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
|
|
1018
|
-
node.value = value;
|
|
1019
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1020
|
-
return node;
|
|
1021
|
-
}
|
|
1022
|
-
function parseList(tokenizer, index) {
|
|
1023
|
-
const context = tokenizer.context();
|
|
1024
|
-
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1025
|
-
const node = startNode(5 /* NodeTypes.List */, offset, loc);
|
|
1026
|
-
node.index = parseInt(index, 10);
|
|
1027
|
-
tokenizer.nextToken(); // skip brach right
|
|
1028
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1029
|
-
return node;
|
|
1030
|
-
}
|
|
1031
|
-
function parseNamed(tokenizer, key) {
|
|
1032
|
-
const context = tokenizer.context();
|
|
1033
|
-
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1034
|
-
const node = startNode(4 /* NodeTypes.Named */, offset, loc);
|
|
1035
|
-
node.key = key;
|
|
1036
|
-
tokenizer.nextToken(); // skip brach right
|
|
1037
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1038
|
-
return node;
|
|
1039
|
-
}
|
|
1040
|
-
function parseLiteral(tokenizer, value) {
|
|
1041
|
-
const context = tokenizer.context();
|
|
1042
|
-
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1043
|
-
const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
|
|
1044
|
-
node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
|
|
1045
|
-
tokenizer.nextToken(); // skip brach right
|
|
1046
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1047
|
-
return node;
|
|
1048
|
-
}
|
|
1049
|
-
function parseLinkedModifier(tokenizer) {
|
|
1050
|
-
const token = tokenizer.nextToken();
|
|
1051
|
-
const context = tokenizer.context();
|
|
1052
|
-
const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
|
|
1053
|
-
const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
|
|
1054
|
-
if (token.type !== 12 /* TokenTypes.LinkedModifier */) {
|
|
1055
|
-
// empty modifier
|
|
1056
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
|
|
1057
|
-
node.value = '';
|
|
1058
|
-
endNode(node, offset, loc);
|
|
1059
|
-
return {
|
|
1060
|
-
nextConsumeToken: token,
|
|
1061
|
-
node
|
|
1062
|
-
};
|
|
1063
|
-
}
|
|
1064
|
-
// check token
|
|
1065
|
-
if (token.value == null) {
|
|
1066
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1067
|
-
}
|
|
1068
|
-
node.value = token.value || '';
|
|
1069
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1070
|
-
return {
|
|
1071
|
-
node
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
function parseLinkedKey(tokenizer, value) {
|
|
1075
|
-
const context = tokenizer.context();
|
|
1076
|
-
const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
|
|
1077
|
-
node.value = value;
|
|
1078
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1079
|
-
return node;
|
|
1080
|
-
}
|
|
1081
|
-
function parseLinked(tokenizer) {
|
|
1082
|
-
const context = tokenizer.context();
|
|
1083
|
-
const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
|
|
1084
|
-
let token = tokenizer.nextToken();
|
|
1085
|
-
if (token.type === 9 /* TokenTypes.LinkedDot */) {
|
|
1086
|
-
const parsed = parseLinkedModifier(tokenizer);
|
|
1087
|
-
linkedNode.modifier = parsed.node;
|
|
1088
|
-
token = parsed.nextConsumeToken || tokenizer.nextToken();
|
|
1089
|
-
}
|
|
1090
|
-
// asset check token
|
|
1091
|
-
if (token.type !== 10 /* TokenTypes.LinkedDelimiter */) {
|
|
1092
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1093
|
-
}
|
|
1094
|
-
token = tokenizer.nextToken();
|
|
1095
|
-
// skip brace left
|
|
1096
|
-
if (token.type === 2 /* TokenTypes.BraceLeft */) {
|
|
1097
|
-
token = tokenizer.nextToken();
|
|
1098
|
-
}
|
|
1099
|
-
switch (token.type) {
|
|
1100
|
-
case 11 /* TokenTypes.LinkedKey */:
|
|
1101
|
-
if (token.value == null) {
|
|
1102
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1103
|
-
}
|
|
1104
|
-
linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
|
|
1105
|
-
break;
|
|
1106
|
-
case 5 /* TokenTypes.Named */:
|
|
1107
|
-
if (token.value == null) {
|
|
1108
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1109
|
-
}
|
|
1110
|
-
linkedNode.key = parseNamed(tokenizer, token.value || '');
|
|
1111
|
-
break;
|
|
1112
|
-
case 6 /* TokenTypes.List */:
|
|
1113
|
-
if (token.value == null) {
|
|
1114
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1115
|
-
}
|
|
1116
|
-
linkedNode.key = parseList(tokenizer, token.value || '');
|
|
1117
|
-
break;
|
|
1118
|
-
case 7 /* TokenTypes.Literal */:
|
|
1119
|
-
if (token.value == null) {
|
|
1120
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1121
|
-
}
|
|
1122
|
-
linkedNode.key = parseLiteral(tokenizer, token.value || '');
|
|
1123
|
-
break;
|
|
1124
|
-
default:
|
|
1125
|
-
// empty key
|
|
1126
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
|
|
1127
|
-
const nextContext = tokenizer.context();
|
|
1128
|
-
const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
|
|
1129
|
-
emptyLinkedKeyNode.value = '';
|
|
1130
|
-
endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
|
|
1131
|
-
linkedNode.key = emptyLinkedKeyNode;
|
|
1132
|
-
endNode(linkedNode, nextContext.offset, nextContext.startLoc);
|
|
1133
|
-
return {
|
|
1134
|
-
nextConsumeToken: token,
|
|
1135
|
-
node: linkedNode
|
|
1136
|
-
};
|
|
1137
|
-
}
|
|
1138
|
-
endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1139
|
-
return {
|
|
1140
|
-
node: linkedNode
|
|
1141
|
-
};
|
|
1142
|
-
}
|
|
1143
|
-
function parseMessage(tokenizer) {
|
|
1144
|
-
const context = tokenizer.context();
|
|
1145
|
-
const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1146
|
-
? tokenizer.currentOffset()
|
|
1147
|
-
: context.offset;
|
|
1148
|
-
const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1149
|
-
? context.endLoc
|
|
1150
|
-
: context.startLoc;
|
|
1151
|
-
const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
|
|
1152
|
-
node.items = [];
|
|
1153
|
-
let nextToken = null;
|
|
1154
|
-
do {
|
|
1155
|
-
const token = nextToken || tokenizer.nextToken();
|
|
1156
|
-
nextToken = null;
|
|
1157
|
-
switch (token.type) {
|
|
1158
|
-
case 0 /* TokenTypes.Text */:
|
|
1159
|
-
if (token.value == null) {
|
|
1160
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1161
|
-
}
|
|
1162
|
-
node.items.push(parseText(tokenizer, token.value || ''));
|
|
1163
|
-
break;
|
|
1164
|
-
case 6 /* TokenTypes.List */:
|
|
1165
|
-
if (token.value == null) {
|
|
1166
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1167
|
-
}
|
|
1168
|
-
node.items.push(parseList(tokenizer, token.value || ''));
|
|
1169
|
-
break;
|
|
1170
|
-
case 5 /* TokenTypes.Named */:
|
|
1171
|
-
if (token.value == null) {
|
|
1172
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1173
|
-
}
|
|
1174
|
-
node.items.push(parseNamed(tokenizer, token.value || ''));
|
|
1175
|
-
break;
|
|
1176
|
-
case 7 /* TokenTypes.Literal */:
|
|
1177
|
-
if (token.value == null) {
|
|
1178
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1179
|
-
}
|
|
1180
|
-
node.items.push(parseLiteral(tokenizer, token.value || ''));
|
|
1181
|
-
break;
|
|
1182
|
-
case 8 /* TokenTypes.LinkedAlias */:
|
|
1183
|
-
const parsed = parseLinked(tokenizer);
|
|
1184
|
-
node.items.push(parsed.node);
|
|
1185
|
-
nextToken = parsed.nextConsumeToken || null;
|
|
1186
|
-
break;
|
|
1187
|
-
}
|
|
1188
|
-
} while (context.currentType !== 14 /* TokenTypes.EOF */ &&
|
|
1189
|
-
context.currentType !== 1 /* TokenTypes.Pipe */);
|
|
1190
|
-
// adjust message node loc
|
|
1191
|
-
const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1192
|
-
? context.lastOffset
|
|
1193
|
-
: tokenizer.currentOffset();
|
|
1194
|
-
const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1195
|
-
? context.lastEndLoc
|
|
1196
|
-
: tokenizer.currentPosition();
|
|
1197
|
-
endNode(node, endOffset, endLoc);
|
|
1198
|
-
return node;
|
|
1199
|
-
}
|
|
1200
|
-
function parsePlural(tokenizer, offset, loc, msgNode) {
|
|
1201
|
-
const context = tokenizer.context();
|
|
1202
|
-
let hasEmptyMessage = msgNode.items.length === 0;
|
|
1203
|
-
const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
|
|
1204
|
-
node.cases = [];
|
|
1205
|
-
node.cases.push(msgNode);
|
|
1206
|
-
do {
|
|
1207
|
-
const msg = parseMessage(tokenizer);
|
|
1208
|
-
if (!hasEmptyMessage) {
|
|
1209
|
-
hasEmptyMessage = msg.items.length === 0;
|
|
1210
|
-
}
|
|
1211
|
-
node.cases.push(msg);
|
|
1212
|
-
} while (context.currentType !== 14 /* TokenTypes.EOF */);
|
|
1213
|
-
if (hasEmptyMessage) {
|
|
1214
|
-
emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
|
|
1215
|
-
}
|
|
1216
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1217
|
-
return node;
|
|
1218
|
-
}
|
|
1219
|
-
function parseResource(tokenizer) {
|
|
1220
|
-
const context = tokenizer.context();
|
|
1221
|
-
const { offset, startLoc } = context;
|
|
1222
|
-
const msgNode = parseMessage(tokenizer);
|
|
1223
|
-
if (context.currentType === 14 /* TokenTypes.EOF */) {
|
|
1224
|
-
return msgNode;
|
|
1225
|
-
}
|
|
1226
|
-
else {
|
|
1227
|
-
return parsePlural(tokenizer, offset, startLoc, msgNode);
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
function parse(source) {
|
|
1231
|
-
const tokenizer = createTokenizer(source, assign({}, options));
|
|
1232
|
-
const context = tokenizer.context();
|
|
1233
|
-
const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
|
|
1234
|
-
if (location && node.loc) {
|
|
1235
|
-
node.loc.source = source;
|
|
1236
|
-
}
|
|
1237
|
-
node.body = parseResource(tokenizer);
|
|
1238
|
-
if (options.onCacheKey) {
|
|
1239
|
-
node.cacheKey = options.onCacheKey(source);
|
|
1240
|
-
}
|
|
1241
|
-
// assert whether achieved to EOF
|
|
1242
|
-
if (context.currentType !== 14 /* TokenTypes.EOF */) {
|
|
1243
|
-
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
|
|
1244
|
-
}
|
|
1245
|
-
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1246
|
-
return node;
|
|
1247
|
-
}
|
|
1248
|
-
return { parse };
|
|
1249
|
-
}
|
|
1250
|
-
function getTokenCaption(token) {
|
|
1251
|
-
if (token.type === 14 /* TokenTypes.EOF */) {
|
|
1252
|
-
return 'EOF';
|
|
1253
|
-
}
|
|
1254
|
-
const name = (token.value || '').replace(/\r?\n/gu, '\\n');
|
|
1255
|
-
return name.length > 10 ? name.slice(0, 9) + '…' : name;
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
function createTransformer(ast, options = {} // eslint-disable-line
|
|
1259
|
-
) {
|
|
1260
|
-
const _context = {
|
|
1261
|
-
ast,
|
|
1262
|
-
helpers: new Set()
|
|
1263
|
-
};
|
|
1264
|
-
const context = () => _context;
|
|
1265
|
-
const helper = (name) => {
|
|
1266
|
-
_context.helpers.add(name);
|
|
1267
|
-
return name;
|
|
1268
|
-
};
|
|
1269
|
-
return { context, helper };
|
|
1270
|
-
}
|
|
1271
|
-
function traverseNodes(nodes, transformer) {
|
|
1272
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
1273
|
-
traverseNode(nodes[i], transformer);
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
function traverseNode(node, transformer) {
|
|
1277
|
-
// TODO: if we need pre-hook of transform, should be implemented to here
|
|
1278
|
-
switch (node.type) {
|
|
1279
|
-
case 1 /* NodeTypes.Plural */:
|
|
1280
|
-
traverseNodes(node.cases, transformer);
|
|
1281
|
-
transformer.helper("plural" /* HelperNameMap.PLURAL */);
|
|
1282
|
-
break;
|
|
1283
|
-
case 2 /* NodeTypes.Message */:
|
|
1284
|
-
traverseNodes(node.items, transformer);
|
|
1285
|
-
break;
|
|
1286
|
-
case 6 /* NodeTypes.Linked */:
|
|
1287
|
-
const linked = node;
|
|
1288
|
-
traverseNode(linked.key, transformer);
|
|
1289
|
-
transformer.helper("linked" /* HelperNameMap.LINKED */);
|
|
1290
|
-
transformer.helper("type" /* HelperNameMap.TYPE */);
|
|
1291
|
-
break;
|
|
1292
|
-
case 5 /* NodeTypes.List */:
|
|
1293
|
-
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1294
|
-
transformer.helper("list" /* HelperNameMap.LIST */);
|
|
1295
|
-
break;
|
|
1296
|
-
case 4 /* NodeTypes.Named */:
|
|
1297
|
-
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1298
|
-
transformer.helper("named" /* HelperNameMap.NAMED */);
|
|
1299
|
-
break;
|
|
1300
|
-
}
|
|
1301
|
-
// TODO: if we need post-hook of transform, should be implemented to here
|
|
1302
|
-
}
|
|
1303
|
-
// transform AST
|
|
1304
|
-
function transform(ast, options = {} // eslint-disable-line
|
|
1305
|
-
) {
|
|
1306
|
-
const transformer = createTransformer(ast);
|
|
1307
|
-
transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
|
|
1308
|
-
// traverse
|
|
1309
|
-
ast.body && traverseNode(ast.body, transformer);
|
|
1310
|
-
// set meta information
|
|
1311
|
-
const context = transformer.context();
|
|
1312
|
-
ast.helpers = Array.from(context.helpers);
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
function optimize(ast) {
|
|
1316
|
-
const body = ast.body;
|
|
1317
|
-
if (body.type === 2 /* NodeTypes.Message */) {
|
|
1318
|
-
optimizeMessageNode(body);
|
|
1319
|
-
}
|
|
1320
|
-
else {
|
|
1321
|
-
body.cases.forEach(c => optimizeMessageNode(c));
|
|
1322
|
-
}
|
|
1323
|
-
return ast;
|
|
1324
|
-
}
|
|
1325
|
-
function optimizeMessageNode(message) {
|
|
1326
|
-
if (message.items.length === 1) {
|
|
1327
|
-
const item = message.items[0];
|
|
1328
|
-
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1329
|
-
message.static = item.value;
|
|
1330
|
-
delete item.value; // optimization for size
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
else {
|
|
1334
|
-
const values = [];
|
|
1335
|
-
for (let i = 0; i < message.items.length; i++) {
|
|
1336
|
-
const item = message.items[i];
|
|
1337
|
-
if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
|
|
1338
|
-
break;
|
|
1339
|
-
}
|
|
1340
|
-
if (item.value == null) {
|
|
1341
|
-
break;
|
|
1342
|
-
}
|
|
1343
|
-
values.push(item.value);
|
|
1344
|
-
}
|
|
1345
|
-
if (values.length === message.items.length) {
|
|
1346
|
-
message.static = join(values);
|
|
1347
|
-
for (let i = 0; i < message.items.length; i++) {
|
|
1348
|
-
const item = message.items[i];
|
|
1349
|
-
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1350
|
-
delete item.value; // optimization for size
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1358
|
-
function minify(node) {
|
|
1359
|
-
node.t = node.type;
|
|
1360
|
-
switch (node.type) {
|
|
1361
|
-
case 0 /* NodeTypes.Resource */:
|
|
1362
|
-
const resource = node;
|
|
1363
|
-
minify(resource.body);
|
|
1364
|
-
resource.b = resource.body;
|
|
1365
|
-
delete resource.body;
|
|
1366
|
-
break;
|
|
1367
|
-
case 1 /* NodeTypes.Plural */:
|
|
1368
|
-
const plural = node;
|
|
1369
|
-
const cases = plural.cases;
|
|
1370
|
-
for (let i = 0; i < cases.length; i++) {
|
|
1371
|
-
minify(cases[i]);
|
|
1372
|
-
}
|
|
1373
|
-
plural.c = cases;
|
|
1374
|
-
delete plural.cases;
|
|
1375
|
-
break;
|
|
1376
|
-
case 2 /* NodeTypes.Message */:
|
|
1377
|
-
const message = node;
|
|
1378
|
-
const items = message.items;
|
|
1379
|
-
for (let i = 0; i < items.length; i++) {
|
|
1380
|
-
minify(items[i]);
|
|
1381
|
-
}
|
|
1382
|
-
message.i = items;
|
|
1383
|
-
delete message.items;
|
|
1384
|
-
if (message.static) {
|
|
1385
|
-
message.s = message.static;
|
|
1386
|
-
delete message.static;
|
|
1387
|
-
}
|
|
1388
|
-
break;
|
|
1389
|
-
case 3 /* NodeTypes.Text */:
|
|
1390
|
-
case 9 /* NodeTypes.Literal */:
|
|
1391
|
-
case 8 /* NodeTypes.LinkedModifier */:
|
|
1392
|
-
case 7 /* NodeTypes.LinkedKey */:
|
|
1393
|
-
const valueNode = node;
|
|
1394
|
-
if (valueNode.value) {
|
|
1395
|
-
valueNode.v = valueNode.value;
|
|
1396
|
-
delete valueNode.value;
|
|
1397
|
-
}
|
|
1398
|
-
break;
|
|
1399
|
-
case 6 /* NodeTypes.Linked */:
|
|
1400
|
-
const linked = node;
|
|
1401
|
-
minify(linked.key);
|
|
1402
|
-
linked.k = linked.key;
|
|
1403
|
-
delete linked.key;
|
|
1404
|
-
if (linked.modifier) {
|
|
1405
|
-
minify(linked.modifier);
|
|
1406
|
-
linked.m = linked.modifier;
|
|
1407
|
-
delete linked.modifier;
|
|
1408
|
-
}
|
|
1409
|
-
break;
|
|
1410
|
-
case 5 /* NodeTypes.List */:
|
|
1411
|
-
const list = node;
|
|
1412
|
-
list.i = list.index;
|
|
1413
|
-
delete list.index;
|
|
1414
|
-
break;
|
|
1415
|
-
case 4 /* NodeTypes.Named */:
|
|
1416
|
-
const named = node;
|
|
1417
|
-
named.k = named.key;
|
|
1418
|
-
delete named.key;
|
|
1419
|
-
break;
|
|
1420
|
-
default:
|
|
1421
|
-
{
|
|
1422
|
-
throw new Error(`unhandled minify node type: ${node.type}`);
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
delete node.type;
|
|
1426
|
-
}
|
|
1427
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1428
|
-
|
|
1429
|
-
function createCodeGenerator(ast, options) {
|
|
1430
|
-
const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
|
|
1431
|
-
const location = options.location !== false;
|
|
1432
|
-
const _context = {
|
|
1433
|
-
filename,
|
|
1434
|
-
code: '',
|
|
1435
|
-
column: 1,
|
|
1436
|
-
line: 1,
|
|
1437
|
-
offset: 0,
|
|
1438
|
-
map: undefined,
|
|
1439
|
-
breakLineCode,
|
|
1440
|
-
needIndent: _needIndent,
|
|
1441
|
-
indentLevel: 0
|
|
1442
|
-
};
|
|
1443
|
-
if (location && ast.loc) {
|
|
1444
|
-
_context.source = ast.loc.source;
|
|
1445
|
-
}
|
|
1446
|
-
const context = () => _context;
|
|
1447
|
-
function push(code, node) {
|
|
1448
|
-
_context.code += code;
|
|
1449
|
-
}
|
|
1450
|
-
function _newline(n, withBreakLine = true) {
|
|
1451
|
-
const _breakLineCode = withBreakLine ? breakLineCode : '';
|
|
1452
|
-
push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
|
|
1453
|
-
}
|
|
1454
|
-
function indent(withNewLine = true) {
|
|
1455
|
-
const level = ++_context.indentLevel;
|
|
1456
|
-
withNewLine && _newline(level);
|
|
1457
|
-
}
|
|
1458
|
-
function deindent(withNewLine = true) {
|
|
1459
|
-
const level = --_context.indentLevel;
|
|
1460
|
-
withNewLine && _newline(level);
|
|
1461
|
-
}
|
|
1462
|
-
function newline() {
|
|
1463
|
-
_newline(_context.indentLevel);
|
|
1464
|
-
}
|
|
1465
|
-
const helper = (key) => `_${key}`;
|
|
1466
|
-
const needIndent = () => _context.needIndent;
|
|
1467
|
-
return {
|
|
1468
|
-
context,
|
|
1469
|
-
push,
|
|
1470
|
-
indent,
|
|
1471
|
-
deindent,
|
|
1472
|
-
newline,
|
|
1473
|
-
helper,
|
|
1474
|
-
needIndent
|
|
1475
|
-
};
|
|
1476
|
-
}
|
|
1477
|
-
function generateLinkedNode(generator, node) {
|
|
1478
|
-
const { helper } = generator;
|
|
1479
|
-
generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
|
|
1480
|
-
generateNode(generator, node.key);
|
|
1481
|
-
if (node.modifier) {
|
|
1482
|
-
generator.push(`, `);
|
|
1483
|
-
generateNode(generator, node.modifier);
|
|
1484
|
-
generator.push(`, _type`);
|
|
1485
|
-
}
|
|
1486
|
-
else {
|
|
1487
|
-
generator.push(`, undefined, _type`);
|
|
1488
|
-
}
|
|
1489
|
-
generator.push(`)`);
|
|
1490
|
-
}
|
|
1491
|
-
function generateMessageNode(generator, node) {
|
|
1492
|
-
const { helper, needIndent } = generator;
|
|
1493
|
-
generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
|
|
1494
|
-
generator.indent(needIndent());
|
|
1495
|
-
const length = node.items.length;
|
|
1496
|
-
for (let i = 0; i < length; i++) {
|
|
1497
|
-
generateNode(generator, node.items[i]);
|
|
1498
|
-
if (i === length - 1) {
|
|
1499
|
-
break;
|
|
1500
|
-
}
|
|
1501
|
-
generator.push(', ');
|
|
1502
|
-
}
|
|
1503
|
-
generator.deindent(needIndent());
|
|
1504
|
-
generator.push('])');
|
|
1505
|
-
}
|
|
1506
|
-
function generatePluralNode(generator, node) {
|
|
1507
|
-
const { helper, needIndent } = generator;
|
|
1508
|
-
if (node.cases.length > 1) {
|
|
1509
|
-
generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
|
|
1510
|
-
generator.indent(needIndent());
|
|
1511
|
-
const length = node.cases.length;
|
|
1512
|
-
for (let i = 0; i < length; i++) {
|
|
1513
|
-
generateNode(generator, node.cases[i]);
|
|
1514
|
-
if (i === length - 1) {
|
|
1515
|
-
break;
|
|
1516
|
-
}
|
|
1517
|
-
generator.push(', ');
|
|
1518
|
-
}
|
|
1519
|
-
generator.deindent(needIndent());
|
|
1520
|
-
generator.push(`])`);
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
function generateResource(generator, node) {
|
|
1524
|
-
if (node.body) {
|
|
1525
|
-
generateNode(generator, node.body);
|
|
1526
|
-
}
|
|
1527
|
-
else {
|
|
1528
|
-
generator.push('null');
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
function generateNode(generator, node) {
|
|
1532
|
-
const { helper } = generator;
|
|
1533
|
-
switch (node.type) {
|
|
1534
|
-
case 0 /* NodeTypes.Resource */:
|
|
1535
|
-
generateResource(generator, node);
|
|
1536
|
-
break;
|
|
1537
|
-
case 1 /* NodeTypes.Plural */:
|
|
1538
|
-
generatePluralNode(generator, node);
|
|
1539
|
-
break;
|
|
1540
|
-
case 2 /* NodeTypes.Message */:
|
|
1541
|
-
generateMessageNode(generator, node);
|
|
1542
|
-
break;
|
|
1543
|
-
case 6 /* NodeTypes.Linked */:
|
|
1544
|
-
generateLinkedNode(generator, node);
|
|
1545
|
-
break;
|
|
1546
|
-
case 8 /* NodeTypes.LinkedModifier */:
|
|
1547
|
-
generator.push(JSON.stringify(node.value), node);
|
|
1548
|
-
break;
|
|
1549
|
-
case 7 /* NodeTypes.LinkedKey */:
|
|
1550
|
-
generator.push(JSON.stringify(node.value), node);
|
|
1551
|
-
break;
|
|
1552
|
-
case 5 /* NodeTypes.List */:
|
|
1553
|
-
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
|
|
1554
|
-
break;
|
|
1555
|
-
case 4 /* NodeTypes.Named */:
|
|
1556
|
-
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
|
|
1557
|
-
break;
|
|
1558
|
-
case 9 /* NodeTypes.Literal */:
|
|
1559
|
-
generator.push(JSON.stringify(node.value), node);
|
|
1560
|
-
break;
|
|
1561
|
-
case 3 /* NodeTypes.Text */:
|
|
1562
|
-
generator.push(JSON.stringify(node.value), node);
|
|
1563
|
-
break;
|
|
1564
|
-
default:
|
|
1565
|
-
{
|
|
1566
|
-
throw new Error(`unhandled codegen node type: ${node.type}`);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
// generate code from AST
|
|
1571
|
-
const generate = (ast, options = {} // eslint-disable-line
|
|
1572
|
-
) => {
|
|
1573
|
-
const mode = isString(options.mode) ? options.mode : 'normal';
|
|
1574
|
-
const filename = isString(options.filename)
|
|
1575
|
-
? options.filename
|
|
1576
|
-
: 'message.intl';
|
|
1577
|
-
const sourceMap = !!options.sourceMap;
|
|
1578
|
-
// prettier-ignore
|
|
1579
|
-
const breakLineCode = options.breakLineCode != null
|
|
1580
|
-
? options.breakLineCode
|
|
1581
|
-
: mode === 'arrow'
|
|
1582
|
-
? ';'
|
|
1583
|
-
: '\n';
|
|
1584
|
-
const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
|
|
1585
|
-
const helpers = ast.helpers || [];
|
|
1586
|
-
const generator = createCodeGenerator(ast, {
|
|
1587
|
-
mode,
|
|
1588
|
-
filename,
|
|
1589
|
-
sourceMap,
|
|
1590
|
-
breakLineCode,
|
|
1591
|
-
needIndent
|
|
1592
|
-
});
|
|
1593
|
-
generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
|
|
1594
|
-
generator.indent(needIndent);
|
|
1595
|
-
if (helpers.length > 0) {
|
|
1596
|
-
generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
|
|
1597
|
-
generator.newline();
|
|
1598
|
-
}
|
|
1599
|
-
generator.push(`return `);
|
|
1600
|
-
generateNode(generator, ast);
|
|
1601
|
-
generator.deindent(needIndent);
|
|
1602
|
-
generator.push(`}`);
|
|
1603
|
-
delete ast.helpers;
|
|
1604
|
-
const { code, map } = generator.context();
|
|
1605
|
-
return {
|
|
1606
|
-
ast,
|
|
1607
|
-
code,
|
|
1608
|
-
map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1609
|
-
};
|
|
1610
|
-
};
|
|
1611
|
-
|
|
1612
|
-
function baseCompile$1(source, options = {}) {
|
|
1613
|
-
const assignedOptions = assign({}, options);
|
|
1614
|
-
const jit = !!assignedOptions.jit;
|
|
1615
|
-
const enalbeMinify = !!assignedOptions.minify;
|
|
1616
|
-
const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
|
|
1617
|
-
// parse source codes
|
|
1618
|
-
const parser = createParser(assignedOptions);
|
|
1619
|
-
const ast = parser.parse(source);
|
|
1620
|
-
if (!jit) {
|
|
1621
|
-
// transform ASTs
|
|
1622
|
-
transform(ast, assignedOptions);
|
|
1623
|
-
// generate javascript codes
|
|
1624
|
-
return generate(ast, assignedOptions);
|
|
1625
|
-
}
|
|
1626
|
-
else {
|
|
1627
|
-
// optimize ASTs
|
|
1628
|
-
enambeOptimize && optimize(ast);
|
|
1629
|
-
// minimize ASTs
|
|
1630
|
-
enalbeMinify && minify(ast);
|
|
1631
|
-
// In JIT mode, no ast transform, no code generation.
|
|
1632
|
-
return { ast, code: '' };
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
206
|
|
|
1636
207
|
const pathStateMachine = [];
|
|
1637
208
|
pathStateMachine[0 /* States.BEFORE_PATH */] = {
|
|
@@ -2022,7 +593,8 @@ var IntlifyCore = (function (exports) {
|
|
|
2022
593
|
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
2023
594
|
["type" /* HelperNameMap.TYPE */]: type,
|
|
2024
595
|
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
2025
|
-
["normalize" /* HelperNameMap.NORMALIZE */]: normalize
|
|
596
|
+
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
597
|
+
["values" /* HelperNameMap.VALUES */]: assign({}, _list, _named)
|
|
2026
598
|
};
|
|
2027
599
|
return ctx;
|
|
2028
600
|
}
|
|
@@ -2061,7 +633,8 @@ var IntlifyCore = (function (exports) {
|
|
|
2061
633
|
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
2062
634
|
CANNOT_FORMAT_DATE: 5,
|
|
2063
635
|
FALLBACK_TO_DATE_FORMAT: 6,
|
|
2064
|
-
|
|
636
|
+
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
637
|
+
__EXTEND_POINT__: 8
|
|
2065
638
|
};
|
|
2066
639
|
/** @internal */
|
|
2067
640
|
const warnMessages = {
|
|
@@ -2070,7 +643,8 @@ var IntlifyCore = (function (exports) {
|
|
|
2070
643
|
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
2071
644
|
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
2072
645
|
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
2073
|
-
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale
|
|
646
|
+
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
647
|
+
[CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`
|
|
2074
648
|
};
|
|
2075
649
|
function getWarnMessage(code, ...args) {
|
|
2076
650
|
return format$1(warnMessages[code], ...args);
|
|
@@ -2197,7 +771,7 @@ var IntlifyCore = (function (exports) {
|
|
|
2197
771
|
* Intlify core-base version
|
|
2198
772
|
* @internal
|
|
2199
773
|
*/
|
|
2200
|
-
const VERSION = '9.3.0-beta.
|
|
774
|
+
const VERSION = '9.3.0-beta.27';
|
|
2201
775
|
const NOT_REOSLVED = -1;
|
|
2202
776
|
const DEFAULT_LOCALE = 'en-US';
|
|
2203
777
|
const MISSING_RESOLVE_VALUE = '';
|
|
@@ -2258,10 +832,10 @@ var IntlifyCore = (function (exports) {
|
|
|
2258
832
|
}
|
|
2259
833
|
// Additional Meta for Intlify DevTools
|
|
2260
834
|
let _additionalMeta = null;
|
|
2261
|
-
const setAdditionalMeta =
|
|
835
|
+
const setAdditionalMeta = /* #__PURE__*/ (meta) => {
|
|
2262
836
|
_additionalMeta = meta;
|
|
2263
837
|
};
|
|
2264
|
-
const getAdditionalMeta =
|
|
838
|
+
const getAdditionalMeta = /* #__PURE__*/ () => _additionalMeta;
|
|
2265
839
|
let _fallbackContext = null;
|
|
2266
840
|
const setFallbackContext = (context) => {
|
|
2267
841
|
_fallbackContext = context;
|
|
@@ -2271,6 +845,7 @@ var IntlifyCore = (function (exports) {
|
|
|
2271
845
|
let _cid = 0;
|
|
2272
846
|
function createCoreContext(options = {}) {
|
|
2273
847
|
// setup options
|
|
848
|
+
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
2274
849
|
const version = isString(options.version) ? options.version : VERSION;
|
|
2275
850
|
const locale = isString(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
2276
851
|
const fallbackLocale = isArray(options.fallbackLocale) ||
|
|
@@ -2321,7 +896,6 @@ var IntlifyCore = (function (exports) {
|
|
|
2321
896
|
const fallbackContext = isObject(options.fallbackContext)
|
|
2322
897
|
? options.fallbackContext
|
|
2323
898
|
: undefined;
|
|
2324
|
-
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
2325
899
|
// setup internal options
|
|
2326
900
|
const internalOptions = options;
|
|
2327
901
|
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters)
|
|
@@ -2479,14 +1053,14 @@ var IntlifyCore = (function (exports) {
|
|
|
2479
1053
|
}
|
|
2480
1054
|
}
|
|
2481
1055
|
|
|
2482
|
-
|
|
2483
|
-
const inc = ()
|
|
1056
|
+
const code = CompileErrorCodes.__EXTEND_POINT__;
|
|
1057
|
+
const inc = incrementer(code);
|
|
2484
1058
|
const CoreErrorCodes = {
|
|
2485
1059
|
INVALID_ARGUMENT: code,
|
|
2486
1060
|
INVALID_DATE_ARGUMENT: inc(),
|
|
2487
1061
|
INVALID_ISO_DATE_ARGUMENT: inc(),
|
|
2488
|
-
|
|
2489
|
-
__EXTEND_POINT__: inc() //
|
|
1062
|
+
NOT_SUPPORT_NON_STRING_MESSAGE: inc(),
|
|
1063
|
+
__EXTEND_POINT__: inc() // 22
|
|
2490
1064
|
};
|
|
2491
1065
|
function createCoreError(code) {
|
|
2492
1066
|
return createCompileError(code, null, { messages: errorMessages } );
|
|
@@ -2497,70 +1071,31 @@ var IntlifyCore = (function (exports) {
|
|
|
2497
1071
|
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: 'The date provided is an invalid Date object.' +
|
|
2498
1072
|
'Make sure your Date represents a valid date.',
|
|
2499
1073
|
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: 'The argument provided is not a valid ISO date string',
|
|
2500
|
-
[CoreErrorCodes.
|
|
1074
|
+
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message'
|
|
2501
1075
|
};
|
|
2502
1076
|
|
|
2503
|
-
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
2504
|
-
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
2505
|
-
if (warnHtmlMessage && detectHtmlTag(source)) {
|
|
2506
|
-
warn(format$1(WARN_MESSAGE, { source }));
|
|
2507
|
-
}
|
|
2508
|
-
}
|
|
2509
|
-
const defaultOnCacheKey = (message) => message;
|
|
2510
1077
|
let compileCache = Object.create(null);
|
|
2511
1078
|
function clearCompileCache() {
|
|
2512
1079
|
compileCache = Object.create(null);
|
|
2513
1080
|
}
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
options.onError = (err) => {
|
|
2519
|
-
detectError = true;
|
|
2520
|
-
onError(err);
|
|
2521
|
-
};
|
|
2522
|
-
// compile with mesasge-compiler
|
|
2523
|
-
return { ...baseCompile$1(message, options), detectError };
|
|
2524
|
-
}
|
|
2525
|
-
function compileToFunction(message, options = {}) {
|
|
1081
|
+
const isMessageAST = (val) => isObject(val) &&
|
|
1082
|
+
(val.t === 0 || val.type === 0) &&
|
|
1083
|
+
('b' in val || 'body' in val);
|
|
1084
|
+
function compileToFunction(message, context) {
|
|
2526
1085
|
if (!isString(message)) {
|
|
2527
|
-
throw createCoreError(CoreErrorCodes.
|
|
1086
|
+
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
|
|
2528
1087
|
}
|
|
2529
1088
|
{
|
|
2530
1089
|
warn(`Runtime compilation is not supported in ${'core.runtime.global.js' }.`);
|
|
2531
1090
|
return (() => message);
|
|
2532
1091
|
}
|
|
2533
1092
|
}
|
|
2534
|
-
function compile(message,
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2540
|
-
options.warnHtmlMessage
|
|
2541
|
-
: true;
|
|
2542
|
-
checkHtmlMessage(message, warnHtmlMessage);
|
|
2543
|
-
// check caches
|
|
2544
|
-
const onCacheKey = options.onCacheKey || defaultOnCacheKey;
|
|
2545
|
-
const cacheKey = onCacheKey(message);
|
|
2546
|
-
const cached = compileCache[cacheKey];
|
|
2547
|
-
if (cached) {
|
|
2548
|
-
return cached;
|
|
1093
|
+
function compile(message, context) {
|
|
1094
|
+
{
|
|
1095
|
+
if (!isMessageAST(message)) {
|
|
1096
|
+
warn(`the message that is resolve with key '${context.key}') is not supported for jit compilation`);
|
|
1097
|
+
return (() => message);
|
|
2549
1098
|
}
|
|
2550
|
-
// compile with JIT mode
|
|
2551
|
-
const { ast, detectError } = baseCompile(message, {
|
|
2552
|
-
...options,
|
|
2553
|
-
location: true,
|
|
2554
|
-
jit: true
|
|
2555
|
-
});
|
|
2556
|
-
// compose message function from AST
|
|
2557
|
-
const msg = format(ast);
|
|
2558
|
-
// if occurred compile error, don't cache
|
|
2559
|
-
return !detectError
|
|
2560
|
-
? (compileCache[cacheKey] = msg)
|
|
2561
|
-
: msg;
|
|
2562
|
-
}
|
|
2563
|
-
else {
|
|
2564
1099
|
// AST case (passed from bundler)
|
|
2565
1100
|
const cacheKey = message.cacheKey;
|
|
2566
1101
|
if (cacheKey) {
|
|
@@ -2580,7 +1115,6 @@ var IntlifyCore = (function (exports) {
|
|
|
2580
1115
|
|
|
2581
1116
|
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
2582
1117
|
const isMessageFunction = (val) => isFunction(val);
|
|
2583
|
-
const isMessageAST = (val) => isObject(val) && val.type === 0 && 'body' in val;
|
|
2584
1118
|
// implementation of `translate` function
|
|
2585
1119
|
function translate(context, ...args) {
|
|
2586
1120
|
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
@@ -2651,12 +1185,12 @@ var IntlifyCore = (function (exports) {
|
|
|
2651
1185
|
}
|
|
2652
1186
|
// setup compile error detecting
|
|
2653
1187
|
let occurred = false;
|
|
2654
|
-
const
|
|
1188
|
+
const onError = () => {
|
|
2655
1189
|
occurred = true;
|
|
2656
1190
|
};
|
|
2657
1191
|
// compile message format
|
|
2658
1192
|
const msg = !isMessageFunction(format)
|
|
2659
|
-
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
1193
|
+
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
|
|
2660
1194
|
: format;
|
|
2661
1195
|
// if occurred compile error, return the message format
|
|
2662
1196
|
if (occurred) {
|
|
@@ -2784,7 +1318,7 @@ var IntlifyCore = (function (exports) {
|
|
|
2784
1318
|
}
|
|
2785
1319
|
return [format, targetLocale, message];
|
|
2786
1320
|
}
|
|
2787
|
-
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
1321
|
+
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
2788
1322
|
const { messageCompiler, warnHtmlMessage } = context;
|
|
2789
1323
|
if (isMessageFunction(format)) {
|
|
2790
1324
|
const msg = format;
|
|
@@ -2808,7 +1342,7 @@ var IntlifyCore = (function (exports) {
|
|
|
2808
1342
|
endTag = 'intlify-message-compilation-end';
|
|
2809
1343
|
mark && mark(startTag);
|
|
2810
1344
|
}
|
|
2811
|
-
const msg = messageCompiler(format,
|
|
1345
|
+
const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
|
|
2812
1346
|
// for vue-devtools timeline event
|
|
2813
1347
|
if (inBrowser) {
|
|
2814
1348
|
const end = window.performance.now();
|
|
@@ -2901,11 +1435,13 @@ var IntlifyCore = (function (exports) {
|
|
|
2901
1435
|
}
|
|
2902
1436
|
return [key, options];
|
|
2903
1437
|
}
|
|
2904
|
-
function
|
|
1438
|
+
function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
|
|
2905
1439
|
return {
|
|
1440
|
+
locale,
|
|
1441
|
+
key,
|
|
2906
1442
|
warnHtmlMessage,
|
|
2907
1443
|
onError: (err) => {
|
|
2908
|
-
|
|
1444
|
+
onError && onError(err);
|
|
2909
1445
|
{
|
|
2910
1446
|
const _source = getSourceForCodeFrame(source);
|
|
2911
1447
|
const message = `Message compilation error: ${err.message}`;
|
|
@@ -2947,10 +1483,10 @@ var IntlifyCore = (function (exports) {
|
|
|
2947
1483
|
}
|
|
2948
1484
|
if (isString(val) || isMessageAST(val)) {
|
|
2949
1485
|
let occurred = false;
|
|
2950
|
-
const
|
|
1486
|
+
const onError = () => {
|
|
2951
1487
|
occurred = true;
|
|
2952
1488
|
};
|
|
2953
|
-
const msg = compileMessageFormat(context, key, locale, val, key,
|
|
1489
|
+
const msg = compileMessageFormat(context, key, locale, val, key, onError);
|
|
2954
1490
|
return !occurred
|
|
2955
1491
|
? msg
|
|
2956
1492
|
: NOOP_MESSAGE_FUNCTION;
|