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

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