@runtime-type-inspector/transpiler 3.3.5 → 3.3.7

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 (3) hide show
  1. package/index.cjs +40 -10
  2. package/index.mjs +40 -10
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -964,7 +964,7 @@
964
964
  function parseJSDoc(src) {
965
965
  var expandType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : expandTypeDepFree;
966
966
  // Parse something like: @param {Object} [kwargs={}] Optional arguments.
967
- var regex = /@param \{(.*?)\} ([\[\]a-zA-Z0-9_$=\{\}\.'" ]+)/g;
967
+ var regex = /@param \{(.*?)\} ([\[\]a-zA-Z0-9_$=\-\{\}\.'" ]+)/g;
968
968
  var matches = _toConsumableArray(src.matchAll(regex));
969
969
  /** @type {Record<string, ExpandTypeReturnType>} */
970
970
  var params = Object.create(null);
@@ -976,10 +976,22 @@
976
976
  // name: [kwargs={}] The configuration parameters.
977
977
  // name: [d = 1.0] Sample spacing
978
978
  if (name[0] === '[') {
979
- // Possible improvement: counting opening/closing brackets for perfect match
980
- var closer = name.lastIndexOf(']');
979
+ // Counting opening/closing brackets for perfect match
980
+ var openCloseCount = 1;
981
+ var i = 1;
982
+ for (; i < name.length; i++) {
983
+ var c = name[i];
984
+ if (c === '[') {
985
+ openCloseCount++;
986
+ } else if (c === ']') {
987
+ openCloseCount--;
988
+ }
989
+ if (openCloseCount === 0) {
990
+ break;
991
+ }
992
+ }
981
993
  // Afterwards name will be: d = 1.0
982
- name = name.substring(1, closer);
994
+ name = name.substring(1, i);
983
995
  // mark it for the type:
984
996
  optional = true;
985
997
  }
@@ -3610,6 +3622,12 @@
3610
3622
  var tmp = JSON.stringify(templates, null, 2).replaceAll('\n', '\n' + spaces);
3611
3623
  out += "\n".concat(spaces, "const rtiTemplates = ").concat(tmp, ";");
3612
3624
  }
3625
+ function newlineBeforeFirst() {
3626
+ if (first) {
3627
+ out += '\n';
3628
+ first = false;
3629
+ }
3630
+ }
3613
3631
  //out += `${spaces}/*${spaces} node.type=${node.type}\n${spaces}
3614
3632
  // ${JSON.stringify(jsdoc)}\n${parent}\n${spaces}*/\n`;
3615
3633
  var _loop = function _loop(name) {
@@ -3653,11 +3671,19 @@
3653
3671
  try {
3654
3672
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
3655
3673
  var element = _step.value;
3674
+ if (element.type === 'RestElement') {
3675
+ // Case for:
3676
+ // function test([a, b, ...rest] = [1, 2, 3]) {return rest;}
3677
+ /** @todo We could typecheck the rest element too */
3678
+ // console.log("Ignore RestElement", element);
3679
+ continue;
3680
+ }
3656
3681
  if (element.type !== 'Identifier') {
3657
3682
  _this2.warn('Only Identifier case handled right now');
3658
3683
  continue;
3659
3684
  }
3660
3685
  var _t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
3686
+ newlineBeforeFirst();
3661
3687
  if (templates) {
3662
3688
  out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3663
3689
  } else {
@@ -3672,13 +3698,19 @@
3672
3698
  }
3673
3699
  return 0; // continue
3674
3700
  } else if (param.left.type === 'ObjectPattern') {
3675
- if (type.type === 'object') {
3701
+ // If we have properties from JSDoc, we can create individual type inspections.
3702
+ if (type !== null && type !== void 0 && type.properties && Object.keys(type.properties).length) {
3676
3703
  // Add a type assertion for each property of the ObjectPattern
3677
3704
  var _iterator2 = _createForOfIteratorHelper(param.left.properties),
3678
3705
  _step2;
3679
3706
  try {
3680
3707
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3681
3708
  var property = _step2.value;
3709
+ if (property.type !== 'ObjectProperty') {
3710
+ // E.g. case of RestElement, {a, b, c, ...kwargs}
3711
+ // console.log("Ignore property", property);
3712
+ continue;
3713
+ }
3682
3714
  if (property.key.type !== 'Identifier') {
3683
3715
  _this2.warn('ObjectPattern> Only Identifier case handled right now');
3684
3716
  continue;
@@ -3694,6 +3726,7 @@
3694
3726
  continue;
3695
3727
  }
3696
3728
  var _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3729
+ newlineBeforeFirst();
3697
3730
  if (templates) {
3698
3731
  out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3699
3732
  } else {
@@ -3707,7 +3740,7 @@
3707
3740
  _iterator2.f();
3708
3741
  }
3709
3742
  return 0; // continue
3710
- } else if (typeof type === 'string') {
3743
+ } else {
3711
3744
  // The case when we have an ObjectPattern with a @typedef
3712
3745
  name = "arguments[".concat(paramIndex, "]");
3713
3746
  }
@@ -3737,10 +3770,7 @@
3737
3770
  if (loc === 'ContactPoint#constructor' || loc === 'ContactResult#constructor' || loc === 'SingleContactResult#constructor') {
3738
3771
  prevCheck = 'arguments.length !== 0 && ';
3739
3772
  }
3740
- if (first) {
3741
- out += '\n';
3742
- first = false;
3743
- }
3773
+ newlineBeforeFirst();
3744
3774
  if (templates) {
3745
3775
  out += "".concat(spaces, "if (").concat(prevCheck, "!inspectTypeWithTemplates(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3746
3776
  } else {
package/index.mjs CHANGED
@@ -744,7 +744,7 @@ function simplifyType(type, optional) {
744
744
  */
745
745
  function parseJSDoc(src, expandType = expandTypeDepFree) {
746
746
  // Parse something like: @param {Object} [kwargs={}] Optional arguments.
747
- const regex = /@param \{(.*?)\} ([\[\]a-zA-Z0-9_$=\{\}\.'" ]+)/g;
747
+ const regex = /@param \{(.*?)\} ([\[\]a-zA-Z0-9_$=\-\{\}\.'" ]+)/g;
748
748
  const matches = [...src.matchAll(regex)];
749
749
  /** @type {Record<string, ExpandTypeReturnType>} */
750
750
  const params = Object.create(null);
@@ -756,10 +756,22 @@ function parseJSDoc(src, expandType = expandTypeDepFree) {
756
756
  // name: [kwargs={}] The configuration parameters.
757
757
  // name: [d = 1.0] Sample spacing
758
758
  if (name[0] === '[') {
759
- // Possible improvement: counting opening/closing brackets for perfect match
760
- const closer = name.lastIndexOf(']');
759
+ // Counting opening/closing brackets for perfect match
760
+ let openCloseCount = 1;
761
+ let i = 1;
762
+ for (; i < name.length; i++) {
763
+ const c = name[i];
764
+ if (c === '[') {
765
+ openCloseCount++;
766
+ } else if (c === ']') {
767
+ openCloseCount--;
768
+ }
769
+ if (openCloseCount === 0) {
770
+ break;
771
+ }
772
+ }
761
773
  // Afterwards name will be: d = 1.0
762
- name = name.substring(1, closer);
774
+ name = name.substring(1, i);
763
775
  // mark it for the type:
764
776
  optional = true;
765
777
  }
@@ -3301,6 +3313,12 @@ class Asserter extends Stringifier {
3301
3313
  const tmp = JSON.stringify(templates, null, 2).replaceAll('\n', '\n' + spaces);
3302
3314
  out += `\n${spaces}const rtiTemplates = ${tmp};`;
3303
3315
  }
3316
+ function newlineBeforeFirst() {
3317
+ if (first) {
3318
+ out += '\n';
3319
+ first = false;
3320
+ }
3321
+ }
3304
3322
  //out += `${spaces}/*${spaces} node.type=${node.type}\n${spaces}
3305
3323
  // ${JSON.stringify(jsdoc)}\n${parent}\n${spaces}*/\n`;
3306
3324
  for (let name in params) {
@@ -3338,11 +3356,19 @@ class Asserter extends Stringifier {
3338
3356
  if (param.left.type === 'ArrayPattern' && type.type === 'array') {
3339
3357
  // Add a type assertion for each element of the ArrayPattern
3340
3358
  for (const element of param.left.elements) {
3359
+ if (element.type === 'RestElement') {
3360
+ // Case for:
3361
+ // function test([a, b, ...rest] = [1, 2, 3]) {return rest;}
3362
+ /** @todo We could typecheck the rest element too */
3363
+ // console.log("Ignore RestElement", element);
3364
+ continue;
3365
+ }
3341
3366
  if (element.type !== 'Identifier') {
3342
3367
  this.warn('Only Identifier case handled right now');
3343
3368
  continue;
3344
3369
  }
3345
3370
  const _t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
3371
+ newlineBeforeFirst();
3346
3372
  if (templates) {
3347
3373
  out += `${spaces}if (!inspectTypeWithTemplates(${element.name}, ${_t}, '${_loc}', '${nameFancy}', rtiTemplates)) {\n`;
3348
3374
  } else {
@@ -3352,9 +3378,15 @@ class Asserter extends Stringifier {
3352
3378
  }
3353
3379
  continue;
3354
3380
  } else if (param.left.type === 'ObjectPattern') {
3355
- if (type.type === 'object') {
3381
+ // If we have properties from JSDoc, we can create individual type inspections.
3382
+ if (type != null && type.properties && Object.keys(type.properties).length) {
3356
3383
  // Add a type assertion for each property of the ObjectPattern
3357
3384
  for (const property of param.left.properties) {
3385
+ if (property.type !== 'ObjectProperty') {
3386
+ // E.g. case of RestElement, {a, b, c, ...kwargs}
3387
+ // console.log("Ignore property", property);
3388
+ continue;
3389
+ }
3358
3390
  if (property.key.type !== 'Identifier') {
3359
3391
  this.warn('ObjectPattern> Only Identifier case handled right now');
3360
3392
  continue;
@@ -3370,6 +3402,7 @@ class Asserter extends Stringifier {
3370
3402
  continue;
3371
3403
  }
3372
3404
  const _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3405
+ newlineBeforeFirst();
3373
3406
  if (templates) {
3374
3407
  out += `${spaces}if (!inspectTypeWithTemplates(${keyName}, ${_t2}, '${_loc}', '${nameFancy}', rtiTemplates)) {\n`;
3375
3408
  } else {
@@ -3378,7 +3411,7 @@ class Asserter extends Stringifier {
3378
3411
  out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
3379
3412
  }
3380
3413
  continue;
3381
- } else if (typeof type === 'string') {
3414
+ } else {
3382
3415
  // The case when we have an ObjectPattern with a @typedef
3383
3416
  name = `arguments[${paramIndex}]`;
3384
3417
  }
@@ -3408,10 +3441,7 @@ class Asserter extends Stringifier {
3408
3441
  if (_loc3 === 'ContactPoint#constructor' || _loc3 === 'ContactResult#constructor' || _loc3 === 'SingleContactResult#constructor') {
3409
3442
  prevCheck = 'arguments.length !== 0 && ';
3410
3443
  }
3411
- if (first) {
3412
- out += '\n';
3413
- first = false;
3414
- }
3444
+ newlineBeforeFirst();
3415
3445
  if (templates) {
3416
3446
  out += `${spaces}if (${prevCheck}!inspectTypeWithTemplates(${name}, ${t}, '${_loc3}', '${nameFancy}', rtiTemplates)) {\n`;
3417
3447
  } else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "bin": "bin.js",
3
3
  "name": "@runtime-type-inspector/transpiler",
4
- "version": "3.3.5",
4
+ "version": "3.3.7",
5
5
  "description": "Validating JSDoc types at runtime for high-quality types - Trust is good, control is better.",
6
6
  "main": "index.cjs",
7
7
  "module": "index.mjs",
@@ -19,7 +19,7 @@
19
19
  "homepage": "https://github.com/kungfooman/RuntimeTypeInspector.js#readme",
20
20
  "dependencies": {
21
21
  "@babel/parser": "^7.23.0",
22
- "@runtime-type-inspector/runtime": "^3.3.5",
22
+ "@runtime-type-inspector/runtime": "^3.3.7",
23
23
  "typescript": "^5.1.6"
24
24
  },
25
25
  "files": [