@intlify/core 10.0.0-alpha.5 → 10.0.0-beta.2

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 v10.0.0-alpha.5
2
+ * core v10.0.0-beta.2
3
3
  * (c) 2024 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -146,17 +146,6 @@ function warnOnce(msg) {
146
146
  }
147
147
  }
148
148
 
149
- function createPosition(line, column, offset) {
150
- return { line, column, offset };
151
- }
152
- function createLocation(start, end, source) {
153
- const loc = { start, end };
154
- if (source != null) {
155
- loc.source = source;
156
- }
157
- return loc;
158
- }
159
-
160
149
  const CompileErrorCodes = {
161
150
  // tokenizer error codes
162
151
  EXPECTED_TOKEN: 1,
@@ -218,1441 +207,6 @@ function createCompileError(code, loc, options = {}) {
218
207
  error.domain = domain;
219
208
  return error;
220
209
  }
221
- /** @internal */
222
- function defaultOnError(error) {
223
- throw error;
224
- }
225
-
226
- // eslint-disable-next-line no-useless-escape
227
- const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
228
- const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
229
-
230
- const CHAR_SP = ' ';
231
- const CHAR_CR = '\r';
232
- const CHAR_LF = '\n';
233
- const CHAR_LS = String.fromCharCode(0x2028);
234
- const CHAR_PS = String.fromCharCode(0x2029);
235
- function createScanner(str) {
236
- const _buf = str;
237
- let _index = 0;
238
- let _line = 1;
239
- let _column = 1;
240
- let _peekOffset = 0;
241
- const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
242
- const isLF = (index) => _buf[index] === CHAR_LF;
243
- const isPS = (index) => _buf[index] === CHAR_PS;
244
- const isLS = (index) => _buf[index] === CHAR_LS;
245
- const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
246
- const index = () => _index;
247
- const line = () => _line;
248
- const column = () => _column;
249
- const peekOffset = () => _peekOffset;
250
- const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
251
- const currentChar = () => charAt(_index);
252
- const currentPeek = () => charAt(_index + _peekOffset);
253
- function next() {
254
- _peekOffset = 0;
255
- if (isLineEnd(_index)) {
256
- _line++;
257
- _column = 0;
258
- }
259
- if (isCRLF(_index)) {
260
- _index++;
261
- }
262
- _index++;
263
- _column++;
264
- return _buf[_index];
265
- }
266
- function peek() {
267
- if (isCRLF(_index + _peekOffset)) {
268
- _peekOffset++;
269
- }
270
- _peekOffset++;
271
- return _buf[_index + _peekOffset];
272
- }
273
- function reset() {
274
- _index = 0;
275
- _line = 1;
276
- _column = 1;
277
- _peekOffset = 0;
278
- }
279
- function resetPeek(offset = 0) {
280
- _peekOffset = offset;
281
- }
282
- function skipToPeek() {
283
- const target = _index + _peekOffset;
284
- while (target !== _index) {
285
- next();
286
- }
287
- _peekOffset = 0;
288
- }
289
- return {
290
- index,
291
- line,
292
- column,
293
- peekOffset,
294
- charAt,
295
- currentChar,
296
- currentPeek,
297
- next,
298
- peek,
299
- reset,
300
- resetPeek,
301
- skipToPeek
302
- };
303
- }
304
-
305
- const EOF = undefined;
306
- const DOT = '.';
307
- const LITERAL_DELIMITER = "'";
308
- const ERROR_DOMAIN$3 = 'tokenizer';
309
- function createTokenizer(source, options = {}) {
310
- const location = options.location !== false;
311
- const _scnr = createScanner(source);
312
- const currentOffset = () => _scnr.index();
313
- const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
314
- const _initLoc = currentPosition();
315
- const _initOffset = currentOffset();
316
- const _context = {
317
- currentType: 13 /* TokenTypes.EOF */,
318
- offset: _initOffset,
319
- startLoc: _initLoc,
320
- endLoc: _initLoc,
321
- lastType: 13 /* TokenTypes.EOF */,
322
- lastOffset: _initOffset,
323
- lastStartLoc: _initLoc,
324
- lastEndLoc: _initLoc,
325
- braceNest: 0,
326
- inLinked: false,
327
- text: ''
328
- };
329
- const context = () => _context;
330
- const { onError } = options;
331
- function emitError(code, pos, offset, ...args) {
332
- const ctx = context();
333
- pos.column += offset;
334
- pos.offset += offset;
335
- if (onError) {
336
- const loc = location ? createLocation(ctx.startLoc, pos) : null;
337
- const err = createCompileError(code, loc, {
338
- domain: ERROR_DOMAIN$3,
339
- args
340
- });
341
- onError(err);
342
- }
343
- }
344
- function getToken(context, type, value) {
345
- context.endLoc = currentPosition();
346
- context.currentType = type;
347
- const token = { type };
348
- if (location) {
349
- token.loc = createLocation(context.startLoc, context.endLoc);
350
- }
351
- if (value != null) {
352
- token.value = value;
353
- }
354
- return token;
355
- }
356
- const getEndToken = (context) => getToken(context, 13 /* TokenTypes.EOF */);
357
- function eat(scnr, ch) {
358
- if (scnr.currentChar() === ch) {
359
- scnr.next();
360
- return ch;
361
- }
362
- else {
363
- emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
364
- return '';
365
- }
366
- }
367
- function peekSpaces(scnr) {
368
- let buf = '';
369
- while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
370
- buf += scnr.currentPeek();
371
- scnr.peek();
372
- }
373
- return buf;
374
- }
375
- function skipSpaces(scnr) {
376
- const buf = peekSpaces(scnr);
377
- scnr.skipToPeek();
378
- return buf;
379
- }
380
- function isIdentifierStart(ch) {
381
- if (ch === EOF) {
382
- return false;
383
- }
384
- const cc = ch.charCodeAt(0);
385
- return ((cc >= 97 && cc <= 122) || // a-z
386
- (cc >= 65 && cc <= 90) || // A-Z
387
- cc === 95 // _
388
- );
389
- }
390
- function isNumberStart(ch) {
391
- if (ch === EOF) {
392
- return false;
393
- }
394
- const cc = ch.charCodeAt(0);
395
- return cc >= 48 && cc <= 57; // 0-9
396
- }
397
- function isNamedIdentifierStart(scnr, context) {
398
- const { currentType } = context;
399
- if (currentType !== 2 /* TokenTypes.BraceLeft */) {
400
- return false;
401
- }
402
- peekSpaces(scnr);
403
- const ret = isIdentifierStart(scnr.currentPeek());
404
- scnr.resetPeek();
405
- return ret;
406
- }
407
- function isListIdentifierStart(scnr, context) {
408
- const { currentType } = context;
409
- if (currentType !== 2 /* TokenTypes.BraceLeft */) {
410
- return false;
411
- }
412
- peekSpaces(scnr);
413
- const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
414
- const ret = isNumberStart(ch);
415
- scnr.resetPeek();
416
- return ret;
417
- }
418
- function isLiteralStart(scnr, context) {
419
- const { currentType } = context;
420
- if (currentType !== 2 /* TokenTypes.BraceLeft */) {
421
- return false;
422
- }
423
- peekSpaces(scnr);
424
- const ret = scnr.currentPeek() === LITERAL_DELIMITER;
425
- scnr.resetPeek();
426
- return ret;
427
- }
428
- function isLinkedDotStart(scnr, context) {
429
- const { currentType } = context;
430
- if (currentType !== 7 /* TokenTypes.LinkedAlias */) {
431
- return false;
432
- }
433
- peekSpaces(scnr);
434
- const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
435
- scnr.resetPeek();
436
- return ret;
437
- }
438
- function isLinkedModifierStart(scnr, context) {
439
- const { currentType } = context;
440
- if (currentType !== 8 /* TokenTypes.LinkedDot */) {
441
- return false;
442
- }
443
- peekSpaces(scnr);
444
- const ret = isIdentifierStart(scnr.currentPeek());
445
- scnr.resetPeek();
446
- return ret;
447
- }
448
- function isLinkedDelimiterStart(scnr, context) {
449
- const { currentType } = context;
450
- if (!(currentType === 7 /* TokenTypes.LinkedAlias */ ||
451
- currentType === 11 /* TokenTypes.LinkedModifier */)) {
452
- return false;
453
- }
454
- peekSpaces(scnr);
455
- const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
456
- scnr.resetPeek();
457
- return ret;
458
- }
459
- function isLinkedReferStart(scnr, context) {
460
- const { currentType } = context;
461
- if (currentType !== 9 /* TokenTypes.LinkedDelimiter */) {
462
- return false;
463
- }
464
- const fn = () => {
465
- const ch = scnr.currentPeek();
466
- if (ch === "{" /* TokenChars.BraceLeft */) {
467
- return isIdentifierStart(scnr.peek());
468
- }
469
- else if (ch === "@" /* TokenChars.LinkedAlias */ ||
470
- ch === "|" /* TokenChars.Pipe */ ||
471
- ch === ":" /* TokenChars.LinkedDelimiter */ ||
472
- ch === "." /* TokenChars.LinkedDot */ ||
473
- ch === CHAR_SP ||
474
- !ch) {
475
- return false;
476
- }
477
- else if (ch === CHAR_LF) {
478
- scnr.peek();
479
- return fn();
480
- }
481
- else {
482
- // other characters
483
- return isTextStart(scnr, false);
484
- }
485
- };
486
- const ret = fn();
487
- scnr.resetPeek();
488
- return ret;
489
- }
490
- function isPluralStart(scnr) {
491
- peekSpaces(scnr);
492
- const ret = scnr.currentPeek() === "|" /* TokenChars.Pipe */;
493
- scnr.resetPeek();
494
- return ret;
495
- }
496
- function isTextStart(scnr, reset = true) {
497
- const fn = (hasSpace = false, prev = '') => {
498
- const ch = scnr.currentPeek();
499
- if (ch === "{" /* TokenChars.BraceLeft */) {
500
- return hasSpace;
501
- }
502
- else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
503
- return hasSpace;
504
- }
505
- else if (ch === "|" /* TokenChars.Pipe */) {
506
- return !(prev === CHAR_SP || prev === CHAR_LF);
507
- }
508
- else if (ch === CHAR_SP) {
509
- scnr.peek();
510
- return fn(true, CHAR_SP);
511
- }
512
- else if (ch === CHAR_LF) {
513
- scnr.peek();
514
- return fn(true, CHAR_LF);
515
- }
516
- else {
517
- return true;
518
- }
519
- };
520
- const ret = fn();
521
- reset && scnr.resetPeek();
522
- return ret;
523
- }
524
- function takeChar(scnr, fn) {
525
- const ch = scnr.currentChar();
526
- if (ch === EOF) {
527
- return EOF;
528
- }
529
- if (fn(ch)) {
530
- scnr.next();
531
- return ch;
532
- }
533
- return null;
534
- }
535
- function isIdentifier(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
- function takeIdentifierChar(scnr) {
545
- return takeChar(scnr, isIdentifier);
546
- }
547
- function isNamedIdentifier(ch) {
548
- const cc = ch.charCodeAt(0);
549
- return ((cc >= 97 && cc <= 122) || // a-z
550
- (cc >= 65 && cc <= 90) || // A-Z
551
- (cc >= 48 && cc <= 57) || // 0-9
552
- cc === 95 || // _
553
- cc === 36 || // $
554
- cc === 45 // -
555
- );
556
- }
557
- function takeNamedIdentifierChar(scnr) {
558
- return takeChar(scnr, isNamedIdentifier);
559
- }
560
- function isDigit(ch) {
561
- const cc = ch.charCodeAt(0);
562
- return cc >= 48 && cc <= 57; // 0-9
563
- }
564
- function takeDigit(scnr) {
565
- return takeChar(scnr, isDigit);
566
- }
567
- function isHexDigit(ch) {
568
- const cc = ch.charCodeAt(0);
569
- return ((cc >= 48 && cc <= 57) || // 0-9
570
- (cc >= 65 && cc <= 70) || // A-F
571
- (cc >= 97 && cc <= 102)); // a-f
572
- }
573
- function takeHexDigit(scnr) {
574
- return takeChar(scnr, isHexDigit);
575
- }
576
- function getDigits(scnr) {
577
- let ch = '';
578
- let num = '';
579
- while ((ch = takeDigit(scnr))) {
580
- num += ch;
581
- }
582
- return num;
583
- }
584
- function readText(scnr) {
585
- let buf = '';
586
- while (true) {
587
- const ch = scnr.currentChar();
588
- if (ch === "{" /* TokenChars.BraceLeft */ ||
589
- ch === "}" /* TokenChars.BraceRight */ ||
590
- ch === "@" /* TokenChars.LinkedAlias */ ||
591
- ch === "|" /* TokenChars.Pipe */ ||
592
- !ch) {
593
- break;
594
- }
595
- else if (ch === CHAR_SP || ch === CHAR_LF) {
596
- if (isTextStart(scnr)) {
597
- buf += ch;
598
- scnr.next();
599
- }
600
- else if (isPluralStart(scnr)) {
601
- break;
602
- }
603
- else {
604
- buf += ch;
605
- scnr.next();
606
- }
607
- }
608
- else {
609
- buf += ch;
610
- scnr.next();
611
- }
612
- }
613
- return buf;
614
- }
615
- function readNamedIdentifier(scnr) {
616
- skipSpaces(scnr);
617
- let ch = '';
618
- let name = '';
619
- while ((ch = takeNamedIdentifierChar(scnr))) {
620
- name += ch;
621
- }
622
- if (scnr.currentChar() === EOF) {
623
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
624
- }
625
- return name;
626
- }
627
- function readListIdentifier(scnr) {
628
- skipSpaces(scnr);
629
- let value = '';
630
- if (scnr.currentChar() === '-') {
631
- scnr.next();
632
- value += `-${getDigits(scnr)}`;
633
- }
634
- else {
635
- value += getDigits(scnr);
636
- }
637
- if (scnr.currentChar() === EOF) {
638
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
639
- }
640
- return value;
641
- }
642
- function isLiteral(ch) {
643
- return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
644
- }
645
- function readLiteral(scnr) {
646
- skipSpaces(scnr);
647
- // eslint-disable-next-line no-useless-escape
648
- eat(scnr, `\'`);
649
- let ch = '';
650
- let literal = '';
651
- while ((ch = takeChar(scnr, isLiteral))) {
652
- if (ch === '\\') {
653
- literal += readEscapeSequence(scnr);
654
- }
655
- else {
656
- literal += ch;
657
- }
658
- }
659
- const current = scnr.currentChar();
660
- if (current === CHAR_LF || current === EOF) {
661
- emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
662
- // TODO: Is it correct really?
663
- if (current === CHAR_LF) {
664
- scnr.next();
665
- // eslint-disable-next-line no-useless-escape
666
- eat(scnr, `\'`);
667
- }
668
- return literal;
669
- }
670
- // eslint-disable-next-line no-useless-escape
671
- eat(scnr, `\'`);
672
- return literal;
673
- }
674
- function readEscapeSequence(scnr) {
675
- const ch = scnr.currentChar();
676
- switch (ch) {
677
- case '\\':
678
- case `\'`: // eslint-disable-line no-useless-escape
679
- scnr.next();
680
- return `\\${ch}`;
681
- case 'u':
682
- return readUnicodeEscapeSequence(scnr, ch, 4);
683
- case 'U':
684
- return readUnicodeEscapeSequence(scnr, ch, 6);
685
- default:
686
- emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
687
- return '';
688
- }
689
- }
690
- function readUnicodeEscapeSequence(scnr, unicode, digits) {
691
- eat(scnr, unicode);
692
- let sequence = '';
693
- for (let i = 0; i < digits; i++) {
694
- const ch = takeHexDigit(scnr);
695
- if (!ch) {
696
- emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
697
- break;
698
- }
699
- sequence += ch;
700
- }
701
- return `\\${unicode}${sequence}`;
702
- }
703
- function isInvalidIdentifier(ch) {
704
- return (ch !== "{" /* TokenChars.BraceLeft */ &&
705
- ch !== "}" /* TokenChars.BraceRight */ &&
706
- ch !== CHAR_SP &&
707
- ch !== CHAR_LF);
708
- }
709
- function readInvalidIdentifier(scnr) {
710
- skipSpaces(scnr);
711
- let ch = '';
712
- let identifiers = '';
713
- while ((ch = takeChar(scnr, isInvalidIdentifier))) {
714
- identifiers += ch;
715
- }
716
- return identifiers;
717
- }
718
- function readLinkedModifier(scnr) {
719
- let ch = '';
720
- let name = '';
721
- while ((ch = takeIdentifierChar(scnr))) {
722
- name += ch;
723
- }
724
- return name;
725
- }
726
- function readLinkedRefer(scnr) {
727
- const fn = (buf) => {
728
- const ch = scnr.currentChar();
729
- if (ch === "{" /* TokenChars.BraceLeft */ ||
730
- ch === "@" /* TokenChars.LinkedAlias */ ||
731
- ch === "|" /* TokenChars.Pipe */ ||
732
- ch === "(" /* TokenChars.ParenLeft */ ||
733
- ch === ")" /* TokenChars.ParenRight */ ||
734
- !ch) {
735
- return buf;
736
- }
737
- else if (ch === CHAR_SP) {
738
- return buf;
739
- }
740
- else if (ch === CHAR_LF || ch === DOT) {
741
- buf += ch;
742
- scnr.next();
743
- return fn(buf);
744
- }
745
- else {
746
- buf += ch;
747
- scnr.next();
748
- return fn(buf);
749
- }
750
- };
751
- return fn('');
752
- }
753
- function readPlural(scnr) {
754
- skipSpaces(scnr);
755
- const plural = eat(scnr, "|" /* TokenChars.Pipe */);
756
- skipSpaces(scnr);
757
- return plural;
758
- }
759
- // TODO: We need refactoring of token parsing ...
760
- function readTokenInPlaceholder(scnr, context) {
761
- let token = null;
762
- const ch = scnr.currentChar();
763
- switch (ch) {
764
- case "{" /* TokenChars.BraceLeft */:
765
- if (context.braceNest >= 1) {
766
- emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
767
- }
768
- scnr.next();
769
- token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
770
- skipSpaces(scnr);
771
- context.braceNest++;
772
- return token;
773
- case "}" /* TokenChars.BraceRight */:
774
- if (context.braceNest > 0 &&
775
- context.currentType === 2 /* TokenTypes.BraceLeft */) {
776
- emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
777
- }
778
- scnr.next();
779
- token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
780
- context.braceNest--;
781
- context.braceNest > 0 && skipSpaces(scnr);
782
- if (context.inLinked && context.braceNest === 0) {
783
- context.inLinked = false;
784
- }
785
- return token;
786
- case "@" /* TokenChars.LinkedAlias */:
787
- if (context.braceNest > 0) {
788
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
789
- }
790
- token = readTokenInLinked(scnr, context) || getEndToken(context);
791
- context.braceNest = 0;
792
- return token;
793
- default: {
794
- let validNamedIdentifier = true;
795
- let validListIdentifier = true;
796
- let validLiteral = true;
797
- if (isPluralStart(scnr)) {
798
- if (context.braceNest > 0) {
799
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
800
- }
801
- token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
802
- // reset
803
- context.braceNest = 0;
804
- context.inLinked = false;
805
- return token;
806
- }
807
- if (context.braceNest > 0 &&
808
- (context.currentType === 4 /* TokenTypes.Named */ ||
809
- context.currentType === 5 /* TokenTypes.List */ ||
810
- context.currentType === 6 /* TokenTypes.Literal */)) {
811
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
812
- context.braceNest = 0;
813
- return readToken(scnr, context);
814
- }
815
- if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
816
- token = getToken(context, 4 /* TokenTypes.Named */, readNamedIdentifier(scnr));
817
- skipSpaces(scnr);
818
- return token;
819
- }
820
- if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
821
- token = getToken(context, 5 /* TokenTypes.List */, readListIdentifier(scnr));
822
- skipSpaces(scnr);
823
- return token;
824
- }
825
- if ((validLiteral = isLiteralStart(scnr, context))) {
826
- token = getToken(context, 6 /* TokenTypes.Literal */, readLiteral(scnr));
827
- skipSpaces(scnr);
828
- return token;
829
- }
830
- if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
831
- // TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
832
- token = getToken(context, 12 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
833
- emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
834
- skipSpaces(scnr);
835
- return token;
836
- }
837
- break;
838
- }
839
- }
840
- return token;
841
- }
842
- // TODO: We need refactoring of token parsing ...
843
- function readTokenInLinked(scnr, context) {
844
- const { currentType } = context;
845
- let token = null;
846
- const ch = scnr.currentChar();
847
- if ((currentType === 7 /* TokenTypes.LinkedAlias */ ||
848
- currentType === 8 /* TokenTypes.LinkedDot */ ||
849
- currentType === 11 /* TokenTypes.LinkedModifier */ ||
850
- currentType === 9 /* TokenTypes.LinkedDelimiter */) &&
851
- (ch === CHAR_LF || ch === CHAR_SP)) {
852
- emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
853
- }
854
- switch (ch) {
855
- case "@" /* TokenChars.LinkedAlias */:
856
- scnr.next();
857
- token = getToken(context, 7 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
858
- context.inLinked = true;
859
- return token;
860
- case "." /* TokenChars.LinkedDot */:
861
- skipSpaces(scnr);
862
- scnr.next();
863
- return getToken(context, 8 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
864
- case ":" /* TokenChars.LinkedDelimiter */:
865
- skipSpaces(scnr);
866
- scnr.next();
867
- return getToken(context, 9 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
868
- default:
869
- if (isPluralStart(scnr)) {
870
- token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
871
- // reset
872
- context.braceNest = 0;
873
- context.inLinked = false;
874
- return token;
875
- }
876
- if (isLinkedDotStart(scnr, context) ||
877
- isLinkedDelimiterStart(scnr, context)) {
878
- skipSpaces(scnr);
879
- return readTokenInLinked(scnr, context);
880
- }
881
- if (isLinkedModifierStart(scnr, context)) {
882
- skipSpaces(scnr);
883
- return getToken(context, 11 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
884
- }
885
- if (isLinkedReferStart(scnr, context)) {
886
- skipSpaces(scnr);
887
- if (ch === "{" /* TokenChars.BraceLeft */) {
888
- // scan the placeholder
889
- return readTokenInPlaceholder(scnr, context) || token;
890
- }
891
- else {
892
- return getToken(context, 10 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
893
- }
894
- }
895
- if (currentType === 7 /* TokenTypes.LinkedAlias */) {
896
- emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
897
- }
898
- context.braceNest = 0;
899
- context.inLinked = false;
900
- return readToken(scnr, context);
901
- }
902
- }
903
- // TODO: We need refactoring of token parsing ...
904
- function readToken(scnr, context) {
905
- let token = { type: 13 /* TokenTypes.EOF */ };
906
- if (context.braceNest > 0) {
907
- return readTokenInPlaceholder(scnr, context) || getEndToken(context);
908
- }
909
- if (context.inLinked) {
910
- return readTokenInLinked(scnr, context) || getEndToken(context);
911
- }
912
- const ch = scnr.currentChar();
913
- switch (ch) {
914
- case "{" /* TokenChars.BraceLeft */:
915
- return readTokenInPlaceholder(scnr, context) || getEndToken(context);
916
- case "}" /* TokenChars.BraceRight */:
917
- emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
918
- scnr.next();
919
- return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
920
- case "@" /* TokenChars.LinkedAlias */:
921
- return readTokenInLinked(scnr, context) || getEndToken(context);
922
- default: {
923
- if (isPluralStart(scnr)) {
924
- token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
925
- // reset
926
- context.braceNest = 0;
927
- context.inLinked = false;
928
- return token;
929
- }
930
- if (isTextStart(scnr)) {
931
- return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
932
- }
933
- break;
934
- }
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, 13 /* 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$2 = '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
- // eslint-disable-next-line no-useless-escape
967
- case `\\\'`:
968
- // eslint-disable-next-line no-useless-escape
969
- return `\'`;
970
- default: {
971
- const codePoint = parseInt(codePoint4 || codePoint6, 16);
972
- if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
973
- return String.fromCodePoint(codePoint);
974
- }
975
- // invalid ...
976
- // Replace them with U+FFFD REPLACEMENT CHARACTER.
977
- return '�';
978
- }
979
- }
980
- }
981
- function createParser(options = {}) {
982
- const location = options.location !== false;
983
- const { onError } = options;
984
- function emitError(tokenzer, code, start, offset, ...args) {
985
- const end = tokenzer.currentPosition();
986
- end.offset += offset;
987
- end.column += offset;
988
- if (onError) {
989
- const loc = location ? createLocation(start, end) : null;
990
- const err = createCompileError(code, loc, {
991
- domain: ERROR_DOMAIN$2,
992
- args
993
- });
994
- onError(err);
995
- }
996
- }
997
- function startNode(type, offset, loc) {
998
- const node = { type };
999
- if (location) {
1000
- node.start = offset;
1001
- node.end = offset;
1002
- node.loc = { start: loc, end: loc };
1003
- }
1004
- return node;
1005
- }
1006
- function endNode(node, offset, pos, type) {
1007
- if (type) {
1008
- node.type = type;
1009
- }
1010
- if (location) {
1011
- node.end = offset;
1012
- if (node.loc) {
1013
- node.loc.end = pos;
1014
- }
1015
- }
1016
- }
1017
- function parseText(tokenizer, value) {
1018
- const context = tokenizer.context();
1019
- const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
1020
- node.value = value;
1021
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1022
- return node;
1023
- }
1024
- function parseList(tokenizer, index) {
1025
- const context = tokenizer.context();
1026
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
1027
- const node = startNode(5 /* NodeTypes.List */, offset, loc);
1028
- node.index = parseInt(index, 10);
1029
- tokenizer.nextToken(); // skip brach right
1030
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1031
- return node;
1032
- }
1033
- function parseNamed(tokenizer, key) {
1034
- const context = tokenizer.context();
1035
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
1036
- const node = startNode(4 /* NodeTypes.Named */, offset, loc);
1037
- node.key = key;
1038
- tokenizer.nextToken(); // skip brach right
1039
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1040
- return node;
1041
- }
1042
- function parseLiteral(tokenizer, value) {
1043
- const context = tokenizer.context();
1044
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
1045
- const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
1046
- node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
1047
- tokenizer.nextToken(); // skip brach right
1048
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1049
- return node;
1050
- }
1051
- function parseLinkedModifier(tokenizer) {
1052
- const token = tokenizer.nextToken();
1053
- const context = tokenizer.context();
1054
- const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
1055
- const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
1056
- if (token.type !== 11 /* TokenTypes.LinkedModifier */) {
1057
- // empty modifier
1058
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
1059
- node.value = '';
1060
- endNode(node, offset, loc);
1061
- return {
1062
- nextConsumeToken: token,
1063
- node
1064
- };
1065
- }
1066
- // check token
1067
- if (token.value == null) {
1068
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1069
- }
1070
- node.value = token.value || '';
1071
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1072
- return {
1073
- node
1074
- };
1075
- }
1076
- function parseLinkedKey(tokenizer, value) {
1077
- const context = tokenizer.context();
1078
- const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
1079
- node.value = value;
1080
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1081
- return node;
1082
- }
1083
- function parseLinked(tokenizer) {
1084
- const context = tokenizer.context();
1085
- const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
1086
- let token = tokenizer.nextToken();
1087
- if (token.type === 8 /* TokenTypes.LinkedDot */) {
1088
- const parsed = parseLinkedModifier(tokenizer);
1089
- linkedNode.modifier = parsed.node;
1090
- token = parsed.nextConsumeToken || tokenizer.nextToken();
1091
- }
1092
- // asset check token
1093
- if (token.type !== 9 /* TokenTypes.LinkedDelimiter */) {
1094
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1095
- }
1096
- token = tokenizer.nextToken();
1097
- // skip brace left
1098
- if (token.type === 2 /* TokenTypes.BraceLeft */) {
1099
- token = tokenizer.nextToken();
1100
- }
1101
- switch (token.type) {
1102
- case 10 /* TokenTypes.LinkedKey */:
1103
- if (token.value == null) {
1104
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1105
- }
1106
- linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
1107
- break;
1108
- case 4 /* TokenTypes.Named */:
1109
- if (token.value == null) {
1110
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1111
- }
1112
- linkedNode.key = parseNamed(tokenizer, token.value || '');
1113
- break;
1114
- case 5 /* TokenTypes.List */:
1115
- if (token.value == null) {
1116
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1117
- }
1118
- linkedNode.key = parseList(tokenizer, token.value || '');
1119
- break;
1120
- case 6 /* TokenTypes.Literal */:
1121
- if (token.value == null) {
1122
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1123
- }
1124
- linkedNode.key = parseLiteral(tokenizer, token.value || '');
1125
- break;
1126
- default: {
1127
- // empty key
1128
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
1129
- const nextContext = tokenizer.context();
1130
- const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
1131
- emptyLinkedKeyNode.value = '';
1132
- endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
1133
- linkedNode.key = emptyLinkedKeyNode;
1134
- endNode(linkedNode, nextContext.offset, nextContext.startLoc);
1135
- return {
1136
- nextConsumeToken: token,
1137
- node: linkedNode
1138
- };
1139
- }
1140
- }
1141
- endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
1142
- return {
1143
- node: linkedNode
1144
- };
1145
- }
1146
- function parseMessage(tokenizer) {
1147
- const context = tokenizer.context();
1148
- const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
1149
- ? tokenizer.currentOffset()
1150
- : context.offset;
1151
- const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
1152
- ? context.endLoc
1153
- : context.startLoc;
1154
- const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
1155
- node.items = [];
1156
- let nextToken = null;
1157
- do {
1158
- const token = nextToken || tokenizer.nextToken();
1159
- nextToken = null;
1160
- switch (token.type) {
1161
- case 0 /* TokenTypes.Text */:
1162
- if (token.value == null) {
1163
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1164
- }
1165
- node.items.push(parseText(tokenizer, token.value || ''));
1166
- break;
1167
- case 5 /* TokenTypes.List */:
1168
- if (token.value == null) {
1169
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1170
- }
1171
- node.items.push(parseList(tokenizer, token.value || ''));
1172
- break;
1173
- case 4 /* TokenTypes.Named */:
1174
- if (token.value == null) {
1175
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1176
- }
1177
- node.items.push(parseNamed(tokenizer, token.value || ''));
1178
- break;
1179
- case 6 /* TokenTypes.Literal */:
1180
- if (token.value == null) {
1181
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1182
- }
1183
- node.items.push(parseLiteral(tokenizer, token.value || ''));
1184
- break;
1185
- case 7 /* TokenTypes.LinkedAlias */: {
1186
- const parsed = parseLinked(tokenizer);
1187
- node.items.push(parsed.node);
1188
- nextToken = parsed.nextConsumeToken || null;
1189
- break;
1190
- }
1191
- }
1192
- } while (context.currentType !== 13 /* TokenTypes.EOF */ &&
1193
- context.currentType !== 1 /* TokenTypes.Pipe */);
1194
- // adjust message node loc
1195
- const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
1196
- ? context.lastOffset
1197
- : tokenizer.currentOffset();
1198
- const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
1199
- ? context.lastEndLoc
1200
- : tokenizer.currentPosition();
1201
- endNode(node, endOffset, endLoc);
1202
- return node;
1203
- }
1204
- function parsePlural(tokenizer, offset, loc, msgNode) {
1205
- const context = tokenizer.context();
1206
- let hasEmptyMessage = msgNode.items.length === 0;
1207
- const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
1208
- node.cases = [];
1209
- node.cases.push(msgNode);
1210
- do {
1211
- const msg = parseMessage(tokenizer);
1212
- if (!hasEmptyMessage) {
1213
- hasEmptyMessage = msg.items.length === 0;
1214
- }
1215
- node.cases.push(msg);
1216
- } while (context.currentType !== 13 /* TokenTypes.EOF */);
1217
- if (hasEmptyMessage) {
1218
- emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
1219
- }
1220
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1221
- return node;
1222
- }
1223
- function parseResource(tokenizer) {
1224
- const context = tokenizer.context();
1225
- const { offset, startLoc } = context;
1226
- const msgNode = parseMessage(tokenizer);
1227
- if (context.currentType === 13 /* TokenTypes.EOF */) {
1228
- return msgNode;
1229
- }
1230
- else {
1231
- return parsePlural(tokenizer, offset, startLoc, msgNode);
1232
- }
1233
- }
1234
- function parse(source) {
1235
- const tokenizer = createTokenizer(source, assign({}, options));
1236
- const context = tokenizer.context();
1237
- const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
1238
- if (location && node.loc) {
1239
- node.loc.source = source;
1240
- }
1241
- node.body = parseResource(tokenizer);
1242
- if (options.onCacheKey) {
1243
- node.cacheKey = options.onCacheKey(source);
1244
- }
1245
- // assert whether achieved to EOF
1246
- if (context.currentType !== 13 /* TokenTypes.EOF */) {
1247
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
1248
- }
1249
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1250
- return node;
1251
- }
1252
- return { parse };
1253
- }
1254
- function getTokenCaption(token) {
1255
- if (token.type === 13 /* TokenTypes.EOF */) {
1256
- return 'EOF';
1257
- }
1258
- const name = (token.value || '').replace(/\r?\n/gu, '\\n');
1259
- return name.length > 10 ? name.slice(0, 9) + '…' : name;
1260
- }
1261
-
1262
- function createTransformer(ast, options = {} // eslint-disable-line
1263
- ) {
1264
- const _context = {
1265
- ast,
1266
- helpers: new Set()
1267
- };
1268
- const context = () => _context;
1269
- const helper = (name) => {
1270
- _context.helpers.add(name);
1271
- return name;
1272
- };
1273
- return { context, helper };
1274
- }
1275
- function traverseNodes(nodes, transformer) {
1276
- for (let i = 0; i < nodes.length; i++) {
1277
- traverseNode(nodes[i], transformer);
1278
- }
1279
- }
1280
- function traverseNode(node, transformer) {
1281
- // TODO: if we need pre-hook of transform, should be implemented to here
1282
- switch (node.type) {
1283
- case 1 /* NodeTypes.Plural */:
1284
- traverseNodes(node.cases, transformer);
1285
- transformer.helper("plural" /* HelperNameMap.PLURAL */);
1286
- break;
1287
- case 2 /* NodeTypes.Message */:
1288
- traverseNodes(node.items, transformer);
1289
- break;
1290
- case 6 /* NodeTypes.Linked */: {
1291
- const linked = node;
1292
- traverseNode(linked.key, transformer);
1293
- transformer.helper("linked" /* HelperNameMap.LINKED */);
1294
- transformer.helper("type" /* HelperNameMap.TYPE */);
1295
- break;
1296
- }
1297
- case 5 /* NodeTypes.List */:
1298
- transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1299
- transformer.helper("list" /* HelperNameMap.LIST */);
1300
- break;
1301
- case 4 /* NodeTypes.Named */:
1302
- transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1303
- transformer.helper("named" /* HelperNameMap.NAMED */);
1304
- break;
1305
- }
1306
- // TODO: if we need post-hook of transform, should be implemented to here
1307
- }
1308
- // transform AST
1309
- function transform(ast, options = {} // eslint-disable-line
1310
- ) {
1311
- const transformer = createTransformer(ast);
1312
- transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
1313
- // traverse
1314
- ast.body && traverseNode(ast.body, transformer);
1315
- // set meta information
1316
- const context = transformer.context();
1317
- ast.helpers = Array.from(context.helpers);
1318
- }
1319
-
1320
- function optimize(ast) {
1321
- const body = ast.body;
1322
- if (body.type === 2 /* NodeTypes.Message */) {
1323
- optimizeMessageNode(body);
1324
- }
1325
- else {
1326
- body.cases.forEach(c => optimizeMessageNode(c));
1327
- }
1328
- return ast;
1329
- }
1330
- function optimizeMessageNode(message) {
1331
- if (message.items.length === 1) {
1332
- const item = message.items[0];
1333
- if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
1334
- message.static = item.value;
1335
- delete item.value; // optimization for size
1336
- }
1337
- }
1338
- else {
1339
- const values = [];
1340
- for (let i = 0; i < message.items.length; i++) {
1341
- const item = message.items[i];
1342
- if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
1343
- break;
1344
- }
1345
- if (item.value == null) {
1346
- break;
1347
- }
1348
- values.push(item.value);
1349
- }
1350
- if (values.length === message.items.length) {
1351
- message.static = join(values);
1352
- for (let i = 0; i < message.items.length; i++) {
1353
- const item = message.items[i];
1354
- if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
1355
- delete item.value; // optimization for size
1356
- }
1357
- }
1358
- }
1359
- }
1360
- }
1361
-
1362
- const ERROR_DOMAIN$1 = 'minifier';
1363
- /* eslint-disable @typescript-eslint/no-explicit-any */
1364
- function minify(node) {
1365
- node.t = node.type;
1366
- switch (node.type) {
1367
- case 0 /* NodeTypes.Resource */: {
1368
- const resource = node;
1369
- minify(resource.body);
1370
- resource.b = resource.body;
1371
- delete resource.body;
1372
- break;
1373
- }
1374
- case 1 /* NodeTypes.Plural */: {
1375
- const plural = node;
1376
- const cases = plural.cases;
1377
- for (let i = 0; i < cases.length; i++) {
1378
- minify(cases[i]);
1379
- }
1380
- plural.c = cases;
1381
- delete plural.cases;
1382
- break;
1383
- }
1384
- case 2 /* NodeTypes.Message */: {
1385
- const message = node;
1386
- const items = message.items;
1387
- for (let i = 0; i < items.length; i++) {
1388
- minify(items[i]);
1389
- }
1390
- message.i = items;
1391
- delete message.items;
1392
- if (message.static) {
1393
- message.s = message.static;
1394
- delete message.static;
1395
- }
1396
- break;
1397
- }
1398
- case 3 /* NodeTypes.Text */:
1399
- case 9 /* NodeTypes.Literal */:
1400
- case 8 /* NodeTypes.LinkedModifier */:
1401
- case 7 /* NodeTypes.LinkedKey */: {
1402
- const valueNode = node;
1403
- if (valueNode.value) {
1404
- valueNode.v = valueNode.value;
1405
- delete valueNode.value;
1406
- }
1407
- break;
1408
- }
1409
- case 6 /* NodeTypes.Linked */: {
1410
- const linked = node;
1411
- minify(linked.key);
1412
- linked.k = linked.key;
1413
- delete linked.key;
1414
- if (linked.modifier) {
1415
- minify(linked.modifier);
1416
- linked.m = linked.modifier;
1417
- delete linked.modifier;
1418
- }
1419
- break;
1420
- }
1421
- case 5 /* NodeTypes.List */: {
1422
- const list = node;
1423
- list.i = list.index;
1424
- delete list.index;
1425
- break;
1426
- }
1427
- case 4 /* NodeTypes.Named */: {
1428
- const named = node;
1429
- named.k = named.key;
1430
- delete named.key;
1431
- break;
1432
- }
1433
- default:
1434
- {
1435
- throw createCompileError(CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE, null, {
1436
- domain: ERROR_DOMAIN$1,
1437
- args: [node.type]
1438
- });
1439
- }
1440
- }
1441
- delete node.type;
1442
- }
1443
- /* eslint-enable @typescript-eslint/no-explicit-any */
1444
-
1445
- // eslint-disable-next-line @typescript-eslint/triple-slash-reference
1446
- /// <reference types="source-map-js" />
1447
- const ERROR_DOMAIN = 'parser';
1448
- function createCodeGenerator(ast, options) {
1449
- const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
1450
- const location = options.location !== false;
1451
- const _context = {
1452
- filename,
1453
- code: '',
1454
- column: 1,
1455
- line: 1,
1456
- offset: 0,
1457
- map: undefined,
1458
- breakLineCode,
1459
- needIndent: _needIndent,
1460
- indentLevel: 0
1461
- };
1462
- if (location && ast.loc) {
1463
- _context.source = ast.loc.source;
1464
- }
1465
- const context = () => _context;
1466
- function push(code, node) {
1467
- _context.code += code;
1468
- }
1469
- function _newline(n, withBreakLine = true) {
1470
- const _breakLineCode = withBreakLine ? breakLineCode : '';
1471
- push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
1472
- }
1473
- function indent(withNewLine = true) {
1474
- const level = ++_context.indentLevel;
1475
- withNewLine && _newline(level);
1476
- }
1477
- function deindent(withNewLine = true) {
1478
- const level = --_context.indentLevel;
1479
- withNewLine && _newline(level);
1480
- }
1481
- function newline() {
1482
- _newline(_context.indentLevel);
1483
- }
1484
- const helper = (key) => `_${key}`;
1485
- const needIndent = () => _context.needIndent;
1486
- return {
1487
- context,
1488
- push,
1489
- indent,
1490
- deindent,
1491
- newline,
1492
- helper,
1493
- needIndent
1494
- };
1495
- }
1496
- function generateLinkedNode(generator, node) {
1497
- const { helper } = generator;
1498
- generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
1499
- generateNode(generator, node.key);
1500
- if (node.modifier) {
1501
- generator.push(`, `);
1502
- generateNode(generator, node.modifier);
1503
- generator.push(`, _type`);
1504
- }
1505
- else {
1506
- generator.push(`, undefined, _type`);
1507
- }
1508
- generator.push(`)`);
1509
- }
1510
- function generateMessageNode(generator, node) {
1511
- const { helper, needIndent } = generator;
1512
- generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
1513
- generator.indent(needIndent());
1514
- const length = node.items.length;
1515
- for (let i = 0; i < length; i++) {
1516
- generateNode(generator, node.items[i]);
1517
- if (i === length - 1) {
1518
- break;
1519
- }
1520
- generator.push(', ');
1521
- }
1522
- generator.deindent(needIndent());
1523
- generator.push('])');
1524
- }
1525
- function generatePluralNode(generator, node) {
1526
- const { helper, needIndent } = generator;
1527
- if (node.cases.length > 1) {
1528
- generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
1529
- generator.indent(needIndent());
1530
- const length = node.cases.length;
1531
- for (let i = 0; i < length; i++) {
1532
- generateNode(generator, node.cases[i]);
1533
- if (i === length - 1) {
1534
- break;
1535
- }
1536
- generator.push(', ');
1537
- }
1538
- generator.deindent(needIndent());
1539
- generator.push(`])`);
1540
- }
1541
- }
1542
- function generateResource(generator, node) {
1543
- if (node.body) {
1544
- generateNode(generator, node.body);
1545
- }
1546
- else {
1547
- generator.push('null');
1548
- }
1549
- }
1550
- function generateNode(generator, node) {
1551
- const { helper } = generator;
1552
- switch (node.type) {
1553
- case 0 /* NodeTypes.Resource */:
1554
- generateResource(generator, node);
1555
- break;
1556
- case 1 /* NodeTypes.Plural */:
1557
- generatePluralNode(generator, node);
1558
- break;
1559
- case 2 /* NodeTypes.Message */:
1560
- generateMessageNode(generator, node);
1561
- break;
1562
- case 6 /* NodeTypes.Linked */:
1563
- generateLinkedNode(generator, node);
1564
- break;
1565
- case 8 /* NodeTypes.LinkedModifier */:
1566
- generator.push(JSON.stringify(node.value), node);
1567
- break;
1568
- case 7 /* NodeTypes.LinkedKey */:
1569
- generator.push(JSON.stringify(node.value), node);
1570
- break;
1571
- case 5 /* NodeTypes.List */:
1572
- generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
1573
- break;
1574
- case 4 /* NodeTypes.Named */:
1575
- generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
1576
- break;
1577
- case 9 /* NodeTypes.Literal */:
1578
- generator.push(JSON.stringify(node.value), node);
1579
- break;
1580
- case 3 /* NodeTypes.Text */:
1581
- generator.push(JSON.stringify(node.value), node);
1582
- break;
1583
- default:
1584
- {
1585
- throw createCompileError(CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE, null, {
1586
- domain: ERROR_DOMAIN,
1587
- args: [node.type]
1588
- });
1589
- }
1590
- }
1591
- }
1592
- // generate code from AST
1593
- const generate = (ast, options = {}) => {
1594
- const mode = isString(options.mode) ? options.mode : 'normal';
1595
- const filename = isString(options.filename)
1596
- ? options.filename
1597
- : 'message.intl';
1598
- const sourceMap = !!options.sourceMap;
1599
- // prettier-ignore
1600
- const breakLineCode = options.breakLineCode != null
1601
- ? options.breakLineCode
1602
- : mode === 'arrow'
1603
- ? ';'
1604
- : '\n';
1605
- const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
1606
- const helpers = ast.helpers || [];
1607
- const generator = createCodeGenerator(ast, {
1608
- mode,
1609
- filename,
1610
- sourceMap,
1611
- breakLineCode,
1612
- needIndent
1613
- });
1614
- generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
1615
- generator.indent(needIndent);
1616
- if (helpers.length > 0) {
1617
- generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
1618
- generator.newline();
1619
- }
1620
- generator.push(`return `);
1621
- generateNode(generator, ast);
1622
- generator.deindent(needIndent);
1623
- generator.push(`}`);
1624
- delete ast.helpers;
1625
- const { code, map } = generator.context();
1626
- return {
1627
- ast,
1628
- code,
1629
- map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
1630
- };
1631
- };
1632
-
1633
- function baseCompile$1(source, options = {}) {
1634
- const assignedOptions = assign({}, options);
1635
- const jit = !!assignedOptions.jit;
1636
- const enalbeMinify = !!assignedOptions.minify;
1637
- const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
1638
- // parse source codes
1639
- const parser = createParser(assignedOptions);
1640
- const ast = parser.parse(source);
1641
- if (!jit) {
1642
- // transform ASTs
1643
- transform(ast, assignedOptions);
1644
- // generate javascript codes
1645
- return generate(ast, assignedOptions);
1646
- }
1647
- else {
1648
- // optimize ASTs
1649
- enambeOptimize && optimize(ast);
1650
- // minimize ASTs
1651
- enalbeMinify && minify(ast);
1652
- // In JIT mode, no ast transform, no code generation.
1653
- return { ast, code: '' };
1654
- }
1655
- }
1656
210
 
1657
211
  const pathStateMachine = [];
1658
212
  pathStateMachine[0 /* States.BEFORE_PATH */] = {
@@ -2277,7 +831,7 @@ function appendItemToChain(chain, target, blocks) {
2277
831
  * Intlify core-base version
2278
832
  * @internal
2279
833
  */
2280
- const VERSION = '10.0.0-alpha.5';
834
+ const VERSION = '10.0.0-beta.2';
2281
835
  const NOT_REOSLVED = -1;
2282
836
  const DEFAULT_LOCALE = 'en-US';
2283
837
  const MISSING_RESOLVE_VALUE = '';
@@ -2593,13 +1147,6 @@ function formatMessagePart(ctx, node) {
2593
1147
  }
2594
1148
  }
2595
1149
 
2596
- const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
2597
- function checkHtmlMessage(source, warnHtmlMessage) {
2598
- if (warnHtmlMessage && detectHtmlTag(source)) {
2599
- warn(format$1(WARN_MESSAGE, { source }));
2600
- }
2601
- }
2602
- const defaultOnCacheKey = (message) => message;
2603
1150
  let compileCache = Object.create(null);
2604
1151
  function clearCompileCache() {
2605
1152
  compileCache = Object.create(null);
@@ -2607,46 +1154,9 @@ function clearCompileCache() {
2607
1154
  const isMessageAST = (val) => isObject(val) &&
2608
1155
  (val.t === 0 || val.type === 0) &&
2609
1156
  ('b' in val || 'body' in val);
2610
- function baseCompile(message, options = {}) {
2611
- // error detecting on compile
2612
- let detectError = false;
2613
- const onError = options.onError || defaultOnError;
2614
- options.onError = (err) => {
2615
- detectError = true;
2616
- onError(err);
2617
- };
2618
- // compile with mesasge-compiler
2619
- return { ...baseCompile$1(message, options), detectError };
2620
- }
2621
1157
  /* #__NO_SIDE_EFFECTS__ */
2622
1158
  function compile(message, context) {
2623
- if (isString(message)) {
2624
- // check HTML message
2625
- const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
2626
- ? context.warnHtmlMessage
2627
- : true;
2628
- checkHtmlMessage(message, warnHtmlMessage);
2629
- // check caches
2630
- const onCacheKey = context.onCacheKey || defaultOnCacheKey;
2631
- const cacheKey = onCacheKey(message);
2632
- const cached = compileCache[cacheKey];
2633
- if (cached) {
2634
- return cached;
2635
- }
2636
- // compile with JIT mode
2637
- const { ast, detectError } = baseCompile(message, {
2638
- ...context,
2639
- location: true,
2640
- jit: true
2641
- });
2642
- // compose message function from AST
2643
- const msg = format(ast);
2644
- // if occurred compile error, don't cache
2645
- return !detectError
2646
- ? (compileCache[cacheKey] = msg)
2647
- : msg;
2648
- }
2649
- else {
1159
+ {
2650
1160
  if (!isMessageAST(message)) {
2651
1161
  warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
2652
1162
  return (() => message);