@runtime-type-inspector/transpiler 3.2.0 → 3.2.2

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 (4) hide show
  1. package/bin.js +11 -0
  2. package/index.cjs +45 -6
  3. package/index.mjs +33 -6
  4. package/package.json +3 -2
package/bin.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ import {addTypeChecks} from './index.mjs';
3
+ import {readFileSync} from "fs";
4
+ const filename = process.argv[2];
5
+ if (!filename) {
6
+ console.error('Please specify a filename as 2nd argument');
7
+ process.exit(1);
8
+ }
9
+ const content = readFileSync(filename, 'utf8');
10
+ const contentTypeChecked = addTypeChecks(content);
11
+ console.log(contentTypeChecked);
package/index.cjs CHANGED
@@ -267,7 +267,7 @@
267
267
  *
268
268
  * @todo Better handling of weird case: Array<>
269
269
  * @example
270
- * const {expandType} = await import("./src-transpiler/expandType.mjs");
270
+ * const {expandType} = await import("./src-transpiler/expandType.js");
271
271
  * expandType('[string, Array|AnyTypedArray, number[]]|[ONNXTensor]');
272
272
  * expandType('(123) '); // Outputs: '123'
273
273
  * expandType(' ( ( 123 ) ) '); // Outputs: '123'
@@ -1405,7 +1405,7 @@
1405
1405
  * Generates a string representing type checks for a given Babel AST node.
1406
1406
  *
1407
1407
  * Note: This method serves as a stub and should be overridden in subclasses.
1408
- * The actual implementation is expected to be provided in Asserter.mjs, where
1408
+ * The actual implementation is expected to be provided in Asserter.js, where
1409
1409
  * it would create runtime type assertions based on the AST node provided.
1410
1410
  *
1411
1411
  * @param {Node} node - The Babel AST node for which to generate type checks.
@@ -2944,7 +2944,7 @@
2944
2944
  /** @typedef {import('@babel/types').Node } Node */
2945
2945
  /** @typedef {import("@babel/types").ClassMethod } ClassMethod */
2946
2946
  /** @typedef {import("@babel/types").ClassPrivateMethod} ClassPrivateMethod */
2947
- /** @typedef {import('./stat.mjs').Stat } Stat */
2947
+ /** @typedef {import('./stat.js').Stat } Stat */
2948
2948
  /**
2949
2949
  * @typedef {object} Options
2950
2950
  * @property {boolean} [forceCurly] - Determines whether curly braces are enforced in Stringifier.
@@ -3025,6 +3025,8 @@
3025
3025
  });
3026
3026
  /** @type {Record<string, object>} */
3027
3027
  _defineProperty(_assertThisInitialized(_this), "typedefs", {});
3028
+ /** @type {string[]} */
3029
+ _defineProperty(_assertThisInitialized(_this), "addLaterImportNamespaceSpecifier", []);
3028
3030
  _this.forceCurly = forceCurly;
3029
3031
  _this.validateDivision = validateDivision;
3030
3032
  // @todo collect every type + manually validate as test set
@@ -3120,7 +3122,7 @@
3120
3122
  if (this.validateDivision) {
3121
3123
  header += ", validateDivision";
3122
3124
  }
3123
- header += ", registerTypedef, registerClass} from '@runtime-type-inspector/runtime';\n";
3125
+ header += ", registerTypedef, registerClass, registerImportNamespaceSpecifier} from '@runtime-type-inspector/runtime';\n";
3124
3126
  // Prevent tree-shaking in UMD build so we can always "add a breakpoint here".
3125
3127
  header += "export * from '@runtime-type-inspector/runtime';\n";
3126
3128
  return header;
@@ -3703,6 +3705,43 @@
3703
3705
  }
3704
3706
  return ret;
3705
3707
  }
3708
+ }, {
3709
+ key: "ImportDeclaration",
3710
+ value:
3711
+ /**
3712
+ * @override
3713
+ * @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
3714
+ * @returns {string} Stringification of the node.
3715
+ */
3716
+ function ImportDeclaration(node) {
3717
+ var out = _get(_getPrototypeOf(Asserter.prototype), "ImportDeclaration", this).call(this, node);
3718
+ var _iterator5 = _createForOfIteratorHelper(this.addLaterImportNamespaceSpecifier),
3719
+ _step5;
3720
+ try {
3721
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
3722
+ var name = _step5.value;
3723
+ out += "\nregisterImportNamespaceSpecifier('".concat(name, "', ").concat(name, ");");
3724
+ }
3725
+ } catch (err) {
3726
+ _iterator5.e(err);
3727
+ } finally {
3728
+ _iterator5.f();
3729
+ }
3730
+ return out;
3731
+ }
3732
+ /**
3733
+ * @override
3734
+ * @param {import("@babel/types").ImportNamespaceSpecifier} node - The Babel AST node.
3735
+ * @returns {string} Stringification of the node.
3736
+ */
3737
+ }, {
3738
+ key: "ImportNamespaceSpecifier",
3739
+ value: function ImportNamespaceSpecifier(node) {
3740
+ var local = node.local;
3741
+ var name = this.toSource(local);
3742
+ this.addLaterImportNamespaceSpecifier.push(name);
3743
+ return _get(_getPrototypeOf(Asserter.prototype), "ImportNamespaceSpecifier", this).call(this, node);
3744
+ }
3706
3745
  }]);
3707
3746
  return Asserter;
3708
3747
  }(Stringifier);
@@ -3718,7 +3757,7 @@
3718
3757
  *
3719
3758
  * @param {string} src - The input source code containing JSDoc comments to be processed
3720
3759
  * for type checks.
3721
- * @param {import('./Asserter.mjs').Options} [options] - Configuration options that dictate
3760
+ * @param {import('./Asserter.js').Options} [options] - Configuration options that dictate
3722
3761
  * how the processing is performed.
3723
3762
  * @returns {string} The transformed source code with inserted runtime type checks, or the
3724
3763
  * original source code commented with an error if processing fails.
@@ -3810,7 +3849,7 @@
3810
3849
  /**
3811
3850
  * @todo Better handling of weird case: Array<>
3812
3851
  * @example
3813
- * const {expandTypeBabelTS} = await import("./src-transpiler/expandTypeBabelTS.mjs");
3852
+ * const {expandTypeBabelTS} = await import("./src-transpiler/expandTypeBabelTS.js");
3814
3853
  * expandTypeBabelTS('[string, Array|AnyTypedArray, number[]]|[ONNXTensor]');
3815
3854
  * expandTypeBabelTS('(123) '); // Outputs: '123'
3816
3855
  * expandTypeBabelTS(' ( ( 123 ) ) '); // Outputs: '123'
package/index.mjs CHANGED
@@ -10,7 +10,7 @@ import ts from 'typescript';
10
10
  *
11
11
  * @todo Better handling of weird case: Array<>
12
12
  * @example
13
- * const {expandType} = await import("./src-transpiler/expandType.mjs");
13
+ * const {expandType} = await import("./src-transpiler/expandType.js");
14
14
  * expandType('[string, Array|AnyTypedArray, number[]]|[ONNXTensor]');
15
15
  * expandType('(123) '); // Outputs: '123'
16
16
  * expandType(' ( ( 123 ) ) '); // Outputs: '123'
@@ -1106,7 +1106,7 @@ class Stringifier {
1106
1106
  * Generates a string representing type checks for a given Babel AST node.
1107
1107
  *
1108
1108
  * Note: This method serves as a stub and should be overridden in subclasses.
1109
- * The actual implementation is expected to be provided in Asserter.mjs, where
1109
+ * The actual implementation is expected to be provided in Asserter.js, where
1110
1110
  * it would create runtime type assertions based on the AST node provided.
1111
1111
  *
1112
1112
  * @param {Node} node - The Babel AST node for which to generate type checks.
@@ -2631,7 +2631,7 @@ class Stringifier {
2631
2631
  /** @typedef {import('@babel/types').Node } Node */
2632
2632
  /** @typedef {import("@babel/types").ClassMethod } ClassMethod */
2633
2633
  /** @typedef {import("@babel/types").ClassPrivateMethod} ClassPrivateMethod */
2634
- /** @typedef {import('./stat.mjs').Stat } Stat */
2634
+ /** @typedef {import('./stat.js').Stat } Stat */
2635
2635
  /**
2636
2636
  * @typedef {object} Options
2637
2637
  * @property {boolean} [forceCurly] - Determines whether curly braces are enforced in Stringifier.
@@ -2703,6 +2703,8 @@ class Asserter extends Stringifier {
2703
2703
  };
2704
2704
  /** @type {Record<string, object>} */
2705
2705
  this.typedefs = {};
2706
+ /** @type {string[]} */
2707
+ this.addLaterImportNamespaceSpecifier = [];
2706
2708
  this.forceCurly = forceCurly;
2707
2709
  this.validateDivision = validateDivision;
2708
2710
  // @todo collect every type + manually validate as test set
@@ -2790,7 +2792,7 @@ class Asserter extends Stringifier {
2790
2792
  if (this.validateDivision) {
2791
2793
  header += ", validateDivision";
2792
2794
  }
2793
- header += ", registerTypedef, registerClass} from '@runtime-type-inspector/runtime';\n";
2795
+ header += ", registerTypedef, registerClass, registerImportNamespaceSpecifier} from '@runtime-type-inspector/runtime';\n";
2794
2796
  // Prevent tree-shaking in UMD build so we can always "add a breakpoint here".
2795
2797
  header += "export * from '@runtime-type-inspector/runtime';\n";
2796
2798
  return header;
@@ -3324,6 +3326,31 @@ class Asserter extends Stringifier {
3324
3326
  }
3325
3327
  return ret;
3326
3328
  }
3329
+ /**
3330
+ * @override
3331
+ * @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
3332
+ * @returns {string} Stringification of the node.
3333
+ */
3334
+ ImportDeclaration(node) {
3335
+ let out = super.ImportDeclaration(node);
3336
+ for (const name of this.addLaterImportNamespaceSpecifier) {
3337
+ out += `\nregisterImportNamespaceSpecifier('${name}', ${name});`;
3338
+ }
3339
+ return out;
3340
+ }
3341
+ /**
3342
+ * @override
3343
+ * @param {import("@babel/types").ImportNamespaceSpecifier} node - The Babel AST node.
3344
+ * @returns {string} Stringification of the node.
3345
+ */
3346
+ ImportNamespaceSpecifier(node) {
3347
+ const {
3348
+ local
3349
+ } = node;
3350
+ const name = this.toSource(local);
3351
+ this.addLaterImportNamespaceSpecifier.push(name);
3352
+ return super.ImportNamespaceSpecifier(node);
3353
+ }
3327
3354
  }
3328
3355
 
3329
3356
  /**
@@ -3337,7 +3364,7 @@ class Asserter extends Stringifier {
3337
3364
  *
3338
3365
  * @param {string} src - The input source code containing JSDoc comments to be processed
3339
3366
  * for type checks.
3340
- * @param {import('./Asserter.mjs').Options} [options] - Configuration options that dictate
3367
+ * @param {import('./Asserter.js').Options} [options] - Configuration options that dictate
3341
3368
  * how the processing is performed.
3342
3369
  * @returns {string} The transformed source code with inserted runtime type checks, or the
3343
3370
  * original source code commented with an error if processing fails.
@@ -3429,7 +3456,7 @@ function compareAST(left, right) {
3429
3456
  /**
3430
3457
  * @todo Better handling of weird case: Array<>
3431
3458
  * @example
3432
- * const {expandTypeBabelTS} = await import("./src-transpiler/expandTypeBabelTS.mjs");
3459
+ * const {expandTypeBabelTS} = await import("./src-transpiler/expandTypeBabelTS.js");
3433
3460
  * expandTypeBabelTS('[string, Array|AnyTypedArray, number[]]|[ONNXTensor]');
3434
3461
  * expandTypeBabelTS('(123) '); // Outputs: '123'
3435
3462
  * expandTypeBabelTS(' ( ( 123 ) ) '); // Outputs: '123'
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
+ "bin": "bin.js",
2
3
  "name": "@runtime-type-inspector/transpiler",
3
- "version": "3.2.0",
4
+ "version": "3.2.2",
4
5
  "description": "Validating JSDoc types at runtime for high-quality types - Trust is good, control is better.",
5
6
  "main": "index.cjs",
6
7
  "module": "index.mjs",
@@ -18,7 +19,7 @@
18
19
  "homepage": "https://github.com/kungfooman/RuntimeTypeInspector.js#readme",
19
20
  "dependencies": {
20
21
  "@babel/parser": "^7.23.0",
21
- "@runtime-type-inspector/runtime": "^3.2.0",
22
+ "@runtime-type-inspector/runtime": "^3.2.2",
22
23
  "typescript": "^5.1.6"
23
24
  },
24
25
  "files": [