@runtime-type-inspector/repl 3.3.7 → 4.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.
@@ -0,0 +1,4 @@
1
+ const generate = (await import("@babel/generator")).default;
2
+ const ast = parse(getLeft(), {sourceType: "module"});
3
+ setRight(generate(ast).code);
4
+
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @param {string} content - The content.
3
+ * @returns {string} data:text string
4
+ */
5
+ function importFile(content) {
6
+ return "data:text/javascript;base64," + btoa(content);
7
+ }
8
+ const nodeModules = location.href + './node_modules/';
9
+ // For React ESM importmap to work you need React ESM: npm i react-es6
10
+ const react = {
11
+ 'prop-types' : nodeModules + 'react-es6/prop-types/index.js',
12
+ 'tiny-warning' : nodeModules + 'react-es6/tiny-warning.mjs',
13
+ 'tiny-invariant' : nodeModules + 'react-es6/tiny-invariant.mjs',
14
+ 'mini-create-react-context': nodeModules + 'react-router/node_modules/mini-create-react-context/dist/esm/index.js',
15
+ 'path-to-regexp' : nodeModules + 'react-es6/path-to-regexp.mjs',
16
+ 'react-is' : nodeModules + 'react-es6/react-is.mjs',
17
+ 'hoist-non-react-statics' : nodeModules + 'react-es6/hoist-non-react-statics.mjs',
18
+ 'resolve-pathname' : nodeModules + 'react-es6/resolve-pathname.mjs',
19
+ 'isarray' : nodeModules + 'react-es6/isarray.mjs',
20
+ };
21
+ const reactMin = {
22
+ ...react,
23
+ 'react' : nodeModules + 'react-es6/build/react.min.mjs',
24
+ 'react-dom' : nodeModules + 'react-es6/build/react-dom.min.mjs',
25
+ 'react-dom/client': nodeModules + 'react-es6/build/react-dom-client.min.mjs',
26
+ };
27
+ window.process = {
28
+ env: {
29
+ BABEL_TYPES_8_BREAKING: true,
30
+ }
31
+ };
32
+ const imports = {
33
+ "@runtime-type-inspector/runtime" : '../src-runtime/index.js',
34
+ "@runtime-type-inspector/transpiler": '../src-transpiler/index.js',
35
+ "@babel/parser" : nodeModules + '@babel/parser/lib/index.js',
36
+ // All needed for generator:
37
+ "@babel/generator" : nodeModules + '@babel/generator/lib/index.js',
38
+ "@babel/types" : nodeModules + '@babel/types/lib/index.js',
39
+ "@babel/helper-validator-identifier": nodeModules + '@babel/helper-validator-identifier/lib/index.js',
40
+ "@babel/helper-string-parser" : nodeModules + '@babel/helper-string-parser/lib/index.js',
41
+ "@jridgewell/gen-mapping" : importFile(`
42
+ export function GenMapping() {
43
+ throw Error("GenMapping not implemented");
44
+ }
45
+ export function toEncodedMap() {
46
+ throw Error("toEncodedMap not implemented");
47
+ }
48
+ export function setSourceContent() {
49
+ throw Error("setSourceContent not implemented");
50
+ }
51
+ export function toDecodedMap() {
52
+ throw Error("toDecodedMap not implemented");
53
+ }
54
+ export function allMappings() {
55
+ throw Error("allMappings not implemented");
56
+ }
57
+ export function maybeAddMapping() {
58
+ throw Error("maybeAddMapping not implemented");
59
+ }
60
+ export default {}`),
61
+ "@jridgewell/trace-mapping" : importFile(`
62
+ export function originalPositionFor() {}
63
+ export function TraceMap() {}
64
+ export default {}
65
+ `),
66
+ "jsesc" : importFile('export default {}'),
67
+ "process" : importFile("export default {};"),
68
+ // </for generator>
69
+ "display-anything" : nodeModules + 'display-anything/src/index.js',
70
+ "worker-with-import-map" : nodeModules + 'worker-with-import-map/src/index.js',
71
+ "test-import-validation-b" : '../test/typechecking/import-validation/b.js',
72
+ //"@babel/helper-plugin-utils" : './babel-helper-plugin-utils.js',
73
+ //"@babel/plugin-syntax-typescript" : './babel-plugin-syntax-typescript.js',
74
+ "fs" : importFile("export default {};"),
75
+ "typescript" : importFile(`
76
+ globalThis.module = {exports: {}};
77
+ await import("${nodeModules}typescript/lib/typescript.js");
78
+ export default globalThis.module.exports;
79
+ `),
80
+ "ace-builds": importFile(`
81
+ await import("${nodeModules}ace-builds/src/ace.js");
82
+ window.ace.config.set('basePath', "${nodeModules}ace-builds/src");
83
+ const {ace} = window;
84
+ export {ace};
85
+ `),
86
+ ...reactMin,
87
+ };
88
+ if (location.host.includes('runtimetypeinspector.org') || location.port === '7000') {
89
+ imports['@runtime-type-inspector/runtime' ] = './node_modules/@runtime-type-inspector/runtime/index.mjs';
90
+ imports['@runtime-type-inspector/transpiler'] = './node_modules/@runtime-type-inspector/transpiler/index.mjs';
91
+ }
92
+ const importmap = document.createElement("script");
93
+ importmap.type = "importmap";
94
+ importmap.textContent = JSON.stringify({imports});
95
+ const dom = document.body || document.head;
96
+ if (!dom) {
97
+ throw new Error("neither <body> nor <head> available to append importmap");
98
+ }
99
+ dom.append(importmap);
package/importmap.js CHANGED
@@ -5,16 +5,47 @@
5
5
  function importFile(content) {
6
6
  return "data:text/javascript;base64," + btoa(content);
7
7
  }
8
+ const nodeModules = location.href + './node_modules/';
9
+ // For React ESM importmap to work you need React ESM: npm i react-es6
10
+ const react = {
11
+ 'prop-types' : nodeModules + 'react-es6/prop-types/index.js',
12
+ 'tiny-warning' : nodeModules + 'react-es6/tiny-warning.mjs',
13
+ 'tiny-invariant' : nodeModules + 'react-es6/tiny-invariant.mjs',
14
+ 'mini-create-react-context': nodeModules + 'react-router/node_modules/mini-create-react-context/dist/esm/index.js',
15
+ 'path-to-regexp' : nodeModules + 'react-es6/path-to-regexp.mjs',
16
+ 'react-is' : nodeModules + 'react-es6/react-is.mjs',
17
+ 'hoist-non-react-statics' : nodeModules + 'react-es6/hoist-non-react-statics.mjs',
18
+ 'resolve-pathname' : nodeModules + 'react-es6/resolve-pathname.mjs',
19
+ 'isarray' : nodeModules + 'react-es6/isarray.mjs',
20
+ };
21
+ const reactMin = {
22
+ ...react,
23
+ 'react' : nodeModules + 'react-es6/build/react.min.mjs',
24
+ 'react-dom' : nodeModules + 'react-es6/build/react-dom.min.mjs',
25
+ 'react-dom/client': nodeModules + 'react-es6/build/react-dom-client.min.mjs',
26
+ };
8
27
  const imports = {
9
28
  "@runtime-type-inspector/runtime" : '../src-runtime/index.js',
10
29
  "@runtime-type-inspector/transpiler": '../src-transpiler/index.js',
11
- "@babel/parser" : "./babel-parser.js",
12
- "display-anything" : "./node_modules/display-anything/src/index.js",
13
- "test-import-validation-b" : "../test/typechecking/import-validation/b.js",
14
- //"@babel/helper-plugin-utils" : "./babel-helper-plugin-utils.js",
15
- //"@babel/plugin-syntax-typescript" : "./babel-plugin-syntax-typescript.js",
30
+ "@babel/parser" : nodeModules + '@babel/parser/lib/index.js',
31
+ "display-anything" : nodeModules + 'display-anything/src/index.js',
32
+ "worker-with-import-map" : nodeModules + 'worker-with-import-map/src/index.js',
33
+ "test-import-validation-b" : '../test/typechecking/import-validation/b.js',
34
+ //"@babel/helper-plugin-utils" : './babel-helper-plugin-utils.js',
35
+ //"@babel/plugin-syntax-typescript" : './babel-plugin-syntax-typescript.js',
16
36
  "fs" : importFile("export default {};"),
17
- "typescript" : importFile("export default ts;"), // UMD import
37
+ "typescript" : importFile(`
38
+ globalThis.module = {exports: {}};
39
+ await import("${nodeModules}typescript/lib/typescript.js");
40
+ export default globalThis.module.exports;
41
+ `),
42
+ "ace-builds": importFile(`
43
+ await import("${nodeModules}ace-builds/src/ace.js");
44
+ window.ace.config.set('basePath', "${nodeModules}ace-builds/src");
45
+ const {ace} = window;
46
+ export {ace};
47
+ `),
48
+ ...reactMin,
18
49
  };
19
50
  if (location.host.includes('runtimetypeinspector.org') || location.port === '7000') {
20
51
  imports['@runtime-type-inspector/runtime' ] = './node_modules/@runtime-type-inspector/runtime/index.mjs';
package/index.html CHANGED
@@ -1,8 +1,6 @@
1
1
  <html>
2
2
  <head>
3
3
  <script src="importmap.js"></script>
4
- <script src="node_modules/ace-builds/src/ace.js"></script>
5
- <script src="node_modules/typescript/lib/typescript.js"></script>
6
4
  <link rel="stylesheet" href="./node_modules/display-anything/src/DisplayAnything.css"></link>
7
5
  </head>
8
6
  <body>
package/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import {parse} from '@babel/parser';
2
+ import {ace} from 'ace-builds';
3
+ import ts from 'typescript';
2
4
  import {
3
5
  parseJSDoc,
4
6
  addTypeChecks,
@@ -6,7 +8,8 @@ import {
6
8
  expandTypeBabelTS,
7
9
  expandTypeDepFree,
8
10
  code2ast2code,
9
- ast2jsonForComparison
11
+ ast2jsonForComparison,
12
+ parserOptions,
10
13
  } from '@runtime-type-inspector/transpiler';
11
14
  import * as ti from '@runtime-type-inspector/transpiler';
12
15
  import * as rti from '@runtime-type-inspector/runtime';
@@ -169,6 +172,7 @@ function activateREPLRuntime() {
169
172
  buttonREPL.onclick = activateREPL;
170
173
  Object.assign(window, {
171
174
  parse,
175
+ ts,
172
176
  ti, ...ti,
173
177
  rti, ...rti,
174
178
  });
@@ -205,7 +209,7 @@ function getRight() {
205
209
  return aceEditorRight.getValue();
206
210
  }
207
211
  function actionAST() {
208
- const ast = parse(getLeft(), {sourceType: 'module'});
212
+ const ast = parse(getLeft(), parserOptions);
209
213
  const out = JSON.stringify(ast, function (name, val) {
210
214
  if (name === "loc" || name === "start" || name === "end") {
211
215
  return undefined; // remove
@@ -438,6 +442,7 @@ const aceEditorLeft = setupAce(
438
442
  // write result to right editor
439
443
  // Tip: open F12/DevTools to see errors and warnings
440
444
  // Press Shift-Enter in right editor to eval result.
445
+ import {TypePanel} from '@runtime-type-inspector/runtime';
441
446
  /**
442
447
  * @param {number} a
443
448
  * @param {number} b
@@ -450,6 +455,8 @@ const arr = [10_20];
450
455
  const [a, b] = arr;
451
456
  const ret = add(a, b);
452
457
  console.log("ret", ret);
458
+ const typePanel = new TypePanel();
459
+ Object.assign(window, {add, typePanel});
453
460
  `,
454
461
  //editor => insertTypes(),
455
462
  editor => runAction(),
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@runtime-type-inspector/repl",
3
+ "version": "4.0.0",
4
+ "description": "REPL of RuntimeTypeInspector.js",
5
+ "main": "index.html",
6
+ "author": "Hermann Rolfes (kungfooman)",
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "dependencies": {
12
+ "@babel/parser": "^8.0.0-alpha.12",
13
+ "@babel/generator": "^8.0.0-alpha.12",
14
+ "@runtime-type-inspector/transpiler": "^4.0.0",
15
+ "ace-builds": "^1.35.2",
16
+ "display-anything": "^1.1.0",
17
+ "express": "^4.18.2",
18
+ "react-es6": "^1.0.0",
19
+ "worker-with-import-map": "^1.0.4"
20
+ }
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtime-type-inspector/repl",
3
- "version": "3.3.7",
3
+ "version": "4.0.1",
4
4
  "description": "REPL of RuntimeTypeInspector.js",
5
5
  "main": "index.html",
6
6
  "author": "Hermann Rolfes (kungfooman)",
@@ -9,9 +9,12 @@
9
9
  "access": "public"
10
10
  },
11
11
  "dependencies": {
12
- "@runtime-type-inspector/transpiler": "^3.3.7",
13
- "ace-builds": "^1.28.0",
12
+ "@babel/parser": "^8.0.0-alpha.12",
13
+ "@runtime-type-inspector/transpiler": "^4.0.1",
14
+ "ace-builds": "^1.35.2",
14
15
  "display-anything": "^1.1.0",
15
- "express": "^4.18.2"
16
+ "express": "^4.18.2",
17
+ "react-es6": "^1.0.0",
18
+ "worker-with-import-map": "^1.0.4"
16
19
  }
17
20
  }
@@ -0,0 +1,23 @@
1
+ import {inspectType, youCanAddABreakpointHere} from '@runtime-type-inspector/runtime';
2
+ /**
3
+ * @param {number} a
4
+ * @param {number} b
5
+ */
6
+ function add(a, b) {
7
+ if (!inspectType(a, "number", 'add', 'a')) {
8
+ youCanAddABreakpointHere();
9
+ }
10
+ if (!inspectType(b, "number", 'add', 'b')) {
11
+ youCanAddABreakpointHere();
12
+ }
13
+ return a + b;
14
+ }
15
+ function f() {
16
+ /** @type {number[]} */
17
+ const arr = [10_20];
18
+ const [a, b] = arr;
19
+ const ret = add(a, b);
20
+ self.postMessage(`ret ${ret}`);
21
+ }
22
+ f();
23
+ setInterval(f, 2000); // Test spam mode
@@ -0,0 +1,48 @@
1
+ import {inspectType, youCanAddABreakpointHere, validateDivision} from '@runtime-type-inspector/runtime';
2
+ /**
3
+ * @param {number} a
4
+ * @param {number} b
5
+ */
6
+ function testDivide(a, b) {
7
+ if (!inspectType(a, "number", 'testDivide', 'a')) {
8
+ youCanAddABreakpointHere();
9
+ }
10
+ if (!inspectType(b, "number", 'testDivide', 'b')) {
11
+ youCanAddABreakpointHere();
12
+ }
13
+ return validateDivision(a, b, "testDivide");
14
+ }
15
+ function f() {
16
+ /** @type {number[]} */
17
+ const arr = [10_20];
18
+ const [a, b] = arr;
19
+ const ret = testDivide(a, b);
20
+ self.postMessage(`ret ${ret}`);
21
+ }
22
+ f();
23
+ setInterval(f, 2000); // Test spam mode
24
+
25
+ /*
26
+ import {TypePanel} from '@runtime-type-inspector/runtime';
27
+ import {WorkerWithImportMapViaBedfordsShim} from 'worker-with-import-map';
28
+ // console.log("WorkerWithImportMapViaBedfordsShim", WorkerWithImportMapViaBedfordsShim);
29
+ // const url = './test-add.worker.js';
30
+ const url = './test-divide.worker.js';
31
+ const worker = new WorkerWithImportMapViaBedfordsShim(url, {
32
+ importMap: 'inherit'
33
+ });
34
+ console.log("worker", worker);
35
+ const typePanel = new TypePanel();
36
+ worker.addEventListener('message', (e) => {
37
+ // console.log('addEventListener message', e.data);
38
+ if (e.data.type !== 'rti') {
39
+ return;
40
+ }
41
+ e.preventDefault();
42
+ e.stopPropagation();
43
+ // Forward message to window listener:
44
+ // window.postMessage(e.data);
45
+ // Forward event directly to typePanel:
46
+ typePanel.handleEvent(e);
47
+ });
48
+ */