@runtime-type-inspector/transpiler 5.0.0 → 5.0.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.
Files changed (3) hide show
  1. package/index.cjs +48 -0
  2. package/index.mjs +45 -0
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -3853,6 +3853,9 @@
3853
3853
  * @typedef {object} Options
3854
3854
  * @property {boolean} [forceCurly] - Determines whether curly braces are enforced in Stringifier.
3855
3855
  * @property {boolean} [validateDivision] - Indicates whether division operations should be validated.
3856
+ * @property {boolean} [inspectIndexedAccess] - Indicates whether indexed accesses
3857
+ * like `arr[i]` should be wrapped for bounds and integer validation. Disable
3858
+ * to drop indexed access inspection entirely. Defaults to true.
3856
3859
  * @property {import('./parseJSDoc.js').ExpandType} [expandType] - A function that expands shorthand types into full descriptions.
3857
3860
  * @property {string} [filename] - The name of a file to which the instance pertains.
3858
3861
  * @property {boolean} [addHeader] - Whether to add import declarations headers. Defaults to true.
@@ -3871,6 +3874,8 @@
3871
3874
  forceCurly = _ref$forceCurly === void 0 ? true : _ref$forceCurly,
3872
3875
  _ref$validateDivision = _ref.validateDivision,
3873
3876
  validateDivision = _ref$validateDivision === void 0 ? true : _ref$validateDivision,
3877
+ _ref$inspectIndexedAc = _ref.inspectIndexedAccess,
3878
+ inspectIndexedAccess = _ref$inspectIndexedAc === void 0 ? true : _ref$inspectIndexedAc,
3874
3879
  _ref$expandType = _ref.expandType,
3875
3880
  expandType = _ref$expandType === void 0 ? expandTypeDepFree : _ref$expandType,
3876
3881
  filename = _ref.filename,
@@ -3933,6 +3938,7 @@
3933
3938
  _defineProperty(_assertThisInitialized(_this), "addLaterImportNamespaceSpecifier", []);
3934
3939
  _this.forceCurly = forceCurly;
3935
3940
  _this.validateDivision = validateDivision;
3941
+ _this.inspectIndexedAccess = inspectIndexedAccess;
3936
3942
  // @todo collect every type + manually validate as test set
3937
3943
  // + implement expandType using Babel Flow type parser aswell
3938
3944
  _this.expandType = expandType;
@@ -4799,6 +4805,9 @@
4799
4805
  var computed = node.computed,
4800
4806
  object = node.object,
4801
4807
  property = node.property;
4808
+ if (!this.inspectIndexedAccess) {
4809
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4810
+ }
4802
4811
  if (!computed || object.type === 'Super') {
4803
4812
  return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4804
4813
  }
@@ -4815,6 +4824,45 @@
4815
4824
  if (parent.type === 'UnaryExpression' && parent.operator === 'delete' && parent.argument === node) {
4816
4825
  return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4817
4826
  }
4827
+ // Destructuring, rest and loop targets are assigned to, never read.
4828
+ if (parent.type === 'ArrayPattern') {
4829
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4830
+ }
4831
+ if (parent.type === 'RestElement' && parent.argument === node) {
4832
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4833
+ }
4834
+ if (parent.type === 'ObjectProperty' && parent.value === node) {
4835
+ // `({p: a[i]})` reads but `({p: a[i]} = ...)` writes: bypass inside patterns only.
4836
+ var grandparent = this.parents[this.parents.findLastIndex(function (_) {
4837
+ return _ === parent;
4838
+ }) - 1];
4839
+ if ((grandparent === null || grandparent === void 0 ? void 0 : grandparent.type) === 'ObjectPattern') {
4840
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4841
+ }
4842
+ }
4843
+ if (parent.type === 'AssignmentPattern' && parent.left === node) {
4844
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4845
+ }
4846
+ if ((parent.type === 'ForOfStatement' || parent.type === 'ForInStatement') && parent.left === node) {
4847
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4848
+ }
4849
+ // Method calls and tags carry their base as `this`; wrapping the callee
4850
+ // would silently rebind it to undefined.
4851
+ if ((parent.type === 'CallExpression' || parent.type === 'OptionalCallExpression') && parent.callee === node) {
4852
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4853
+ }
4854
+ if (parent.type === 'TaggedTemplateExpression' && parent.tag === node) {
4855
+ return _get(_getPrototypeOf(Asserter.prototype), "MemberExpression", this).call(this, node);
4856
+ }
4857
+ // `new a[i](...)` must stay `new (inspectIndexedAccess(...))(...)`:
4858
+ // without parens it parses as `(new inspectIndexedAccess(...))(...)`,
4859
+ // constructing the wrapper instead of the accessed value.
4860
+ if (parent.type === 'NewExpression' && parent.callee === node) {
4861
+ var _object_ = this.toSource(object);
4862
+ var _property_ = this.toSource(property);
4863
+ var _loc4 = this.getName(node);
4864
+ return "(inspectIndexedAccess(".concat(_object_, ", ").concat(_property_, ", ").concat(JSON.stringify(_loc4), "))");
4865
+ }
4818
4866
  }
4819
4867
  var object_ = this.toSource(object);
4820
4868
  var property_ = this.toSource(property);
package/index.mjs CHANGED
@@ -3516,6 +3516,9 @@ class Stringifier {
3516
3516
  * @typedef {object} Options
3517
3517
  * @property {boolean} [forceCurly] - Determines whether curly braces are enforced in Stringifier.
3518
3518
  * @property {boolean} [validateDivision] - Indicates whether division operations should be validated.
3519
+ * @property {boolean} [inspectIndexedAccess] - Indicates whether indexed accesses
3520
+ * like `arr[i]` should be wrapped for bounds and integer validation. Disable
3521
+ * to drop indexed access inspection entirely. Defaults to true.
3519
3522
  * @property {import('./parseJSDoc.js').ExpandType} [expandType] - A function that expands shorthand types into full descriptions.
3520
3523
  * @property {string} [filename] - The name of a file to which the instance pertains.
3521
3524
  * @property {boolean} [addHeader] - Whether to add import declarations headers. Defaults to true.
@@ -3528,6 +3531,7 @@ class Asserter extends Stringifier {
3528
3531
  constructor({
3529
3532
  forceCurly = true,
3530
3533
  validateDivision = true,
3534
+ inspectIndexedAccess = true,
3531
3535
  expandType = expandTypeDepFree,
3532
3536
  filename,
3533
3537
  addHeader = true,
@@ -3587,6 +3591,7 @@ class Asserter extends Stringifier {
3587
3591
  this.addLaterImportNamespaceSpecifier = [];
3588
3592
  this.forceCurly = forceCurly;
3589
3593
  this.validateDivision = validateDivision;
3594
+ this.inspectIndexedAccess = inspectIndexedAccess;
3590
3595
  // @todo collect every type + manually validate as test set
3591
3596
  // + implement expandType using Babel Flow type parser aswell
3592
3597
  this.expandType = expandType;
@@ -4393,6 +4398,9 @@ class Asserter extends Stringifier {
4393
4398
  object,
4394
4399
  property
4395
4400
  } = node;
4401
+ if (!this.inspectIndexedAccess) {
4402
+ return super.MemberExpression(node);
4403
+ }
4396
4404
  if (!computed || object.type === 'Super') {
4397
4405
  return super.MemberExpression(node);
4398
4406
  }
@@ -4409,6 +4417,43 @@ class Asserter extends Stringifier {
4409
4417
  if (parent.type === 'UnaryExpression' && parent.operator === 'delete' && parent.argument === node) {
4410
4418
  return super.MemberExpression(node);
4411
4419
  }
4420
+ // Destructuring, rest and loop targets are assigned to, never read.
4421
+ if (parent.type === 'ArrayPattern') {
4422
+ return super.MemberExpression(node);
4423
+ }
4424
+ if (parent.type === 'RestElement' && parent.argument === node) {
4425
+ return super.MemberExpression(node);
4426
+ }
4427
+ if (parent.type === 'ObjectProperty' && parent.value === node) {
4428
+ // `({p: a[i]})` reads but `({p: a[i]} = ...)` writes: bypass inside patterns only.
4429
+ const grandparent = this.parents[this.parents.findLastIndex(_ => _ === parent) - 1];
4430
+ if ((grandparent == null ? void 0 : grandparent.type) === 'ObjectPattern') {
4431
+ return super.MemberExpression(node);
4432
+ }
4433
+ }
4434
+ if (parent.type === 'AssignmentPattern' && parent.left === node) {
4435
+ return super.MemberExpression(node);
4436
+ }
4437
+ if ((parent.type === 'ForOfStatement' || parent.type === 'ForInStatement') && parent.left === node) {
4438
+ return super.MemberExpression(node);
4439
+ }
4440
+ // Method calls and tags carry their base as `this`; wrapping the callee
4441
+ // would silently rebind it to undefined.
4442
+ if ((parent.type === 'CallExpression' || parent.type === 'OptionalCallExpression') && parent.callee === node) {
4443
+ return super.MemberExpression(node);
4444
+ }
4445
+ if (parent.type === 'TaggedTemplateExpression' && parent.tag === node) {
4446
+ return super.MemberExpression(node);
4447
+ }
4448
+ // `new a[i](...)` must stay `new (inspectIndexedAccess(...))(...)`:
4449
+ // without parens it parses as `(new inspectIndexedAccess(...))(...)`,
4450
+ // constructing the wrapper instead of the accessed value.
4451
+ if (parent.type === 'NewExpression' && parent.callee === node) {
4452
+ const _object_ = this.toSource(object);
4453
+ const _property_ = this.toSource(property);
4454
+ const _loc5 = this.getName(node);
4455
+ return `(inspectIndexedAccess(${_object_}, ${_property_}, ${JSON.stringify(_loc5)}))`;
4456
+ }
4412
4457
  }
4413
4458
  const object_ = this.toSource(object);
4414
4459
  const property_ = this.toSource(property);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "ts2js": "ts2js.js"
5
5
  },
6
6
  "name": "@runtime-type-inspector/transpiler",
7
- "version": "5.0.0",
7
+ "version": "5.0.1",
8
8
  "description": "Validating JSDoc types at runtime for high-quality types - Trust is good, control is better.",
9
9
  "main": "index.cjs",
10
10
  "module": "index.mjs",
@@ -22,7 +22,7 @@
22
22
  "homepage": "https://github.com/kungfooman/RuntimeTypeInspector.js#readme",
23
23
  "dependencies": {
24
24
  "@babel/parser": "^7.23.0",
25
- "@runtime-type-inspector/runtime": "^5.0.0",
25
+ "@runtime-type-inspector/runtime": "^5.0.1",
26
26
  "typescript": "^5.1.6"
27
27
  },
28
28
  "files": [