runtime-type-inspector 2.0.1 → 2.0.3

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 (55) hide show
  1. package/README.md +8 -5
  2. package/build/rti-transpiler.cjs +2359 -0
  3. package/build/rti-transpiler.mjs +1979 -0
  4. package/package.json +4 -2
  5. package/.eslintrc.js +0 -480
  6. package/jsconfig.json +0 -10
  7. package/plugin-parcel/index.mjs +0 -4
  8. package/plugin-rollup/index.mjs +0 -78
  9. package/plugin-webpack/index.mjs +0 -4
  10. package/rollup.config.mjs +0 -183
  11. package/src-runtime/assertType.mjs +0 -54
  12. package/src-runtime/createDiv.mjs +0 -69
  13. package/src-runtime/index.mjs +0 -21
  14. package/src-runtime/makeJSDoc.mjs +0 -40
  15. package/src-runtime/registerClass.mjs +0 -11
  16. package/src-runtime/registerTypedef.mjs +0 -12
  17. package/src-runtime/typecheckDiv.mjs +0 -3
  18. package/src-runtime/typecheckEvery.mjs +0 -21
  19. package/src-runtime/typecheckOptions.mjs +0 -60
  20. package/src-runtime/typecheckTable.mjs +0 -14
  21. package/src-runtime/typecheckWarn.mjs +0 -23
  22. package/src-runtime/validateArray.mjs +0 -26
  23. package/src-runtime/validateDivision.mjs +0 -31
  24. package/src-runtime/validateMap.mjs +0 -36
  25. package/src-runtime/validateNumber.mjs +0 -29
  26. package/src-runtime/validateObject.mjs +0 -43
  27. package/src-runtime/validateRecord.mjs +0 -31
  28. package/src-runtime/validateSet.mjs +0 -26
  29. package/src-runtime/validateTuple.mjs +0 -28
  30. package/src-runtime/validateType.mjs +0 -214
  31. package/src-runtime/validateTypedef.mjs +0 -18
  32. package/src-runtime/validateUnion.mjs +0 -19
  33. package/src-runtime/youCanAddABreakpointHere.mjs +0 -11
  34. package/src-transpiler/Stringifier.mjs +0 -1268
  35. package/src-transpiler/StringifierWithTypeAssertions.mjs +0 -478
  36. package/src-transpiler/addTypeChecks.mjs +0 -20
  37. package/src-transpiler/ast2json.mjs +0 -13
  38. package/src-transpiler/ast2jsonForComparison.mjs +0 -28
  39. package/src-transpiler/code2ast2code.mjs +0 -16
  40. package/src-transpiler/expandType.mjs +0 -187
  41. package/src-transpiler/expandTypeDepFree.mjs +0 -122
  42. package/src-transpiler/extractNameAndOptionality.mjs +0 -23
  43. package/src-transpiler/index.mjs +0 -13
  44. package/src-transpiler/parseJSDoc.mjs +0 -69
  45. package/src-transpiler/parseJSDocSetter.mjs +0 -16
  46. package/src-transpiler/parseJSDocTypedef.mjs +0 -69
  47. package/src-transpiler/simplifyType.mjs +0 -31
  48. package/src-transpiler/stat.mjs +0 -13
  49. package/ui/babel-core.mjs +0 -5
  50. package/ui/babel-helper-plugin-utils.mjs +0 -7
  51. package/ui/babel-parser.mjs +0 -8
  52. package/ui/babel-plugin-syntax-typescript.mjs +0 -58
  53. package/ui/importmap.js +0 -38
  54. package/ui/index.html +0 -30
  55. package/ui/index.mjs +0 -379
package/ui/index.html DELETED
@@ -1,30 +0,0 @@
1
- <html>
2
- <head>
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
- </head>
7
- <body>
8
- <select id="action">
9
- <option value="typechecking">Add full typechecking</option>
10
- <option value="code2ast2code">code2ast2code (testing Stringifier class)</option>
11
- <option value="ast">Show AST from JS</option>
12
- <option value="ast-ts">Show AST from TS</option>
13
- <option value="jsdoc">Show parsed JSDoc</option>
14
- <option value="expand-type">expandType</option>
15
- <option value="eval">Eval</option>
16
- </select>
17
- <select id="preferred-expand-type">
18
- <option value="expandTypeTS">expandTypeTS</option>
19
- <option value="expandTypeDepFree">expandTypeDepFree</option>
20
- </select>
21
- <button id="repl">REPL</button>
22
- <button id="button-compare-ast" onclick="compareAST()">Compare AST</button>
23
- Left: <input id="left" type="checkbox" checked></input>
24
- Right: <input id="right" type="checkbox" checked></input>
25
- <script type="module">
26
- import * as Index from "./index.mjs";
27
- Object.assign(window, { ...Index });
28
- </script>
29
- </body>
30
- </html>
package/ui/index.mjs DELETED
@@ -1,379 +0,0 @@
1
- import {parse } from '@babel/parser';
2
- import {parseJSDoc } from '../src-transpiler/parseJSDoc.mjs';
3
- import {addTypeChecks } from '../src-transpiler/addTypeChecks.mjs';
4
- import {expandType } from '../src-transpiler/expandType.mjs';
5
- import {expandTypeDepFree } from '../src-transpiler/expandTypeDepFree.mjs';
6
- import {code2ast2code } from '../src-transpiler/code2ast2code.mjs';
7
- import {ast2jsonForComparison} from '../src-transpiler/ast2jsonForComparison.mjs';
8
- import * as ti from '../src-transpiler/index.mjs';
9
- import * as rti from '../src-runtime/index.mjs';
10
- const currentAction = location.hash.slice(1).split('=')[1] || 'typechecking';
11
- const selectAction = document.getElementById("action");
12
- if (!(selectAction instanceof HTMLSelectElement)) {
13
- throw 'This module requires a <select id="action" ...';
14
- }
15
- const selectPreferredExpandType = document.getElementById('preferred-expand-type');
16
- if (!(selectPreferredExpandType instanceof HTMLSelectElement)) {
17
- throw 'This module requires a <select id="action" ...';
18
- }
19
- // selectPreferredExpandType.value;
20
- selectAction.value = currentAction;
21
- const buttonREPL = document.getElementById('repl');
22
- if (!(buttonREPL instanceof HTMLButtonElement)) {
23
- throw 'This module requires a <button id="repl" ...';
24
- }
25
- const inputLeft = document.getElementById('left');
26
- if (!(inputLeft instanceof HTMLInputElement)) {
27
- throw 'This module requires a <input id="left" ...';
28
- }
29
- const inputRight = document.getElementById('right');
30
- if (!(inputRight instanceof HTMLInputElement)) {
31
- throw 'This module requires a <input id="right" ...';
32
- }
33
- inputLeft.onchange = (event) => {
34
- const { checked } = event.target;
35
- if (checked) {
36
- // Unhide right editor and resize right one to half size again
37
- aceDivLeft.style.display = "";
38
- setHalfSizeLeft();
39
- setHalfSizeRight();
40
- } else {
41
- // Hide left editor and resize right one to full size
42
- aceDivLeft.style.display = "none";
43
- Object.assign(aceDivRight.style, {
44
- position: "absolute",
45
- left: "0vw",
46
- top: "40px",
47
- width: "100vw",
48
- height: "calc(100vh - 40px)",
49
- });
50
- }
51
- }
52
- inputRight.onchange = (event) => {
53
- const { checked } = event.target;
54
- if (checked) {
55
- // Unhide left editor and resize right one to half size again
56
- aceDivRight.style.display = "";
57
- setHalfSizeLeft();
58
- setHalfSizeRight();
59
- } else {
60
- // Hide left editor and resize right one to full size
61
- aceDivRight.style.display = "none";
62
- Object.assign(aceDivLeft.style, {
63
- position: "absolute",
64
- left: "0vw",
65
- top: "40px",
66
- width: "100vw",
67
- height: "calc(100vh - 40px)",
68
- });
69
- }
70
- }
71
- function getPreferredExpandType() {
72
- const {value} = selectPreferredExpandType;
73
- if (value == 'expandTypeTS') {
74
- return expandType;
75
- }
76
- return expandTypeDepFree;
77
- }
78
- function getCodeForAction() {
79
- switch (getAction()) {
80
- case 'typechecking':
81
- return "const ret = addTypeChecks(jsdoc, {expandType: getPreferredExpandType()});\nsetRight(ret);";
82
- case 'jsdoc':
83
- return "const ret = parseJSDoc(jsdoc);\nsetRight(JSON.stringify(ret, null, 2));";
84
- case 'code2ast2code':
85
- return "const ret = code2ast2code(jsdoc);\nsetRight(ret);";
86
- case 'expand-type':
87
- return 'setRight(expandTypeAll(jsdoc));';
88
- }
89
- return '// This action has no special code';
90
- }
91
- /**
92
- * @param {string} name - The name.
93
- * @param {any} data - Function or object.
94
- * @returns {string}
95
- */
96
- function data2code(name, data) {
97
- if (data instanceof Function) {
98
- return data.toString();
99
- } else if (data instanceof Object && data !== null) {
100
- return `let ${name} = ${JSON.stringify(data, null, 2)};`;
101
- }
102
- return `// data2code> unhandled type: name=${name}, type of data: ${typeof data}`;
103
- }
104
- function activateREPL() {
105
- const code = Object.entries(ti).map(([key, val]) => data2code(key, val)).join('\n');
106
- const codeLocal = [getPreferredExpandType, expandTypeAll].join('\n');
107
- const leftContent = aceEditorLeft.getValue().replaceAll('`', '\\`');
108
- const leftContentAsCode = `const jsdoc = \`${leftContent}\`;`;
109
- const out = [leftContentAsCode, code, codeLocal, getCodeForAction()].join('\n');
110
- setLeft(out);
111
- // @ts-ignore
112
- selectAction.value = "eval";
113
- }
114
- buttonREPL.onclick = activateREPL;
115
- Object.assign(window, {
116
- parse,
117
- ti, ...ti,
118
- rti, ...rti,
119
- });
120
- let lastStats = {};
121
- function statsPrint() {
122
- console.table(lastStats);
123
- }
124
- /**
125
- * @param {string} value
126
- */
127
- function setLeft(value) {
128
- if (typeof value !== 'string') {
129
- aceEditorRight.setValue(`Cannot set type ${typeof value}`);
130
- return;
131
- }
132
- aceEditorLeft.setValue(value);
133
- aceEditorLeft.clearSelection(); // setValue() selects everything, so unselect it now
134
- }
135
- /**
136
- * @param {string} value
137
- */
138
- function setRight(value) {
139
- if (typeof value !== 'string') {
140
- aceEditorRight.setValue(`Cannot set type ${typeof value}`);
141
- return;
142
- }
143
- aceEditorRight.setValue(value);
144
- aceEditorRight.clearSelection(); // setValue() selects everything, so unselect it now
145
- }
146
- function getLeft() {
147
- return aceEditorLeft.getValue();
148
- }
149
- function getRight() {
150
- return aceEditorRight.getValue();
151
- }
152
- async function actionAST() {
153
- const ast = parse(getLeft(), {sourceType: 'module'});
154
- const out = JSON.stringify(ast, function(name, val) {
155
- if (name == "loc" || name == "start" || name == "end") {
156
- return undefined; // remove
157
- }
158
- return val; // keep
159
- }, 2);
160
- setRight(out);
161
- }
162
- async function actionAST_TS() {
163
- const str = getLeft();
164
- const ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, false /*setParentNodes*/);
165
- const out = JSON.stringify(ast, null, 2);
166
- setRight(out);
167
- }
168
- /**
169
- * @param {string} type - The type like `...string`.
170
- */
171
- function expandTypeAll(type) {
172
- const ts = expandType(type);
173
- const depFree = expandTypeDepFree(type);
174
- let out = '';
175
- out += '// expandTypeTS:\n';
176
- out += JSON.stringify(ts, null, 2) + '\n';
177
- out += '// expandTypeDepFree:\n';
178
- out += JSON.stringify(depFree, null, 2) + '\n';
179
- return out;
180
- }
181
- function actionExpandType() {
182
- setRight(expandTypeAll(getLeft()));
183
- }
184
- function actionJSDoc() {
185
- const ret = parseJSDoc(getLeft());
186
- /** @type {string} */
187
- let out;
188
- if (ret === undefined) {
189
- out = '// parseJSDoc returned undefined';
190
- } else {
191
- out = '// parseJSDoc output:\n' + JSON.stringify(ret, null, 2);
192
- }
193
- setRight(out);
194
- }
195
- function actionTypeChecking() {
196
- setRight(addTypeChecks(getLeft(), {expandType: getPreferredExpandType()}));
197
- }
198
- /**
199
- * @todo use Monaco Diff Editor
200
- * @param {string} left
201
- * @param {string} right
202
- */
203
- function compareAST(left = getLeft(), right = getRight()) {
204
- const l = parse(left , {sourceType: 'module'});
205
- const r = parse(right, {sourceType: 'module'});
206
- const ljson = ast2jsonForComparison(l);
207
- const rjson = ast2jsonForComparison(r);
208
- const test = ljson == rjson;
209
- const _ = test ? ' ' : ' NOT ';
210
- console.log(`AST is ${_} equal`);
211
- // todo add conversion buttons
212
- //console.log('ljson', ljson);
213
- //console.log('rjson', rjson);
214
- setLeft(ljson);
215
- setRight(rjson);
216
- }
217
- async function actionCode2Ast2Code() {
218
- const content = getLeft();
219
- const out = code2ast2code(content);
220
- if (!out) {
221
- setRight("// code2ast2code failed");
222
- return;
223
- }
224
- setRight(out);
225
- // compareAST(content, out);
226
- }
227
- async function runAction() {
228
- const action = getAction();
229
- switch (action) {
230
- case 'ast':
231
- await actionAST();
232
- break;
233
- case 'ast-ts':
234
- await actionAST_TS();
235
- break;
236
- case 'jsdoc':
237
- await actionJSDoc();
238
- break;
239
- case 'typechecking':
240
- await actionTypeChecking();
241
- break;
242
- case 'code2ast2code':
243
- await actionCode2Ast2Code();
244
- break;
245
- case 'eval':
246
- eval(aceEditorLeft.getValue());
247
- break;
248
- case 'expand-type':
249
- await actionExpandType();
250
- break;
251
- default:
252
- setRight(`Action ${action} not implemented`);
253
- }
254
- }
255
- async function insertTypes() {
256
- const ret = await postData('insertTypes', {
257
- file: aceEditorLeft.getValue()
258
- });
259
- ret.logs?.map(_ => console.log(..._));
260
- ret.warns?.map(_ => console.warn(..._));
261
- console.log(ret);
262
- if (ret.success) {
263
- lastStats = ret.stats;
264
- statsPrint();
265
- aceEditorRight.setValue(ret.result);
266
- if (ret.insertTypesAndEval) {
267
- eval(ret.result.replaceAll('export ', ''));
268
- }
269
- } else {
270
- aceEditorRight.setValue(JSON.stringify(ret, null, 2));
271
- }
272
- aceEditorRight.clearSelection(); // setValue() selects everything, so unselect it now
273
- }
274
- /**
275
- * @typedef {'typechecking'|'code2ast2code'|'ast'|'ast-ts'|'jsdoc'|'eval'|'expand-type'} Action
276
- */
277
- /** @returns {Action} */
278
- const getAction = () => selectAction.value;
279
- /**
280
- * @param {string} id - The ID.
281
- * @returns {HTMLDivElement} - The DIV element.
282
- */
283
- function createDivWithId(id) {
284
- let div = document.getElementById(id);
285
- if (!div) {
286
- div = document.createElement('div');
287
- div.id = id;
288
- }
289
- if (!(div instanceof HTMLDivElement)) {
290
- throw "createDivWithId> div creation failed";
291
- }
292
- return div;
293
- }
294
- const aceDivLeft = createDivWithId("aceDivLeft");
295
- const aceDivRight = createDivWithId("aceDivRight");
296
- document.body.append(aceDivLeft, aceDivRight);
297
- function setHalfSizeLeft() {
298
- Object.assign(aceDivLeft.style, {
299
- position: "absolute",
300
- left: "0px",
301
- top: "40px",
302
- width: "50vw",
303
- height: "calc(100vh - 40px)",
304
- });
305
- }
306
- function setHalfSizeRight() {
307
- Object.assign(aceDivRight.style, {
308
- position: "absolute",
309
- left: "50vw",
310
- top: "40px",
311
- width: "50vw",
312
- height: "calc(100vh - 40px)",
313
- });
314
- }
315
- setHalfSizeLeft();
316
- setHalfSizeRight();
317
- /**
318
- * @param {string} id - ID of editor.
319
- * @param {string} txt - Initial text of editor.
320
- * @param {Function} execShiftEnter - Function for Shift+Enter
321
- * @param {Function} execAltEnter - Function for Alt+Enter
322
- * @returns {ACE.Editor} The ACE editor.
323
- */
324
- function setupAce(id, txt, execShiftEnter, execAltEnter) {
325
- const aceEditor = ace.edit(id);
326
- aceEditor.setFontSize(20);
327
- aceEditor.setTheme('ace/theme/chrome');
328
- aceEditor.session.setMode('ace/mode/javascript');
329
- aceEditor.session.setUseWorker(false);
330
- aceEditor.session.setOptions({
331
- tabSize: 2,
332
- useSoftTabs: true
333
- });
334
- aceEditor.setValue(txt);
335
- aceEditor.clearSelection(); // setValue() selects everything, so unselect it now
336
- aceEditor.commands.addCommand({
337
- name: 'Insert types',
338
- bindKey: {win: 'Shift-Enter', mac: 'Shift-Enter'},
339
- exec: execShiftEnter
340
- });
341
- aceEditor.commands.addCommand({
342
- name: 'Show AST',
343
- bindKey: {win: 'Alt-Enter', mac: 'Alt-Enter'},
344
- exec: execAltEnter
345
- });
346
- return aceEditor;
347
- }
348
- const aceEditorLeft = setupAce(
349
- 'aceDivLeft',
350
- '// Shift-Enter: convert content of left editor,\n// write result to right editor\n// Tip: open Devtools to see warnings',
351
- //editor => insertTypes(),
352
- editor => runAction(),
353
- editor => actionAST()
354
- );
355
- const aceEditorRight = setupAce(
356
- 'aceDivRight',
357
- '// Result editor',
358
- editor => runRightEditor(),
359
- editor => console.log("right alt")
360
- );
361
- function runRightEditor() {
362
- eval(aceEditorRight.getValue());
363
- }
364
- export {
365
- selectAction,
366
- selectPreferredExpandType,
367
- buttonREPL,
368
- aceDivLeft,
369
- aceDivRight,
370
- aceEditorLeft,
371
- aceEditorRight,
372
- setLeft,
373
- setRight,
374
- getLeft,
375
- getRight,
376
- compareAST,
377
- getPreferredExpandType,
378
- expandTypeAll,
379
- };