@intlify/message-compiler 9.11.1 → 9.12.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
- * message-compiler v9.11.1
2
+ * message-compiler v9.12.1
3
3
  * (c) 2024 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -21,6 +21,23 @@ function createLocation(start, end, source) {
21
21
  return loc;
22
22
  }
23
23
 
24
+ const CompileWarnCodes = {
25
+ USE_MODULO_SYNTAX: 1,
26
+ __EXTEND_POINT__: 2
27
+ };
28
+ /** @internal */
29
+ const warnMessages = {
30
+ [CompileWarnCodes.USE_MODULO_SYNTAX]: `Use modulo before '{{0}}'.`
31
+ };
32
+ function createCompileWarn(code, loc, ...args) {
33
+ const msg = (process.env.NODE_ENV !== 'production') ? format(warnMessages[code] || '', ...(args || [])) : code;
34
+ const message = { message: String(msg), code };
35
+ if (loc) {
36
+ message.location = loc;
37
+ }
38
+ return message;
39
+ }
40
+
24
41
  const CompileErrorCodes = {
25
42
  // tokenizer error codes
26
43
  EXPECTED_TOKEN: 1,
@@ -415,33 +432,46 @@ function createTokenizer(source, options = {}) {
415
432
  }
416
433
  return null;
417
434
  }
435
+ function isIdentifier(ch) {
436
+ const cc = ch.charCodeAt(0);
437
+ return ((cc >= 97 && cc <= 122) || // a-z
438
+ (cc >= 65 && cc <= 90) || // A-Z
439
+ (cc >= 48 && cc <= 57) || // 0-9
440
+ cc === 95 || // _
441
+ cc === 36 // $
442
+ );
443
+ }
418
444
  function takeIdentifierChar(scnr) {
419
- const closure = (ch) => {
420
- const cc = ch.charCodeAt(0);
421
- return ((cc >= 97 && cc <= 122) || // a-z
422
- (cc >= 65 && cc <= 90) || // A-Z
423
- (cc >= 48 && cc <= 57) || // 0-9
424
- cc === 95 || // _
425
- cc === 36 // $
426
- );
427
- };
428
- return takeChar(scnr, closure);
445
+ return takeChar(scnr, isIdentifier);
446
+ }
447
+ function isNamedIdentifier(ch) {
448
+ const cc = ch.charCodeAt(0);
449
+ return ((cc >= 97 && cc <= 122) || // a-z
450
+ (cc >= 65 && cc <= 90) || // A-Z
451
+ (cc >= 48 && cc <= 57) || // 0-9
452
+ cc === 95 || // _
453
+ cc === 36 || // $
454
+ cc === 45 // -
455
+ );
456
+ }
457
+ function takeNamedIdentifierChar(scnr) {
458
+ return takeChar(scnr, isNamedIdentifier);
459
+ }
460
+ function isDigit(ch) {
461
+ const cc = ch.charCodeAt(0);
462
+ return cc >= 48 && cc <= 57; // 0-9
429
463
  }
430
464
  function takeDigit(scnr) {
431
- const closure = (ch) => {
432
- const cc = ch.charCodeAt(0);
433
- return cc >= 48 && cc <= 57; // 0-9
434
- };
435
- return takeChar(scnr, closure);
465
+ return takeChar(scnr, isDigit);
466
+ }
467
+ function isHexDigit(ch) {
468
+ const cc = ch.charCodeAt(0);
469
+ return ((cc >= 48 && cc <= 57) || // 0-9
470
+ (cc >= 65 && cc <= 70) || // A-F
471
+ (cc >= 97 && cc <= 102)); // a-f
436
472
  }
437
473
  function takeHexDigit(scnr) {
438
- const closure = (ch) => {
439
- const cc = ch.charCodeAt(0);
440
- return ((cc >= 48 && cc <= 57) || // 0-9
441
- (cc >= 65 && cc <= 70) || // A-F
442
- (cc >= 97 && cc <= 102)); // a-f
443
- };
444
- return takeChar(scnr, closure);
474
+ return takeChar(scnr, isHexDigit);
445
475
  }
446
476
  function getDigits(scnr) {
447
477
  let ch = '';
@@ -505,7 +535,7 @@ function createTokenizer(source, options = {}) {
505
535
  skipSpaces(scnr);
506
536
  let ch = '';
507
537
  let name = '';
508
- while ((ch = takeIdentifierChar(scnr))) {
538
+ while ((ch = takeNamedIdentifierChar(scnr))) {
509
539
  name += ch;
510
540
  }
511
541
  if (scnr.currentChar() === EOF) {
@@ -528,14 +558,16 @@ function createTokenizer(source, options = {}) {
528
558
  }
529
559
  return value;
530
560
  }
561
+ function isLiteral(ch) {
562
+ return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
563
+ }
531
564
  function readLiteral(scnr) {
532
565
  skipSpaces(scnr);
533
566
  // eslint-disable-next-line no-useless-escape
534
567
  eat(scnr, `\'`);
535
568
  let ch = '';
536
569
  let literal = '';
537
- const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
538
- while ((ch = takeChar(scnr, fn))) {
570
+ while ((ch = takeChar(scnr, isLiteral))) {
539
571
  if (ch === '\\') {
540
572
  literal += readEscapeSequence(scnr);
541
573
  }
@@ -587,15 +619,17 @@ function createTokenizer(source, options = {}) {
587
619
  }
588
620
  return `\\${unicode}${sequence}`;
589
621
  }
622
+ function isInvalidIdentifier(ch) {
623
+ return (ch !== "{" /* TokenChars.BraceLeft */ &&
624
+ ch !== "}" /* TokenChars.BraceRight */ &&
625
+ ch !== CHAR_SP &&
626
+ ch !== CHAR_LF);
627
+ }
590
628
  function readInvalidIdentifier(scnr) {
591
629
  skipSpaces(scnr);
592
630
  let ch = '';
593
631
  let identifiers = '';
594
- const closure = (ch) => ch !== "{" /* TokenChars.BraceLeft */ &&
595
- ch !== "}" /* TokenChars.BraceRight */ &&
596
- ch !== CHAR_SP &&
597
- ch !== CHAR_LF;
598
- while ((ch = takeChar(scnr, closure))) {
632
+ while ((ch = takeChar(scnr, isInvalidIdentifier))) {
599
633
  identifiers += ch;
600
634
  }
601
635
  return identifiers;
@@ -872,7 +906,7 @@ function fromEscapeSequence(match, codePoint4, codePoint6) {
872
906
  }
873
907
  function createParser(options = {}) {
874
908
  const location = options.location !== false;
875
- const { onError } = options;
909
+ const { onError, onWarn } = options;
876
910
  function emitError(tokenzer, code, start, offset, ...args) {
877
911
  const end = tokenzer.currentPosition();
878
912
  end.offset += offset;
@@ -886,6 +920,15 @@ function createParser(options = {}) {
886
920
  onError(err);
887
921
  }
888
922
  }
923
+ function emitWarn(tokenzer, code, start, offset, ...args) {
924
+ const end = tokenzer.currentPosition();
925
+ end.offset += offset;
926
+ end.column += offset;
927
+ if (onWarn) {
928
+ const loc = location ? createLocation(start, end) : null;
929
+ onWarn(createCompileWarn(code, loc, args));
930
+ }
931
+ }
889
932
  function startNode(type, offset, loc) {
890
933
  const node = { type };
891
934
  if (location) {
@@ -922,11 +965,14 @@ function createParser(options = {}) {
922
965
  endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
923
966
  return node;
924
967
  }
925
- function parseNamed(tokenizer, key) {
968
+ function parseNamed(tokenizer, key, modulo) {
926
969
  const context = tokenizer.context();
927
970
  const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
928
971
  const node = startNode(4 /* NodeTypes.Named */, offset, loc);
929
972
  node.key = key;
973
+ if (modulo === true) {
974
+ node.modulo = true;
975
+ }
930
976
  tokenizer.nextToken(); // skip brach right
931
977
  endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
932
978
  return node;
@@ -1046,6 +1092,7 @@ function createParser(options = {}) {
1046
1092
  const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
1047
1093
  node.items = [];
1048
1094
  let nextToken = null;
1095
+ let modulo = null;
1049
1096
  do {
1050
1097
  const token = nextToken || tokenizer.nextToken();
1051
1098
  nextToken = null;
@@ -1062,11 +1109,18 @@ function createParser(options = {}) {
1062
1109
  }
1063
1110
  node.items.push(parseList(tokenizer, token.value || ''));
1064
1111
  break;
1112
+ case 4 /* TokenTypes.Modulo */:
1113
+ modulo = true;
1114
+ break;
1065
1115
  case 5 /* TokenTypes.Named */:
1066
1116
  if (token.value == null) {
1067
1117
  emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1068
1118
  }
1069
- node.items.push(parseNamed(tokenizer, token.value || ''));
1119
+ node.items.push(parseNamed(tokenizer, token.value || '', !!modulo));
1120
+ if (modulo) {
1121
+ emitWarn(tokenizer, CompileWarnCodes.USE_MODULO_SYNTAX, context.lastStartLoc, 0, getTokenCaption(token));
1122
+ modulo = null;
1123
+ }
1070
1124
  break;
1071
1125
  case 7 /* TokenTypes.Literal */:
1072
1126
  if (token.value == null) {
@@ -1603,4 +1657,4 @@ function baseCompile(source, options = {}) {
1603
1657
  }
1604
1658
  }
1605
1659
 
1606
- export { CompileErrorCodes, ERROR_DOMAIN$2 as ERROR_DOMAIN, LOCATION_STUB, baseCompile, createCompileError, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages };
1660
+ export { CompileErrorCodes, CompileWarnCodes, ERROR_DOMAIN$2 as ERROR_DOMAIN, LOCATION_STUB, baseCompile, createCompileError, createCompileWarn, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages, warnMessages };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * message-compiler v9.11.1
2
+ * message-compiler v9.12.1
3
3
  * (c) 2024 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -23,6 +23,23 @@ function createLocation(start, end, source) {
23
23
  return loc;
24
24
  }
25
25
 
26
+ const CompileWarnCodes = {
27
+ USE_MODULO_SYNTAX: 1,
28
+ __EXTEND_POINT__: 2
29
+ };
30
+ /** @internal */
31
+ const warnMessages = {
32
+ [CompileWarnCodes.USE_MODULO_SYNTAX]: `Use modulo before '{{0}}'.`
33
+ };
34
+ function createCompileWarn(code, loc, ...args) {
35
+ const msg = code;
36
+ const message = { message: String(msg), code };
37
+ if (loc) {
38
+ message.location = loc;
39
+ }
40
+ return message;
41
+ }
42
+
26
43
  const CompileErrorCodes = {
27
44
  // tokenizer error codes
28
45
  EXPECTED_TOKEN: 1,
@@ -415,33 +432,46 @@ function createTokenizer(source, options = {}) {
415
432
  }
416
433
  return null;
417
434
  }
435
+ function isIdentifier(ch) {
436
+ const cc = ch.charCodeAt(0);
437
+ return ((cc >= 97 && cc <= 122) || // a-z
438
+ (cc >= 65 && cc <= 90) || // A-Z
439
+ (cc >= 48 && cc <= 57) || // 0-9
440
+ cc === 95 || // _
441
+ cc === 36 // $
442
+ );
443
+ }
418
444
  function takeIdentifierChar(scnr) {
419
- const closure = (ch) => {
420
- const cc = ch.charCodeAt(0);
421
- return ((cc >= 97 && cc <= 122) || // a-z
422
- (cc >= 65 && cc <= 90) || // A-Z
423
- (cc >= 48 && cc <= 57) || // 0-9
424
- cc === 95 || // _
425
- cc === 36 // $
426
- );
427
- };
428
- return takeChar(scnr, closure);
445
+ return takeChar(scnr, isIdentifier);
446
+ }
447
+ function isNamedIdentifier(ch) {
448
+ const cc = ch.charCodeAt(0);
449
+ return ((cc >= 97 && cc <= 122) || // a-z
450
+ (cc >= 65 && cc <= 90) || // A-Z
451
+ (cc >= 48 && cc <= 57) || // 0-9
452
+ cc === 95 || // _
453
+ cc === 36 || // $
454
+ cc === 45 // -
455
+ );
456
+ }
457
+ function takeNamedIdentifierChar(scnr) {
458
+ return takeChar(scnr, isNamedIdentifier);
459
+ }
460
+ function isDigit(ch) {
461
+ const cc = ch.charCodeAt(0);
462
+ return cc >= 48 && cc <= 57; // 0-9
429
463
  }
430
464
  function takeDigit(scnr) {
431
- const closure = (ch) => {
432
- const cc = ch.charCodeAt(0);
433
- return cc >= 48 && cc <= 57; // 0-9
434
- };
435
- return takeChar(scnr, closure);
465
+ return takeChar(scnr, isDigit);
466
+ }
467
+ function isHexDigit(ch) {
468
+ const cc = ch.charCodeAt(0);
469
+ return ((cc >= 48 && cc <= 57) || // 0-9
470
+ (cc >= 65 && cc <= 70) || // A-F
471
+ (cc >= 97 && cc <= 102)); // a-f
436
472
  }
437
473
  function takeHexDigit(scnr) {
438
- const closure = (ch) => {
439
- const cc = ch.charCodeAt(0);
440
- return ((cc >= 48 && cc <= 57) || // 0-9
441
- (cc >= 65 && cc <= 70) || // A-F
442
- (cc >= 97 && cc <= 102)); // a-f
443
- };
444
- return takeChar(scnr, closure);
474
+ return takeChar(scnr, isHexDigit);
445
475
  }
446
476
  function getDigits(scnr) {
447
477
  let ch = '';
@@ -505,7 +535,7 @@ function createTokenizer(source, options = {}) {
505
535
  skipSpaces(scnr);
506
536
  let ch = '';
507
537
  let name = '';
508
- while ((ch = takeIdentifierChar(scnr))) {
538
+ while ((ch = takeNamedIdentifierChar(scnr))) {
509
539
  name += ch;
510
540
  }
511
541
  if (scnr.currentChar() === EOF) {
@@ -528,14 +558,16 @@ function createTokenizer(source, options = {}) {
528
558
  }
529
559
  return value;
530
560
  }
561
+ function isLiteral(ch) {
562
+ return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
563
+ }
531
564
  function readLiteral(scnr) {
532
565
  skipSpaces(scnr);
533
566
  // eslint-disable-next-line no-useless-escape
534
567
  eat(scnr, `\'`);
535
568
  let ch = '';
536
569
  let literal = '';
537
- const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
538
- while ((ch = takeChar(scnr, fn))) {
570
+ while ((ch = takeChar(scnr, isLiteral))) {
539
571
  if (ch === '\\') {
540
572
  literal += readEscapeSequence(scnr);
541
573
  }
@@ -587,15 +619,17 @@ function createTokenizer(source, options = {}) {
587
619
  }
588
620
  return `\\${unicode}${sequence}`;
589
621
  }
622
+ function isInvalidIdentifier(ch) {
623
+ return (ch !== "{" /* TokenChars.BraceLeft */ &&
624
+ ch !== "}" /* TokenChars.BraceRight */ &&
625
+ ch !== CHAR_SP &&
626
+ ch !== CHAR_LF);
627
+ }
590
628
  function readInvalidIdentifier(scnr) {
591
629
  skipSpaces(scnr);
592
630
  let ch = '';
593
631
  let identifiers = '';
594
- const closure = (ch) => ch !== "{" /* TokenChars.BraceLeft */ &&
595
- ch !== "}" /* TokenChars.BraceRight */ &&
596
- ch !== CHAR_SP &&
597
- ch !== CHAR_LF;
598
- while ((ch = takeChar(scnr, closure))) {
632
+ while ((ch = takeChar(scnr, isInvalidIdentifier))) {
599
633
  identifiers += ch;
600
634
  }
601
635
  return identifiers;
@@ -872,7 +906,7 @@ function fromEscapeSequence(match, codePoint4, codePoint6) {
872
906
  }
873
907
  function createParser(options = {}) {
874
908
  const location = options.location !== false;
875
- const { onError } = options;
909
+ const { onError, onWarn } = options;
876
910
  function emitError(tokenzer, code, start, offset, ...args) {
877
911
  const end = tokenzer.currentPosition();
878
912
  end.offset += offset;
@@ -886,6 +920,15 @@ function createParser(options = {}) {
886
920
  onError(err);
887
921
  }
888
922
  }
923
+ function emitWarn(tokenzer, code, start, offset, ...args) {
924
+ const end = tokenzer.currentPosition();
925
+ end.offset += offset;
926
+ end.column += offset;
927
+ if (onWarn) {
928
+ const loc = location ? createLocation(start, end) : null;
929
+ onWarn(createCompileWarn(code, loc, args));
930
+ }
931
+ }
889
932
  function startNode(type, offset, loc) {
890
933
  const node = { type };
891
934
  if (location) {
@@ -922,11 +965,14 @@ function createParser(options = {}) {
922
965
  endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
923
966
  return node;
924
967
  }
925
- function parseNamed(tokenizer, key) {
968
+ function parseNamed(tokenizer, key, modulo) {
926
969
  const context = tokenizer.context();
927
970
  const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
928
971
  const node = startNode(4 /* NodeTypes.Named */, offset, loc);
929
972
  node.key = key;
973
+ if (modulo === true) {
974
+ node.modulo = true;
975
+ }
930
976
  tokenizer.nextToken(); // skip brach right
931
977
  endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
932
978
  return node;
@@ -1046,6 +1092,7 @@ function createParser(options = {}) {
1046
1092
  const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
1047
1093
  node.items = [];
1048
1094
  let nextToken = null;
1095
+ let modulo = null;
1049
1096
  do {
1050
1097
  const token = nextToken || tokenizer.nextToken();
1051
1098
  nextToken = null;
@@ -1062,11 +1109,18 @@ function createParser(options = {}) {
1062
1109
  }
1063
1110
  node.items.push(parseList(tokenizer, token.value || ''));
1064
1111
  break;
1112
+ case 4 /* TokenTypes.Modulo */:
1113
+ modulo = true;
1114
+ break;
1065
1115
  case 5 /* TokenTypes.Named */:
1066
1116
  if (token.value == null) {
1067
1117
  emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1068
1118
  }
1069
- node.items.push(parseNamed(tokenizer, token.value || ''));
1119
+ node.items.push(parseNamed(tokenizer, token.value || '', !!modulo));
1120
+ if (modulo) {
1121
+ emitWarn(tokenizer, CompileWarnCodes.USE_MODULO_SYNTAX, context.lastStartLoc, 0, getTokenCaption(token));
1122
+ modulo = null;
1123
+ }
1070
1124
  break;
1071
1125
  case 7 /* TokenTypes.Literal */:
1072
1126
  if (token.value == null) {
@@ -1588,13 +1642,16 @@ function baseCompile(source, options = {}) {
1588
1642
  }
1589
1643
 
1590
1644
  exports.CompileErrorCodes = CompileErrorCodes;
1645
+ exports.CompileWarnCodes = CompileWarnCodes;
1591
1646
  exports.ERROR_DOMAIN = ERROR_DOMAIN;
1592
1647
  exports.LOCATION_STUB = LOCATION_STUB;
1593
1648
  exports.baseCompile = baseCompile;
1594
1649
  exports.createCompileError = createCompileError;
1650
+ exports.createCompileWarn = createCompileWarn;
1595
1651
  exports.createLocation = createLocation;
1596
1652
  exports.createParser = createParser;
1597
1653
  exports.createPosition = createPosition;
1598
1654
  exports.defaultOnError = defaultOnError;
1599
1655
  exports.detectHtmlTag = detectHtmlTag;
1600
1656
  exports.errorMessages = errorMessages;
1657
+ exports.warnMessages = warnMessages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/message-compiler",
3
- "version": "9.11.1",
3
+ "version": "9.12.1",
4
4
  "description": "@intlify/message-compiler",
5
5
  "keywords": [
6
6
  "compiler",
@@ -34,7 +34,7 @@
34
34
  "types": "dist/message-compiler.d.ts",
35
35
  "dependencies": {
36
36
  "source-map-js": "^1.0.2",
37
- "@intlify/shared": "9.11.1"
37
+ "@intlify/shared": "9.12.1"
38
38
  },
39
39
  "engines": {
40
40
  "node": ">= 16"