@runtime-type-inspector/transpiler 3.3.4 → 3.3.6

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 +67 -42
  2. package/index.mjs +59 -34
  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);
@@ -3610,10 +3610,18 @@
3610
3610
  var tmp = JSON.stringify(templates, null, 2).replaceAll('\n', '\n' + spaces);
3611
3611
  out += "\n".concat(spaces, "const rtiTemplates = ").concat(tmp, ";");
3612
3612
  }
3613
+ function newlineBeforeFirst() {
3614
+ if (first) {
3615
+ out += '\n';
3616
+ first = false;
3617
+ }
3618
+ }
3613
3619
  //out += `${spaces}/*${spaces} node.type=${node.type}\n${spaces}
3614
3620
  // ${JSON.stringify(jsdoc)}\n${parent}\n${spaces}*/\n`;
3615
3621
  var _loop = function _loop(name) {
3616
3622
  var type = params[name];
3623
+ // Copy name for warnings, `name` may become `arguments[${paramIndex}]`.
3624
+ var nameFancy = name;
3617
3625
  var hasParam = _this2.nodeHasParamName(node, name);
3618
3626
  if (!hasParam) {
3619
3627
  var testNode = node;
@@ -3651,15 +3659,23 @@
3651
3659
  try {
3652
3660
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
3653
3661
  var element = _step.value;
3662
+ if (element.type === 'RestElement') {
3663
+ // Case for:
3664
+ // function test([a, b, ...rest] = [1, 2, 3]) {return rest;}
3665
+ /** @todo We could typecheck the rest element too */
3666
+ // console.log("Ignore RestElement", element);
3667
+ continue;
3668
+ }
3654
3669
  if (element.type !== 'Identifier') {
3655
3670
  _this2.warn('Only Identifier case handled right now');
3656
3671
  continue;
3657
3672
  }
3658
3673
  var _t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
3674
+ newlineBeforeFirst();
3659
3675
  if (templates) {
3660
- out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(name, "', rtiTemplates)) {\n");
3676
+ out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3661
3677
  } else {
3662
- out += "".concat(spaces, "if (!inspectType(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(name, "')) {\n");
3678
+ out += "".concat(spaces, "if (!inspectType(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(nameFancy, "')) {\n");
3663
3679
  }
3664
3680
  out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
3665
3681
  }
@@ -3669,44 +3685,56 @@
3669
3685
  _iterator.f();
3670
3686
  }
3671
3687
  return 0; // continue
3672
- } else if (param.left.type === 'ObjectPattern' && type.type === 'object') {
3673
- // Add a type assertion for each property of the ObjectPattern
3674
- var _iterator2 = _createForOfIteratorHelper(param.left.properties),
3675
- _step2;
3676
- try {
3677
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3678
- var property = _step2.value;
3679
- if (property.key.type !== 'Identifier') {
3680
- _this2.warn('ObjectPattern> Only Identifier case handled right now');
3681
- continue;
3688
+ } else if (param.left.type === 'ObjectPattern') {
3689
+ if (type.type === 'object') {
3690
+ // Add a type assertion for each property of the ObjectPattern
3691
+ var _iterator2 = _createForOfIteratorHelper(param.left.properties),
3692
+ _step2;
3693
+ try {
3694
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3695
+ var property = _step2.value;
3696
+ if (property.type !== 'ObjectProperty') {
3697
+ // E.g. case of RestElement, {a, b, c, ...kwargs}
3698
+ // console.log("Ignore property", property);
3699
+ continue;
3700
+ }
3701
+ if (property.key.type !== 'Identifier') {
3702
+ _this2.warn('ObjectPattern> Only Identifier case handled right now');
3703
+ continue;
3704
+ }
3705
+ var keyName = property.key.name;
3706
+ if (type.type !== 'object' || !type.properties) {
3707
+ _this2.warn("missing subtype information in JSDoc> in type", JSON.stringify(type, null, 2), "for ObjectPattern:", _this2.toSource(property).trim());
3708
+ continue;
3709
+ }
3710
+ var subType = type.properties[keyName];
3711
+ if (!subType) {
3712
+ _this2.warn("missing subtype information in JSDoc");
3713
+ continue;
3714
+ }
3715
+ var _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3716
+ newlineBeforeFirst();
3717
+ if (templates) {
3718
+ out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3719
+ } else {
3720
+ out += "".concat(spaces, "if (!inspectType(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(nameFancy, "')) {\n");
3721
+ }
3722
+ out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
3682
3723
  }
3683
- var keyName = property.key.name;
3684
- if (type.type !== 'object' || !type.properties) {
3685
- _this2.warn("missing subtype information in JSDoc> in type", JSON.stringify(type, null, 2), "for ObjectPattern:", _this2.toSource(property).trim());
3686
- continue;
3687
- }
3688
- var subType = type.properties[keyName];
3689
- if (!subType) {
3690
- _this2.warn("missing subtype information in JSDoc");
3691
- continue;
3692
- }
3693
- var _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3694
- if (templates) {
3695
- out += "".concat(spaces, "if (!inspectTypeWithTemplates(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(name, "', rtiTemplates)) {\n");
3696
- } else {
3697
- out += "".concat(spaces, "if (!inspectType(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(name, "')) {\n");
3698
- }
3699
- out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
3724
+ } catch (err) {
3725
+ _iterator2.e(err);
3726
+ } finally {
3727
+ _iterator2.f();
3700
3728
  }
3701
- } catch (err) {
3702
- _iterator2.e(err);
3703
- } finally {
3704
- _iterator2.f();
3729
+ return 0; // continue
3730
+ } else if (typeof type === 'string') {
3731
+ // The case when we have an ObjectPattern with a @typedef
3732
+ name = "arguments[".concat(paramIndex, "]");
3705
3733
  }
3734
+ } else {
3735
+ _this2.warn("generateTypeChecks> ".concat(_loc, "> todo implement"), "AssignmentPattern for parameter ".concat(name));
3706
3736
  return 0; // continue
3707
3737
  }
3708
- _this2.warn("generateTypeChecks> ".concat(_loc, "> todo implement"), "AssignmentPattern for parameter ".concat(name));
3709
- return 0; // continue
3710
3738
  }
3711
3739
  } else {
3712
3740
  var _loc2 = _this2.getName(node);
@@ -3729,14 +3757,11 @@
3729
3757
  if (loc === 'ContactPoint#constructor' || loc === 'ContactResult#constructor' || loc === 'SingleContactResult#constructor') {
3730
3758
  prevCheck = 'arguments.length !== 0 && ';
3731
3759
  }
3732
- if (first) {
3733
- out += '\n';
3734
- first = false;
3735
- }
3760
+ newlineBeforeFirst();
3736
3761
  if (templates) {
3737
- out += "".concat(spaces, "if (").concat(prevCheck, "!inspectTypeWithTemplates(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(name, "', rtiTemplates)) {\n");
3762
+ out += "".concat(spaces, "if (").concat(prevCheck, "!inspectTypeWithTemplates(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(nameFancy, "', rtiTemplates)) {\n");
3738
3763
  } else {
3739
- out += "".concat(spaces, "if (").concat(prevCheck, "!inspectType(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(name, "')) {\n");
3764
+ out += "".concat(spaces, "if (").concat(prevCheck, "!inspectType(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(nameFancy, "')) {\n");
3740
3765
  }
3741
3766
  out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
3742
3767
  },
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);
@@ -3301,10 +3301,18 @@ class Asserter extends Stringifier {
3301
3301
  const tmp = JSON.stringify(templates, null, 2).replaceAll('\n', '\n' + spaces);
3302
3302
  out += `\n${spaces}const rtiTemplates = ${tmp};`;
3303
3303
  }
3304
+ function newlineBeforeFirst() {
3305
+ if (first) {
3306
+ out += '\n';
3307
+ first = false;
3308
+ }
3309
+ }
3304
3310
  //out += `${spaces}/*${spaces} node.type=${node.type}\n${spaces}
3305
3311
  // ${JSON.stringify(jsdoc)}\n${parent}\n${spaces}*/\n`;
3306
3312
  for (let name in params) {
3307
3313
  const type = params[name];
3314
+ // Copy name for warnings, `name` may become `arguments[${paramIndex}]`.
3315
+ const nameFancy = name;
3308
3316
  const hasParam = this.nodeHasParamName(node, name);
3309
3317
  if (!hasParam) {
3310
3318
  let testNode = node;
@@ -3336,48 +3344,68 @@ class Asserter extends Stringifier {
3336
3344
  if (param.left.type === 'ArrayPattern' && type.type === 'array') {
3337
3345
  // Add a type assertion for each element of the ArrayPattern
3338
3346
  for (const element of param.left.elements) {
3347
+ if (element.type === 'RestElement') {
3348
+ // Case for:
3349
+ // function test([a, b, ...rest] = [1, 2, 3]) {return rest;}
3350
+ /** @todo We could typecheck the rest element too */
3351
+ // console.log("Ignore RestElement", element);
3352
+ continue;
3353
+ }
3339
3354
  if (element.type !== 'Identifier') {
3340
3355
  this.warn('Only Identifier case handled right now');
3341
3356
  continue;
3342
3357
  }
3343
3358
  const _t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
3359
+ newlineBeforeFirst();
3344
3360
  if (templates) {
3345
- out += `${spaces}if (!inspectTypeWithTemplates(${element.name}, ${_t}, '${_loc}', '${name}', rtiTemplates)) {\n`;
3361
+ out += `${spaces}if (!inspectTypeWithTemplates(${element.name}, ${_t}, '${_loc}', '${nameFancy}', rtiTemplates)) {\n`;
3346
3362
  } else {
3347
- out += `${spaces}if (!inspectType(${element.name}, ${_t}, '${_loc}', '${name}')) {\n`;
3363
+ out += `${spaces}if (!inspectType(${element.name}, ${_t}, '${_loc}', '${nameFancy}')) {\n`;
3348
3364
  }
3349
3365
  out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
3350
3366
  }
3351
3367
  continue;
3352
- } else if (param.left.type === 'ObjectPattern' && type.type === 'object') {
3353
- // Add a type assertion for each property of the ObjectPattern
3354
- for (const property of param.left.properties) {
3355
- if (property.key.type !== 'Identifier') {
3356
- this.warn('ObjectPattern> Only Identifier case handled right now');
3357
- continue;
3358
- }
3359
- const keyName = property.key.name;
3360
- if (type.type !== 'object' || !type.properties) {
3361
- this.warn("missing subtype information in JSDoc> in type", JSON.stringify(type, null, 2), "for ObjectPattern:", this.toSource(property).trim());
3362
- continue;
3368
+ } else if (param.left.type === 'ObjectPattern') {
3369
+ if (type.type === 'object') {
3370
+ // Add a type assertion for each property of the ObjectPattern
3371
+ for (const property of param.left.properties) {
3372
+ if (property.type !== 'ObjectProperty') {
3373
+ // E.g. case of RestElement, {a, b, c, ...kwargs}
3374
+ // console.log("Ignore property", property);
3375
+ continue;
3376
+ }
3377
+ if (property.key.type !== 'Identifier') {
3378
+ this.warn('ObjectPattern> Only Identifier case handled right now');
3379
+ continue;
3380
+ }
3381
+ const keyName = property.key.name;
3382
+ if (type.type !== 'object' || !type.properties) {
3383
+ this.warn("missing subtype information in JSDoc> in type", JSON.stringify(type, null, 2), "for ObjectPattern:", this.toSource(property).trim());
3384
+ continue;
3385
+ }
3386
+ const subType = type.properties[keyName];
3387
+ if (!subType) {
3388
+ this.warn("missing subtype information in JSDoc");
3389
+ continue;
3390
+ }
3391
+ const _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3392
+ newlineBeforeFirst();
3393
+ if (templates) {
3394
+ out += `${spaces}if (!inspectTypeWithTemplates(${keyName}, ${_t2}, '${_loc}', '${nameFancy}', rtiTemplates)) {\n`;
3395
+ } else {
3396
+ out += `${spaces}if (!inspectType(${keyName}, ${_t2}, '${_loc}', '${nameFancy}')) {\n`;
3397
+ }
3398
+ out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
3363
3399
  }
3364
- const subType = type.properties[keyName];
3365
- if (!subType) {
3366
- this.warn("missing subtype information in JSDoc");
3367
- continue;
3368
- }
3369
- const _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
3370
- if (templates) {
3371
- out += `${spaces}if (!inspectTypeWithTemplates(${keyName}, ${_t2}, '${_loc}', '${name}', rtiTemplates)) {\n`;
3372
- } else {
3373
- out += `${spaces}if (!inspectType(${keyName}, ${_t2}, '${_loc}', '${name}')) {\n`;
3374
- }
3375
- out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
3400
+ continue;
3401
+ } else if (typeof type === 'string') {
3402
+ // The case when we have an ObjectPattern with a @typedef
3403
+ name = `arguments[${paramIndex}]`;
3376
3404
  }
3405
+ } else {
3406
+ this.warn(`generateTypeChecks> ${_loc}> todo implement`, `AssignmentPattern for parameter ${name}`);
3377
3407
  continue;
3378
3408
  }
3379
- this.warn(`generateTypeChecks> ${_loc}> todo implement`, `AssignmentPattern for parameter ${name}`);
3380
- continue;
3381
3409
  }
3382
3410
  } else {
3383
3411
  const _loc2 = this.getName(node);
@@ -3400,14 +3428,11 @@ class Asserter extends Stringifier {
3400
3428
  if (_loc3 === 'ContactPoint#constructor' || _loc3 === 'ContactResult#constructor' || _loc3 === 'SingleContactResult#constructor') {
3401
3429
  prevCheck = 'arguments.length !== 0 && ';
3402
3430
  }
3403
- if (first) {
3404
- out += '\n';
3405
- first = false;
3406
- }
3431
+ newlineBeforeFirst();
3407
3432
  if (templates) {
3408
- out += `${spaces}if (${prevCheck}!inspectTypeWithTemplates(${name}, ${t}, '${_loc3}', '${name}', rtiTemplates)) {\n`;
3433
+ out += `${spaces}if (${prevCheck}!inspectTypeWithTemplates(${name}, ${t}, '${_loc3}', '${nameFancy}', rtiTemplates)) {\n`;
3409
3434
  } else {
3410
- out += `${spaces}if (${prevCheck}!inspectType(${name}, ${t}, '${_loc3}', '${name}')) {\n`;
3435
+ out += `${spaces}if (${prevCheck}!inspectType(${name}, ${t}, '${_loc3}', '${nameFancy}')) {\n`;
3411
3436
  }
3412
3437
  out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
3413
3438
  }
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.4",
4
+ "version": "3.3.6",
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.4",
22
+ "@runtime-type-inspector/runtime": "^3.3.6",
23
23
  "typescript": "^5.1.6"
24
24
  },
25
25
  "files": [