@ibyar/expressions 0.5.6 → 0.5.62

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.
Files changed (85) hide show
  1. package/api/abstract.js +5 -0
  2. package/api/abstract.js.map +1 -1
  3. package/api/class/class.d.ts +25 -0
  4. package/api/class/class.js +27 -0
  5. package/api/class/class.js.map +1 -1
  6. package/api/computing/return.d.ts +4 -0
  7. package/api/computing/return.js +5 -0
  8. package/api/computing/return.js.map +1 -1
  9. package/api/computing/throw.d.ts +4 -0
  10. package/api/computing/throw.js +4 -0
  11. package/api/computing/throw.js.map +1 -1
  12. package/api/definition/bind.d.ts +6 -0
  13. package/api/definition/bind.js +6 -0
  14. package/api/definition/bind.js.map +1 -1
  15. package/api/definition/function.js +1 -0
  16. package/api/definition/function.js.map +1 -1
  17. package/api/definition/values.d.ts +9 -0
  18. package/api/definition/values.js +21 -0
  19. package/api/definition/values.js.map +1 -1
  20. package/api/deserialize/deserialize.d.ts +5 -0
  21. package/api/deserialize/deserialize.js +5 -0
  22. package/api/deserialize/deserialize.js.map +1 -1
  23. package/api/expression.d.ts +146 -0
  24. package/api/module/export.d.ts +63 -0
  25. package/api/module/export.js +63 -0
  26. package/api/module/export.js.map +1 -1
  27. package/api/module/import.d.ts +53 -0
  28. package/api/module/import.js +53 -0
  29. package/api/module/import.js.map +1 -1
  30. package/api/operators/grouping.d.ts +7 -0
  31. package/api/operators/grouping.js +7 -0
  32. package/api/operators/grouping.js.map +1 -1
  33. package/api/operators/pipeline.d.ts +12 -0
  34. package/api/operators/pipeline.js +12 -0
  35. package/api/operators/pipeline.js.map +1 -1
  36. package/api/operators/unary.js +3 -0
  37. package/api/operators/unary.js.map +1 -1
  38. package/api/statement/control/block.d.ts +4 -0
  39. package/api/statement/control/block.js +4 -0
  40. package/api/statement/control/block.js.map +1 -1
  41. package/api/statement/control/empty.d.ts +7 -0
  42. package/api/statement/control/empty.js +7 -0
  43. package/api/statement/control/empty.js.map +1 -1
  44. package/api/statement/control/if.d.ts +5 -0
  45. package/api/statement/control/if.js +5 -0
  46. package/api/statement/control/if.js.map +1 -1
  47. package/api/statement/control/switch.d.ts +6 -0
  48. package/api/statement/control/switch.js +9 -0
  49. package/api/statement/control/switch.js.map +1 -1
  50. package/api/statement/control/terminate.d.ts +8 -0
  51. package/api/statement/control/terminate.js +8 -0
  52. package/api/statement/control/terminate.js.map +1 -1
  53. package/api/statement/iterations/for.d.ts +5 -0
  54. package/api/statement/iterations/for.js +13 -0
  55. package/api/statement/iterations/for.js.map +1 -1
  56. package/api/statement/iterations/while.d.ts +6 -0
  57. package/api/statement/iterations/while.js +10 -0
  58. package/api/statement/iterations/while.js.map +1 -1
  59. package/package.json +2 -2
  60. package/scope/proxy.d.ts +3 -0
  61. package/scope/proxy.js +4 -0
  62. package/scope/proxy.js.map +1 -1
  63. package/scope/scope.d.ts +68 -0
  64. package/scope/scope.js +5 -0
  65. package/scope/scope.js.map +1 -1
  66. package/scope/stack.d.ts +74 -0
  67. package/scope/stack.js +6 -0
  68. package/scope/stack.js.map +1 -1
  69. package/v8/enums.js +31 -0
  70. package/v8/enums.js.map +1 -1
  71. package/v8/inline.d.ts +36 -0
  72. package/v8/inline.js +407 -5
  73. package/v8/inline.js.map +1 -1
  74. package/v8/nodes.js +2 -1
  75. package/v8/nodes.js.map +1 -1
  76. package/v8/parser.d.ts +8 -0
  77. package/v8/parser.js +304 -1
  78. package/v8/parser.js.map +1 -1
  79. package/v8/stream.d.ts +4 -0
  80. package/v8/stream.js +79 -17
  81. package/v8/stream.js.map +1 -1
  82. package/v8/token.d.ts +229 -0
  83. package/v8/token.js +236 -1
  84. package/v8/token.js.map +1 -1
  85. package/visit/visitor.d.ts +3 -0
package/v8/stream.js CHANGED
@@ -122,6 +122,7 @@ export class TokenStream {
122
122
  }
123
123
  }
124
124
  readTokensPair(open, close, tokens) {
125
+ // check pair
125
126
  let count = 0;
126
127
  let token;
127
128
  while (true) {
@@ -246,6 +247,7 @@ export class TokenStreamImpl extends TokenStream {
246
247
  }
247
248
  else if (this.isRadixInteger()
248
249
  || this.isNumber()
250
+ // || this.isRegExp()
249
251
  || this.isString()
250
252
  || this.isTemplateLiteral()
251
253
  || this.isCurlY()
@@ -331,6 +333,7 @@ export class TokenStreamImpl extends TokenStream {
331
333
  return true;
332
334
  }
333
335
  if (c0 == '' || this.pos >= this.expression.length) {
336
+ // Unterminated string literal
334
337
  break;
335
338
  }
336
339
  addLiteralChar(c0);
@@ -348,6 +351,21 @@ export class TokenStreamImpl extends TokenStream {
348
351
  return false;
349
352
  }
350
353
  scanTemplateSpan() {
354
+ // When scanning a TemplateSpan, we are looking for the following construct:
355
+ // TEMPLATE_SPAN ::
356
+ // ` LiteralChars* ${
357
+ // | } LiteralChars* ${
358
+ //
359
+ // TEMPLATE_TAIL ::
360
+ // ` LiteralChars* `
361
+ // | } LiteralChar* `
362
+ //
363
+ // A TEMPLATE_SPAN should always be followed by an Expression, while a
364
+ // TEMPLATE_TAIL terminates a TemplateLiteral and does not need to be
365
+ // followed by an Expression.
366
+ // These scoped helpers save and restore the original error state, so that we
367
+ // can specially treat invalid escape sequences in templates (which are
368
+ // handled by the parser).
351
369
  let result = Token.TEMPLATE_SPAN;
352
370
  let literal = [], c0 = this.expression.charAt(this.pos);
353
371
  const advance = () => {
@@ -358,21 +376,24 @@ export class TokenStreamImpl extends TokenStream {
358
376
  while (true) {
359
377
  let c = c0;
360
378
  if (c == '`') {
361
- advance();
379
+ advance(); // Consume '`'
362
380
  result = Token.TEMPLATE_TAIL;
363
381
  break;
364
382
  }
365
383
  else if (c == '$' && this.expression.charAt(this.pos + 1) == '{') {
366
- advance();
367
- advance();
384
+ advance(); // Consume '$'
385
+ advance(); // Consume '{'
368
386
  break;
369
387
  }
370
388
  else if (c == '\\') {
371
- advance();
389
+ advance(); // Consume '\\'
372
390
  if (this.isLineTerminator(c0)) {
391
+ // The TV of LineContinuation :: \ LineTerminatorSequence is the empty
392
+ // code unit sequence.
373
393
  let lastChar = c0;
374
394
  advance();
375
395
  if (lastChar == '\r') {
396
+ // Also skip \n.
376
397
  if (c0 == '\n')
377
398
  advance();
378
399
  lastChar = '\n';
@@ -389,13 +410,17 @@ export class TokenStreamImpl extends TokenStream {
389
410
  }
390
411
  }
391
412
  else if (c == '' || this.pos >= this.expression.length) {
413
+ // Unterminated template literal
392
414
  break;
393
415
  }
394
416
  else {
395
- advance();
417
+ advance(); // Consume c.
418
+ // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A.
419
+ // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence
420
+ // consisting of the CV 0x000A.
396
421
  if (c == '\r') {
397
422
  if (c0 == '\n')
398
- advance();
423
+ advance(); // Consume '\n'
399
424
  c = '\n';
400
425
  }
401
426
  addLiteralChar(c);
@@ -412,7 +437,9 @@ export class TokenStreamImpl extends TokenStream {
412
437
  scanEscape(scanTemplateSpan) {
413
438
  let c0 = this.expression.charAt(this.pos);
414
439
  let c = c0;
440
+ // Skip escaped newlines.
415
441
  if (!scanTemplateSpan && this.isLineTerminator(c)) {
442
+ // Allow escaped CR+LF newlines in multiline string literals.
416
443
  if (c == '\r' && this.expression.charAt(this.pos + 1) == '\n')
417
444
  this.pos++;
418
445
  return '\n';
@@ -463,11 +490,16 @@ export class TokenStreamImpl extends TokenStream {
463
490
  break;
464
491
  case '8':
465
492
  case '9':
493
+ // '\8' and '\9' are disallowed in strict mode.
494
+ // Re-use the octal error state to propagate the error.
466
495
  throw new SyntaxError(this.createError(`'\\8' and '\\9' are disallowed in strict mode.`));
467
496
  }
497
+ // Other escaped characters are interpreted as their non-escaped version.
468
498
  return c;
469
499
  }
470
500
  scanUnicodeEscape() {
501
+ // Accept both \uxxxx and \u{xxxxxx}. In the latter case, the number of
502
+ // hex digits between { } is arbitrary. \ and u have already been read.
471
503
  let c0 = this.expression.charAt(this.pos);
472
504
  if (c0 == '{') {
473
505
  this.pos++;
@@ -481,7 +513,7 @@ export class TokenStreamImpl extends TokenStream {
481
513
  return this.scanHexNumber(4);
482
514
  }
483
515
  scanHexNumber(length) {
484
- if (length > 4) {
516
+ if (length > 4) { // prevent overflow
485
517
  throw new RangeError(this.createError('ScanHexNumber: length should be less than or equal to 4'));
486
518
  }
487
519
  let x = 0;
@@ -521,7 +553,7 @@ export class TokenStreamImpl extends TokenStream {
521
553
  let x = c.charCodeAt(0) - '0'.charCodeAt(0);
522
554
  if (x <= 9)
523
555
  return x;
524
- x = (x | 0x20) - ('a'.charCodeAt(0) - '0'.charCodeAt(0));
556
+ x = (x | 0x20) - ('a'.charCodeAt(0) - '0'.charCodeAt(0)); // detect 0x11..0x16 and 0x31..0x36.
525
557
  if (x <= 5)
526
558
  return x + 10;
527
559
  return -1;
@@ -615,6 +647,7 @@ export class TokenStreamImpl extends TokenStream {
615
647
  let isPrivate = false;
616
648
  let escape = false;
617
649
  let escapeFlag = false;
650
+ // check is private property
618
651
  let char = this.expression.charAt(i);
619
652
  if (char === '#') {
620
653
  isPrivate = true;
@@ -626,6 +659,7 @@ export class TokenStreamImpl extends TokenStream {
626
659
  if (char === '\\' && this.expression.charAt(i + 1) === 'u') {
627
660
  i += 2;
628
661
  escapeFlag = escape = true;
662
+ // need to valid the unicode char
629
663
  }
630
664
  else {
631
665
  return false;
@@ -753,16 +787,22 @@ export class TokenStreamImpl extends TokenStream {
753
787
  }
754
788
  return result;
755
789
  }
790
+ /**
791
+ * [JavaScript 5 Comment Types](https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/org/mozilla/javascript/ast/Comment.html)
792
+ * @returns boolean
793
+ */
756
794
  isComment() {
757
795
  const char = this.expression.charAt(this.pos);
758
796
  const nextChar = this.expression.charAt(this.pos + 1);
759
797
  let closeTag;
760
798
  let endPos;
761
799
  switch (true) {
800
+ // // line comments
762
801
  case (char === '/' && nextChar === '/'):
763
802
  closeTag = '\n';
764
803
  endPos = this.expression.indexOf(closeTag, this.pos);
765
804
  break;
805
+ // /* block comments *\/ and /** jsdoc comments *\/
766
806
  case (char === '/' && nextChar === '*'):
767
807
  closeTag = '*/';
768
808
  endPos = this.expression.indexOf(closeTag, this.pos);
@@ -770,6 +810,7 @@ export class TokenStreamImpl extends TokenStream {
770
810
  throw new SyntaxError(this.createError('Invalid or unexpected token'));
771
811
  }
772
812
  break;
813
+ // <!-- html-open line comments
773
814
  case (char === '<' && nextChar === '!'):
774
815
  if ('<!--' === this.expression.substring(this.pos, this.pos + 4)) {
775
816
  if (isStrict(this.mode)) {
@@ -791,17 +832,20 @@ export class TokenStreamImpl extends TokenStream {
791
832
  }
792
833
  break;
793
834
  }
835
+ // ^\\s*--> html-close line comments
794
836
  case (char === '-' && nextChar === '-'):
795
837
  if ('-->' === this.expression.substring(this.pos, this.pos + 3)) {
796
838
  if (isStrict(this.mode)) {
797
839
  throw new SyntaxError('Html Comment not allowed In Module');
798
840
  }
841
+ // check if '-->' is the first non-whitespace on the line
799
842
  let indexOfLastLine = this.expression.lastIndexOf('\n', this.pos + 1);
800
843
  if (indexOfLastLine == -1) {
801
844
  indexOfLastLine = 0;
802
845
  }
803
846
  let lastLine = this.expression.substring(indexOfLastLine, this.pos + 3);
804
847
  if (!/^\s*-->$/g.test(lastLine)) {
848
+ // test with close of block comments
805
849
  let lastBlockCommentPos = lastLine.lastIndexOf('*/');
806
850
  if (lastBlockCommentPos == -1) {
807
851
  return false;
@@ -842,7 +886,7 @@ export class TokenStreamImpl extends TokenStream {
842
886
  if (c == '' || this.isLineTerminator(c)) {
843
887
  return false;
844
888
  }
845
- if (c === '\\') {
889
+ if (c === '\\') { // Escape sequence.
846
890
  pattern += c;
847
891
  c = this.expression.charAt(++currentPos);
848
892
  if (c == '' || this.isLineTerminator(c)) {
@@ -860,13 +904,13 @@ export class TokenStreamImpl extends TokenStream {
860
904
  c = this.expression.charAt(++currentPos);
861
905
  }
862
906
  }
863
- currentPos++;
907
+ currentPos++; // consume '/'
864
908
  let flags = '';
865
909
  const remainFlags = TokenStreamImpl.REGEXP_FLAGS.slice();
866
910
  while (true) {
867
911
  const code = this.expression.charCodeAt(currentPos);
868
912
  const nextChar = this.expression.charAt(currentPos);
869
- if (/[\.\s]/.test(nextChar) || Number.isNaN(code)) {
913
+ if (/[\.\s]/.test(nextChar) || Number.isNaN(code) /* || FORBIDDEN_CODE_POINT.includes(code.toString(16))*/) {
870
914
  break;
871
915
  }
872
916
  const nextCharIndex = remainFlags.indexOf(nextChar);
@@ -933,6 +977,7 @@ export class TokenStreamImpl extends TokenStream {
933
977
  break;
934
978
  case 'u': {
935
979
  index++;
980
+ // interpret the following 4 characters as the hex of the unicode code point
936
981
  if ('{' === v.charAt(index)) {
937
982
  index++;
938
983
  const start = index;
@@ -953,6 +998,7 @@ export class TokenStreamImpl extends TokenStream {
953
998
  }
954
999
  case 'x': {
955
1000
  index++;
1001
+ // interpret the following 2 characters as the hex of the decimal code point
956
1002
  let codePoint = v.substring(index, index + 2);
957
1003
  if (!TokenStreamImpl.DecimalPattern.test(codePoint)) {
958
1004
  throw new Error(this.createError('Illegal escape sequence: \\x ' + codePoint));
@@ -983,6 +1029,7 @@ export class TokenStreamImpl extends TokenStream {
983
1029
  index++;
984
1030
  buffer += c;
985
1031
  break;
1032
+ // throw new Error(this.createError('Illegal escape sequence: "\\' + c + '"'));
986
1033
  }
987
1034
  let backslash = v.indexOf('\\', index);
988
1035
  if (backslash == -1) {
@@ -1409,6 +1456,7 @@ export class TokenStreamImpl extends TokenStream {
1409
1456
  this.pos++;
1410
1457
  }
1411
1458
  return true;
1459
+ // no break
1412
1460
  case ':':
1413
1461
  if (this.expression.charAt(this.pos + 1) === ':') {
1414
1462
  this.current = this.newToken(Token.QUESTION_BIND);
@@ -1435,7 +1483,7 @@ export class TokenStreamImpl extends TokenStream {
1435
1483
  return true;
1436
1484
  }
1437
1485
  return false;
1438
- case 'i':
1486
+ case 'i': // in, instanceof
1439
1487
  if (this.expression.charAt(this.pos + 1) === 'n') {
1440
1488
  if (/instanceof\s/.test(this.expression.substring(this.pos, this.pos + 11))) {
1441
1489
  this.current = this.newToken(Token.INSTANCEOF);
@@ -1449,35 +1497,35 @@ export class TokenStreamImpl extends TokenStream {
1449
1497
  }
1450
1498
  }
1451
1499
  return false;
1452
- case 't':
1500
+ case 't': // typeof
1453
1501
  if (/typeof\s/.test(this.expression.substring(this.pos, this.pos + 7))) {
1454
1502
  this.current = this.newToken(Token.TYPEOF);
1455
1503
  this.pos += 7;
1456
1504
  return true;
1457
1505
  }
1458
1506
  return false;
1459
- case 'v':
1507
+ case 'v': // void
1460
1508
  if (/void\s/.test(this.expression.substring(this.pos, this.pos + 5))) {
1461
1509
  this.current = this.newToken(Token.VOID);
1462
1510
  this.pos += 5;
1463
1511
  return true;
1464
1512
  }
1465
1513
  return false;
1466
- case 'd':
1514
+ case 'd': // delete
1467
1515
  if (/delete\s/.test(this.expression.substring(this.pos, this.pos + 7))) {
1468
1516
  this.current = this.newToken(Token.DELETE);
1469
1517
  this.pos += 7;
1470
1518
  return true;
1471
1519
  }
1472
1520
  return false;
1473
- case 'n':
1521
+ case 'n': // new
1474
1522
  if (/new\s/.test(this.expression.substring(this.pos, this.pos + 4))) {
1475
1523
  this.current = this.newToken(Token.NEW);
1476
1524
  this.pos += 4;
1477
1525
  return true;
1478
1526
  }
1479
1527
  return false;
1480
- case 'n':
1528
+ case 'n': // new
1481
1529
  if (/new\s/.test(this.expression.substring(this.pos, this.pos + 4))) {
1482
1530
  this.current = this.newToken(Token.NEW);
1483
1531
  this.pos += 4;
@@ -1491,6 +1539,13 @@ export class TokenStreamImpl extends TokenStream {
1491
1539
  isStatement() {
1492
1540
  const char = this.expression.charAt(this.pos);
1493
1541
  switch (char) {
1542
+ // case 'a':
1543
+ // if (/as\s/.test(this.expression.substring(this.pos, this.pos + 3))) {
1544
+ // this.current = this.newToken(Token.AS);
1545
+ // this.pos += 3;
1546
+ // return true;
1547
+ // }
1548
+ // return false;
1494
1549
  case 'b':
1495
1550
  if (/break\s?;?/.test(this.expression.substring(this.pos, this.pos + 7))) {
1496
1551
  this.current = this.newToken(Token.BREAK);
@@ -1616,6 +1671,13 @@ export class TokenStreamImpl extends TokenStream {
1616
1671
  return true;
1617
1672
  }
1618
1673
  return false;
1674
+ // case 'of':
1675
+ // if (/of\s/.test(this.expression.substring(this.pos, this.pos + 3))) {
1676
+ // this.current = this.newToken(Token.OF);
1677
+ // this.pos += 3;
1678
+ // return true;
1679
+ // }
1680
+ // return false;
1619
1681
  case 's':
1620
1682
  if (/switch(\s|\()/.test(this.expression.substring(this.pos, this.pos + 7))) {
1621
1683
  this.current = this.newToken(Token.SWITCH);