@intlify/core 10.0.0-alpha.4 → 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.4
2
+ * core v10.0.0-beta.1
3
3
  * (c) 2024 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -131,10 +131,6 @@ var IntlifyCore = (function (exports) {
131
131
  }
132
132
  return res.join('\n');
133
133
  }
134
- function incrementer(code) {
135
- let current = code;
136
- return () => ++current;
137
- }
138
134
 
139
135
  function warn(msg, err) {
140
136
  if (typeof console !== 'undefined') {
@@ -146,17 +142,6 @@ var IntlifyCore = (function (exports) {
146
142
  }
147
143
  }
148
144
 
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
145
  const CompileErrorCodes = {
161
146
  // tokenizer error codes
162
147
  EXPECTED_TOKEN: 1,
@@ -177,12 +162,12 @@ var IntlifyCore = (function (exports) {
177
162
  // generator error codes
178
163
  UNHANDLED_CODEGEN_NODE_TYPE: 15,
179
164
  // minifier error codes
180
- UNHANDLED_MINIFIER_NODE_TYPE: 16,
181
- // Special value for higher-order compilers to pick up the last code
182
- // to avoid collision of error codes. This should always be kept as the last
183
- // item.
184
- __EXTEND_POINT__: 17
165
+ UNHANDLED_MINIFIER_NODE_TYPE: 16
185
166
  };
167
+ // Special value for higher-order compilers to pick up the last code
168
+ // to avoid collision of error codes.
169
+ // This should always be kept as the last item.
170
+ const COMPILE_ERROR_CODES_EXTEND_POINT = 17;
186
171
  /** @internal */
187
172
  const errorMessages$1 = {
188
173
  // tokenizer error messages
@@ -218,1441 +203,6 @@ var IntlifyCore = (function (exports) {
218
203
  error.domain = domain;
219
204
  return error;
220
205
  }
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
206
 
1657
207
  const pathStateMachine = [];
1658
208
  pathStateMachine[0 /* States.BEFORE_PATH */] = {
@@ -2075,18 +625,16 @@ var IntlifyCore = (function (exports) {
2075
625
  return (payloads) => devtools && devtools.emit(hook, payloads);
2076
626
  }
2077
627
 
2078
- const code$1 = 1;
2079
- const inc$1 = incrementer(code$1);
2080
628
  const CoreWarnCodes = {
2081
- NOT_FOUND_KEY: code$1, // 1
2082
- FALLBACK_TO_TRANSLATE: inc$1(), // 2
2083
- CANNOT_FORMAT_NUMBER: inc$1(), // 3
2084
- FALLBACK_TO_NUMBER_FORMAT: inc$1(), // 4
2085
- CANNOT_FORMAT_DATE: inc$1(), // 5
2086
- FALLBACK_TO_DATE_FORMAT: inc$1(), // 6
2087
- EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: inc$1(), // 7
2088
- __EXTEND_POINT__: inc$1() // 8
629
+ NOT_FOUND_KEY: 1,
630
+ FALLBACK_TO_TRANSLATE: 2,
631
+ CANNOT_FORMAT_NUMBER: 3,
632
+ FALLBACK_TO_NUMBER_FORMAT: 4,
633
+ CANNOT_FORMAT_DATE: 5,
634
+ FALLBACK_TO_DATE_FORMAT: 6,
635
+ EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
2089
636
  };
637
+ const CORE_WARN_CODES_EXTEND_POINT = 8;
2090
638
  /** @internal */
2091
639
  const warnMessages = {
2092
640
  [CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
@@ -2101,18 +649,16 @@ var IntlifyCore = (function (exports) {
2101
649
  return format$1(warnMessages[code], ...args);
2102
650
  }
2103
651
 
2104
- const code = CompileErrorCodes.__EXTEND_POINT__;
2105
- const inc = incrementer(code);
2106
652
  const CoreErrorCodes = {
2107
- INVALID_ARGUMENT: code, // 17
2108
- INVALID_DATE_ARGUMENT: inc(), // 18
2109
- INVALID_ISO_DATE_ARGUMENT: inc(), // 19
2110
- NOT_SUPPORT_NON_STRING_MESSAGE: inc(), // 20
2111
- NOT_SUPPORT_LOCALE_PROMISE_VALUE: inc(), // 21
2112
- NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: inc(), // 22
2113
- NOT_SUPPORT_LOCALE_TYPE: inc(), // 23
2114
- __EXTEND_POINT__: inc() // 24
653
+ INVALID_ARGUMENT: COMPILE_ERROR_CODES_EXTEND_POINT, // 17
654
+ INVALID_DATE_ARGUMENT: 18,
655
+ INVALID_ISO_DATE_ARGUMENT: 19,
656
+ NOT_SUPPORT_NON_STRING_MESSAGE: 20,
657
+ NOT_SUPPORT_LOCALE_PROMISE_VALUE: 21,
658
+ NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 22,
659
+ NOT_SUPPORT_LOCALE_TYPE: 23
2115
660
  };
661
+ const CORE_ERROR_CODES_EXTEND_POINT = 24;
2116
662
  function createCoreError(code) {
2117
663
  return createCompileError(code, null, { messages: errorMessages } );
2118
664
  }
@@ -2281,7 +827,7 @@ var IntlifyCore = (function (exports) {
2281
827
  * Intlify core-base version
2282
828
  * @internal
2283
829
  */
2284
- const VERSION = '10.0.0-alpha.4';
830
+ const VERSION = '10.0.0-beta.1';
2285
831
  const NOT_REOSLVED = -1;
2286
832
  const DEFAULT_LOCALE = 'en-US';
2287
833
  const MISSING_RESOLVE_VALUE = '';
@@ -2594,13 +1140,6 @@ var IntlifyCore = (function (exports) {
2594
1140
  }
2595
1141
  }
2596
1142
 
2597
- const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
2598
- function checkHtmlMessage(source, warnHtmlMessage) {
2599
- if (warnHtmlMessage && detectHtmlTag(source)) {
2600
- warn(format$1(WARN_MESSAGE, { source }));
2601
- }
2602
- }
2603
- const defaultOnCacheKey = (message) => message;
2604
1143
  let compileCache = Object.create(null);
2605
1144
  function clearCompileCache() {
2606
1145
  compileCache = Object.create(null);
@@ -2608,55 +1147,9 @@ var IntlifyCore = (function (exports) {
2608
1147
  const isMessageAST = (val) => isObject(val) &&
2609
1148
  (val.t === 0 || val.type === 0) &&
2610
1149
  ('b' in val || 'body' in val);
2611
- function baseCompile(message, options = {}) {
2612
- // error detecting on compile
2613
- let detectError = false;
2614
- const onError = options.onError || defaultOnError;
2615
- options.onError = (err) => {
2616
- detectError = true;
2617
- onError(err);
2618
- };
2619
- // compile with mesasge-compiler
2620
- return { ...baseCompile$1(message, options), detectError };
2621
- }
2622
1150
  /* #__NO_SIDE_EFFECTS__ */
2623
- const compileToFunction = (message, context) => {
2624
- if (!isString(message)) {
2625
- throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
2626
- }
2627
- {
2628
- warn(`Runtime compilation is not supported in ${'core.runtime.global.js' }.`);
2629
- return (() => message);
2630
- }
2631
- };
2632
1151
  function compile(message, context) {
2633
- if (isString(message)) {
2634
- // check HTML message
2635
- const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
2636
- ? context.warnHtmlMessage
2637
- : true;
2638
- checkHtmlMessage(message, warnHtmlMessage);
2639
- // check caches
2640
- const onCacheKey = context.onCacheKey || defaultOnCacheKey;
2641
- const cacheKey = onCacheKey(message);
2642
- const cached = compileCache[cacheKey];
2643
- if (cached) {
2644
- return cached;
2645
- }
2646
- // compile with JIT mode
2647
- const { ast, detectError } = baseCompile(message, {
2648
- ...context,
2649
- location: true,
2650
- jit: true
2651
- });
2652
- // compose message function from AST
2653
- const msg = format(ast);
2654
- // if occurred compile error, don't cache
2655
- return !detectError
2656
- ? (compileCache[cacheKey] = msg)
2657
- : msg;
2658
- }
2659
- else {
1152
+ {
2660
1153
  if (!isMessageAST(message)) {
2661
1154
  warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
2662
1155
  return (() => message);
@@ -2701,8 +1194,10 @@ var IntlifyCore = (function (exports) {
2701
1194
  : (!messageCompiler ? () => key : key)
2702
1195
  : fallbackFormat // default by `fallbackFormat` option
2703
1196
  ? (!messageCompiler ? () => key : key)
2704
- : '';
2705
- const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== '';
1197
+ : null;
1198
+ const enableDefaultMsg = fallbackFormat ||
1199
+ (defaultMsgOrKey != null &&
1200
+ (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey)));
2706
1201
  const locale = getLocale(context, options);
2707
1202
  // escape params
2708
1203
  escapeParameter && escapeParams(options);
@@ -3412,11 +1907,15 @@ var IntlifyCore = (function (exports) {
3412
1907
  }
3413
1908
  }
3414
1909
 
1910
+ // register message compiler for jit compilation
1911
+ registerMessageCompiler(compile);
3415
1912
  // register message resolver at @intlify/core
3416
1913
  registerMessageResolver(resolveValue);
3417
1914
  // register fallback locale at @intlify/core
3418
1915
  registerLocaleFallbacker(fallbackWithLocaleChain);
3419
1916
 
1917
+ exports.CORE_ERROR_CODES_EXTEND_POINT = CORE_ERROR_CODES_EXTEND_POINT;
1918
+ exports.CORE_WARN_CODES_EXTEND_POINT = CORE_WARN_CODES_EXTEND_POINT;
3420
1919
  exports.CompileErrorCodes = CompileErrorCodes;
3421
1920
  exports.CoreErrorCodes = CoreErrorCodes;
3422
1921
  exports.CoreWarnCodes = CoreWarnCodes;
@@ -3431,7 +1930,6 @@ var IntlifyCore = (function (exports) {
3431
1930
  exports.clearDateTimeFormat = clearDateTimeFormat;
3432
1931
  exports.clearNumberFormat = clearNumberFormat;
3433
1932
  exports.compile = compile;
3434
- exports.compileToFunction = compileToFunction;
3435
1933
  exports.createCompileError = createCompileError;
3436
1934
  exports.createCoreContext = createCoreContext;
3437
1935
  exports.createCoreError = createCoreError;