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