runtime-type-inspector 2.0.2 → 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 (54) hide show
  1. package/build/rti-transpiler.cjs +2359 -0
  2. package/build/rti-transpiler.mjs +1979 -0
  3. package/package.json +4 -2
  4. package/.eslintrc.js +0 -480
  5. package/jsconfig.json +0 -10
  6. package/plugin-parcel/index.mjs +0 -4
  7. package/plugin-rollup/index.mjs +0 -78
  8. package/plugin-webpack/index.mjs +0 -4
  9. package/rollup.config.mjs +0 -183
  10. package/src-runtime/assertType.mjs +0 -54
  11. package/src-runtime/createDiv.mjs +0 -69
  12. package/src-runtime/index.mjs +0 -21
  13. package/src-runtime/makeJSDoc.mjs +0 -40
  14. package/src-runtime/registerClass.mjs +0 -11
  15. package/src-runtime/registerTypedef.mjs +0 -12
  16. package/src-runtime/typecheckDiv.mjs +0 -3
  17. package/src-runtime/typecheckEvery.mjs +0 -21
  18. package/src-runtime/typecheckOptions.mjs +0 -60
  19. package/src-runtime/typecheckTable.mjs +0 -14
  20. package/src-runtime/typecheckWarn.mjs +0 -23
  21. package/src-runtime/validateArray.mjs +0 -26
  22. package/src-runtime/validateDivision.mjs +0 -31
  23. package/src-runtime/validateMap.mjs +0 -37
  24. package/src-runtime/validateNumber.mjs +0 -29
  25. package/src-runtime/validateObject.mjs +0 -43
  26. package/src-runtime/validateRecord.mjs +0 -31
  27. package/src-runtime/validateSet.mjs +0 -26
  28. package/src-runtime/validateTuple.mjs +0 -28
  29. package/src-runtime/validateType.mjs +0 -214
  30. package/src-runtime/validateTypedef.mjs +0 -18
  31. package/src-runtime/validateUnion.mjs +0 -19
  32. package/src-runtime/youCanAddABreakpointHere.mjs +0 -11
  33. package/src-transpiler/Stringifier.mjs +0 -1275
  34. package/src-transpiler/StringifierWithTypeAssertions.mjs +0 -478
  35. package/src-transpiler/addTypeChecks.mjs +0 -20
  36. package/src-transpiler/ast2json.mjs +0 -13
  37. package/src-transpiler/ast2jsonForComparison.mjs +0 -28
  38. package/src-transpiler/code2ast2code.mjs +0 -16
  39. package/src-transpiler/expandType.mjs +0 -187
  40. package/src-transpiler/expandTypeDepFree.mjs +0 -122
  41. package/src-transpiler/extractNameAndOptionality.mjs +0 -23
  42. package/src-transpiler/index.mjs +0 -13
  43. package/src-transpiler/parseJSDoc.mjs +0 -69
  44. package/src-transpiler/parseJSDocSetter.mjs +0 -16
  45. package/src-transpiler/parseJSDocTypedef.mjs +0 -69
  46. package/src-transpiler/simplifyType.mjs +0 -31
  47. package/src-transpiler/stat.mjs +0 -13
  48. package/ui/babel-core.mjs +0 -5
  49. package/ui/babel-helper-plugin-utils.mjs +0 -7
  50. package/ui/babel-parser.mjs +0 -8
  51. package/ui/babel-plugin-syntax-typescript.mjs +0 -58
  52. package/ui/importmap.js +0 -38
  53. package/ui/index.html +0 -30
  54. package/ui/index.mjs +0 -379
@@ -0,0 +1,2359 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/parser'), require('typescript')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@babel/parser', 'typescript'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.rtiTranspiler = {}, global.parser, global.ts));
5
+ })(this, (function (exports, parser, ts) { 'use strict';
6
+
7
+ function _iterableToArrayLimit(r, l) {
8
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
9
+ if (null != t) {
10
+ var e,
11
+ n,
12
+ i,
13
+ u,
14
+ a = [],
15
+ f = !0,
16
+ o = !1;
17
+ try {
18
+ if (i = (t = t.call(r)).next, 0 === l) {
19
+ if (Object(t) !== t) return;
20
+ f = !1;
21
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
22
+ } catch (r) {
23
+ o = !0, n = r;
24
+ } finally {
25
+ try {
26
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
27
+ } finally {
28
+ if (o) throw n;
29
+ }
30
+ }
31
+ return a;
32
+ }
33
+ }
34
+ function _classCallCheck(instance, Constructor) {
35
+ if (!(instance instanceof Constructor)) {
36
+ throw new TypeError("Cannot call a class as a function");
37
+ }
38
+ }
39
+ function _defineProperties(target, props) {
40
+ for (var i = 0; i < props.length; i++) {
41
+ var descriptor = props[i];
42
+ descriptor.enumerable = descriptor.enumerable || false;
43
+ descriptor.configurable = true;
44
+ if ("value" in descriptor) descriptor.writable = true;
45
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
46
+ }
47
+ }
48
+ function _createClass(Constructor, protoProps, staticProps) {
49
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
50
+ if (staticProps) _defineProperties(Constructor, staticProps);
51
+ Object.defineProperty(Constructor, "prototype", {
52
+ writable: false
53
+ });
54
+ return Constructor;
55
+ }
56
+ function _defineProperty(obj, key, value) {
57
+ key = _toPropertyKey(key);
58
+ if (key in obj) {
59
+ Object.defineProperty(obj, key, {
60
+ value: value,
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true
64
+ });
65
+ } else {
66
+ obj[key] = value;
67
+ }
68
+ return obj;
69
+ }
70
+ function _inherits(subClass, superClass) {
71
+ if (typeof superClass !== "function" && superClass !== null) {
72
+ throw new TypeError("Super expression must either be null or a function");
73
+ }
74
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
75
+ constructor: {
76
+ value: subClass,
77
+ writable: true,
78
+ configurable: true
79
+ }
80
+ });
81
+ Object.defineProperty(subClass, "prototype", {
82
+ writable: false
83
+ });
84
+ if (superClass) _setPrototypeOf(subClass, superClass);
85
+ }
86
+ function _getPrototypeOf(o) {
87
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
88
+ return o.__proto__ || Object.getPrototypeOf(o);
89
+ };
90
+ return _getPrototypeOf(o);
91
+ }
92
+ function _setPrototypeOf(o, p) {
93
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
94
+ o.__proto__ = p;
95
+ return o;
96
+ };
97
+ return _setPrototypeOf(o, p);
98
+ }
99
+ function _isNativeReflectConstruct() {
100
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
101
+ if (Reflect.construct.sham) return false;
102
+ if (typeof Proxy === "function") return true;
103
+ try {
104
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
105
+ return true;
106
+ } catch (e) {
107
+ return false;
108
+ }
109
+ }
110
+ function _assertThisInitialized(self) {
111
+ if (self === void 0) {
112
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
113
+ }
114
+ return self;
115
+ }
116
+ function _possibleConstructorReturn(self, call) {
117
+ if (call && (typeof call === "object" || typeof call === "function")) {
118
+ return call;
119
+ } else if (call !== void 0) {
120
+ throw new TypeError("Derived constructors may only return object or undefined");
121
+ }
122
+ return _assertThisInitialized(self);
123
+ }
124
+ function _createSuper(Derived) {
125
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
126
+ return function _createSuperInternal() {
127
+ var Super = _getPrototypeOf(Derived),
128
+ result;
129
+ if (hasNativeReflectConstruct) {
130
+ var NewTarget = _getPrototypeOf(this).constructor;
131
+ result = Reflect.construct(Super, arguments, NewTarget);
132
+ } else {
133
+ result = Super.apply(this, arguments);
134
+ }
135
+ return _possibleConstructorReturn(this, result);
136
+ };
137
+ }
138
+ function _superPropBase(object, property) {
139
+ while (!Object.prototype.hasOwnProperty.call(object, property)) {
140
+ object = _getPrototypeOf(object);
141
+ if (object === null) break;
142
+ }
143
+ return object;
144
+ }
145
+ function _get() {
146
+ if (typeof Reflect !== "undefined" && Reflect.get) {
147
+ _get = Reflect.get.bind();
148
+ } else {
149
+ _get = function _get(target, property, receiver) {
150
+ var base = _superPropBase(target, property);
151
+ if (!base) return;
152
+ var desc = Object.getOwnPropertyDescriptor(base, property);
153
+ if (desc.get) {
154
+ return desc.get.call(arguments.length < 3 ? target : receiver);
155
+ }
156
+ return desc.value;
157
+ };
158
+ }
159
+ return _get.apply(this, arguments);
160
+ }
161
+ function _slicedToArray(arr, i) {
162
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
163
+ }
164
+ function _toConsumableArray(arr) {
165
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
166
+ }
167
+ function _arrayWithoutHoles(arr) {
168
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
169
+ }
170
+ function _arrayWithHoles(arr) {
171
+ if (Array.isArray(arr)) return arr;
172
+ }
173
+ function _iterableToArray(iter) {
174
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
175
+ }
176
+ function _unsupportedIterableToArray(o, minLen) {
177
+ if (!o) return;
178
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
179
+ var n = Object.prototype.toString.call(o).slice(8, -1);
180
+ if (n === "Object" && o.constructor) n = o.constructor.name;
181
+ if (n === "Map" || n === "Set") return Array.from(o);
182
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
183
+ }
184
+ function _arrayLikeToArray(arr, len) {
185
+ if (len == null || len > arr.length) len = arr.length;
186
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
187
+ return arr2;
188
+ }
189
+ function _nonIterableSpread() {
190
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
191
+ }
192
+ function _nonIterableRest() {
193
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
194
+ }
195
+ function _createForOfIteratorHelper(o, allowArrayLike) {
196
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
197
+ if (!it) {
198
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
199
+ if (it) o = it;
200
+ var i = 0;
201
+ var F = function () {};
202
+ return {
203
+ s: F,
204
+ n: function () {
205
+ if (i >= o.length) return {
206
+ done: true
207
+ };
208
+ return {
209
+ done: false,
210
+ value: o[i++]
211
+ };
212
+ },
213
+ e: function (e) {
214
+ throw e;
215
+ },
216
+ f: F
217
+ };
218
+ }
219
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
220
+ }
221
+ var normalCompletion = true,
222
+ didErr = false,
223
+ err;
224
+ return {
225
+ s: function () {
226
+ it = it.call(o);
227
+ },
228
+ n: function () {
229
+ var step = it.next();
230
+ normalCompletion = step.done;
231
+ return step;
232
+ },
233
+ e: function (e) {
234
+ didErr = true;
235
+ err = e;
236
+ },
237
+ f: function () {
238
+ try {
239
+ if (!normalCompletion && it.return != null) it.return();
240
+ } finally {
241
+ if (didErr) throw err;
242
+ }
243
+ }
244
+ };
245
+ }
246
+ function _toPrimitive(input, hint) {
247
+ if (typeof input !== "object" || input === null) return input;
248
+ var prim = input[Symbol.toPrimitive];
249
+ if (prim !== undefined) {
250
+ var res = prim.call(input, hint || "default");
251
+ if (typeof res !== "object") return res;
252
+ throw new TypeError("@@toPrimitive must return a primitive value.");
253
+ }
254
+ return (hint === "string" ? String : Number)(input);
255
+ }
256
+ function _toPropertyKey(arg) {
257
+ var key = _toPrimitive(arg, "string");
258
+ return typeof key === "symbol" ? key : String(key);
259
+ }
260
+
261
+ function expandTypeDepFree(type) {
262
+ type = type.trim();
263
+ while (!type.includes('|') && type[0] == '(' && type[type.length - 1] == ')') {
264
+ type = type.slice(1, -1).trim();
265
+ }
266
+ if (type[0] === '.' && type[1] === '.' && type[2] === '.') {
267
+ return {
268
+ type: 'array',
269
+ elementType: type.slice(3)
270
+ };
271
+ }
272
+ if (type.startsWith("Array<") && type.endsWith('>')) {
273
+ var typeSlice = type.slice(6, -1);
274
+ return {
275
+ type: "array",
276
+ elementType: expandTypeDepFree(typeSlice)
277
+ };
278
+ }
279
+ if ((type.startsWith("Object<") || type.startsWith("Record<")) && type.endsWith('>')) {
280
+ var recordSlice = type.slice(7, -1);
281
+ var firstComma = recordSlice.indexOf(',');
282
+ if (firstComma === -1) {
283
+ console.warn("expandTypeDepFree> invalid Object/Record");
284
+ }
285
+ var key = recordSlice.slice(0, firstComma).trim();
286
+ var val = recordSlice.slice(firstComma + 1).trim();
287
+ return {
288
+ type: "record",
289
+ key: expandTypeDepFree(key),
290
+ val: expandTypeDepFree(val)
291
+ };
292
+ }
293
+ if (type[0] == '{' && type[type.length - 1] == '}') {
294
+ var propertiesArray = type.slice(1, -1).split(',');
295
+ var properties = {};
296
+ propertiesArray.forEach(function (_) {
297
+ var _$split$map = _.split(":").map(function (_) {
298
+ return _.trim();
299
+ }),
300
+ _$split$map2 = _slicedToArray(_$split$map, 2),
301
+ propName = _$split$map2[0],
302
+ propType = _$split$map2[1];
303
+ if (!propName || !propType) {
304
+ console.warn('expandTypeDepFree> unexpected type format, fix');
305
+ return false;
306
+ }
307
+ properties[propName] = propType;
308
+ });
309
+ return {
310
+ type: 'object',
311
+ properties: properties
312
+ };
313
+ }
314
+ var members = type.split("|");
315
+ if (members.length >= 2) {
316
+ members.forEach(function (_, i) {
317
+ return members[i] = _.trim();
318
+ });
319
+ return {
320
+ type: 'union',
321
+ members: members.map(expandTypeDepFree)
322
+ };
323
+ }
324
+ if (type.endsWith("[]")) {
325
+ var _typeSlice = type.slice(0, -2);
326
+ return {
327
+ type: 'array',
328
+ elementType: expandTypeDepFree(_typeSlice)
329
+ };
330
+ }
331
+ if (type[0] == '[' && type[type.length - 1] == ']') {
332
+ var elements = type.slice(1, -1).split(',');
333
+ return {
334
+ type: 'tuple',
335
+ elements: elements.map(expandTypeDepFree)
336
+ };
337
+ }
338
+ if (type === 'object' || type === 'Object') {
339
+ return {
340
+ type: 'object',
341
+ properties: {}
342
+ };
343
+ }
344
+ return type;
345
+ }
346
+
347
+ function simplifyType(type, optional) {
348
+ if (type instanceof Object) {
349
+ type.optional = optional;
350
+ } else if (typeof type === 'string') {
351
+ type = type.trim();
352
+ if (type != 'object' && type != 'object[]' && type != 'union' && !optional) {
353
+ return type;
354
+ }
355
+ type = {
356
+ type: type,
357
+ optional: optional
358
+ };
359
+ } else {
360
+ debugger;
361
+ console.warn("simplifyType> neither object nor string for type", type);
362
+ }
363
+ if (type.type == 'object' && type.properties && Object.keys(type.properties).length === 0) {
364
+ delete type.properties;
365
+ }
366
+ return type;
367
+ }
368
+
369
+ function parseJSDoc(src) {
370
+ var expandType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : expandTypeDepFree;
371
+ var regex = /@param \{(.*?)\} ([\[\]a-zA-Z0-9_$=\{\}\.'" ]+)/g;
372
+ var matches = _toConsumableArray(src.matchAll(regex));
373
+ var params = Object.create(null);
374
+ matches.forEach(function (_) {
375
+ var type = expandType(_[1].trim());
376
+ var name = _[2].trim();
377
+ var optional = false;
378
+ if (name[0] === '[') {
379
+ var closer = name.lastIndexOf(']');
380
+ name = name.substring(1, closer);
381
+ optional = true;
382
+ }
383
+ name = name.split(' ')[0].split('=')[0].trim();
384
+ var simplifiedType = simplifyType(type, optional);
385
+ var parts = name.split(".");
386
+ if (parts.length == 3) {
387
+ var parts0 = parts[0];
388
+ var parts1 = parts[1];
389
+ var parts2 = parts[2];
390
+ var toptype = params[parts0];
391
+ toptype.properties[parts1].properties = toptype.properties[parts1].properties || {};
392
+ toptype.properties[parts1].properties[parts2] = simplifiedType;
393
+ } else if (parts.length == 2) {
394
+ var _parts = parts[0];
395
+ var _parts2 = parts[1];
396
+ if (_parts.endsWith('[]')) {
397
+ _parts = _parts.slice(0, -2);
398
+ }
399
+ var _toptype = params[_parts];
400
+ if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) == "union") {
401
+ var typeObject = _toptype.members.find(function (_) {
402
+ return (_ === null || _ === void 0 ? void 0 : _.type) == 'object';
403
+ });
404
+ typeObject.properties[_parts2] = simplifiedType;
405
+ } else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) == "array") {
406
+ _toptype.elementType.properties[_parts2] = simplifiedType;
407
+ } else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) == "object") {
408
+ _toptype.properties = _toptype.properties || {};
409
+ _toptype.properties[_parts2] = simplifiedType;
410
+ } else {
411
+ console.warn("parseJSDoc> skipping @param, unseen syntax detected, please check if your JSDoc is valid or open an issue about this", {
412
+ src: src,
413
+ toptype: _toptype,
414
+ parts0: _parts,
415
+ parts1: _parts2,
416
+ simplifiedType: simplifiedType
417
+ });
418
+ }
419
+ } else {
420
+ params[name] = simplifiedType;
421
+ }
422
+ });
423
+ if (Object.keys(params).length === 0) {
424
+ return;
425
+ }
426
+ return params;
427
+ }
428
+
429
+ function parseJSDocSetter(src) {
430
+ var expandType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : expandTypeDepFree;
431
+ var regex = /@type \{(.*?)\}/g;
432
+ var matches = _toConsumableArray(src.matchAll(regex));
433
+ if (matches.length === 1) {
434
+ var match = matches[0];
435
+ var type = expandType(match[1]);
436
+ var simplifiedType = simplifyType(type, false);
437
+ return simplifiedType;
438
+ }
439
+ }
440
+
441
+ function extractNameAndOptionality(rest) {
442
+ rest = rest.trim();
443
+ var optional = false;
444
+ if (rest[0] === '[') {
445
+ var closer = rest.lastIndexOf(']');
446
+ rest = rest.substring(1, closer);
447
+ optional = true;
448
+ }
449
+ var name = rest.split(' ')[0].split('=')[0].trim();
450
+ return [name, optional];
451
+ }
452
+
453
+ function extractCurlyContent(line) {
454
+ var firstCurly = line.indexOf('{');
455
+ var k = firstCurly + 1;
456
+ var count = 0;
457
+ for (; k < line.length; k++) {
458
+ var c = line[k];
459
+ if (c === '{') {
460
+ count++;
461
+ } else if (c === '}') {
462
+ count--;
463
+ }
464
+ if (count === -1) {
465
+ break;
466
+ }
467
+ }
468
+ var content = line.substring(firstCurly + 1, k);
469
+ return {
470
+ content: content,
471
+ nextIndex: k + 1
472
+ };
473
+ }
474
+ function parseJSDocTypedef(typedefs, warn, comment, expandType) {
475
+ var type = comment.type,
476
+ value = comment.value;
477
+ if (type !== 'CommentBlock') {
478
+ return;
479
+ }
480
+ var lines = value.split('\n');
481
+ var lastTypedef = undefined;
482
+ var _iterator = _createForOfIteratorHelper(lines),
483
+ _step;
484
+ try {
485
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
486
+ var line = _step.value;
487
+ line = line.trim();
488
+ if (line[0] === '*') {
489
+ line = line.slice(1).trim();
490
+ }
491
+ if (line.startsWith('@typedef')) {
492
+ var _extractCurlyContent = extractCurlyContent(line),
493
+ def = _extractCurlyContent.content,
494
+ nextIndex = _extractCurlyContent.nextIndex;
495
+ var name = line.substring(nextIndex).trim();
496
+ name = name.split(' ')[0];
497
+ lastTypedef = expandType(def);
498
+ typedefs[name] = lastTypedef;
499
+ } else if (line.startsWith('@property')) {
500
+ var _lastTypedef;
501
+ if (!lastTypedef) {
502
+ continue;
503
+ }
504
+ var _extractCurlyContent2 = extractCurlyContent(line),
505
+ content = _extractCurlyContent2.content,
506
+ _nextIndex = _extractCurlyContent2.nextIndex;
507
+ var rest = line.substring(_nextIndex);
508
+ var propType = expandType(content);
509
+ var _extractNameAndOption = extractNameAndOptionality(rest),
510
+ _extractNameAndOption2 = _slicedToArray(_extractNameAndOption, 2),
511
+ _name = _extractNameAndOption2[0],
512
+ optional = _extractNameAndOption2[1];
513
+ var finalType = simplifyType(propType, optional);
514
+ if (((_lastTypedef = lastTypedef) === null || _lastTypedef === void 0 ? void 0 : _lastTypedef.type) === 'object') {
515
+ lastTypedef.properties[_name] = finalType;
516
+ } else {
517
+ warn("not an extensible type", lastTypedef);
518
+ }
519
+ }
520
+ }
521
+ } catch (err) {
522
+ _iterator.e(err);
523
+ } finally {
524
+ _iterator.f();
525
+ }
526
+ }
527
+
528
+ function statReset(stat) {
529
+ stat.checked = 0;
530
+ stat.unchecked = 0;
531
+ }
532
+
533
+ var Stringifier = function () {
534
+ function Stringifier() {
535
+ _classCallCheck(this, Stringifier);
536
+ _defineProperty(this, "forceCurly", false);
537
+ _defineProperty(this, "parents", []);
538
+ }
539
+ _createClass(Stringifier, [{
540
+ key: "toSource",
541
+ value: function toSource(node) {
542
+ var _node$extra;
543
+ if (node === null) {
544
+ return '';
545
+ }
546
+ if (node.type === 'File') {
547
+ this.parents.length = 0;
548
+ }
549
+ this.parents.push(node);
550
+ var out = '';
551
+ var comments = '';
552
+ if (node && node.leadingComments) {
553
+ comments += this.mapToSource(node.leadingComments).join('');
554
+ }
555
+ if (node !== null && node !== void 0 && (_node$extra = node.extra) !== null && _node$extra !== void 0 && _node$extra.parenthesized) {
556
+ out += "(".concat(comments).concat(this.toSource_(node), ")");
557
+ } else {
558
+ out += comments + this.toSource_(node);
559
+ }
560
+ this.parents.pop();
561
+ return out;
562
+ }
563
+ }, {
564
+ key: "toSource_",
565
+ value: function toSource_(node) {
566
+ if (!node) {
567
+ return '';
568
+ }
569
+ var type = node.type;
570
+ if (type === undefined) {
571
+ console.log("toSource> node without type", node);
572
+ }
573
+ if (this[type]) {
574
+ return this[type](node);
575
+ }
576
+ console.log(node);
577
+ var notOfInterest = ["start", "end", "loc", "type"];
578
+ var ofInterest = Object.keys(node).filter(function (_) {
579
+ return !notOfInterest.includes(_);
580
+ });
581
+ var props = ofInterest.join(', ');
582
+ var fn = "".concat(type, "(node) {\n const {").concat(props, "} = node;\n return ;\n}");
583
+ console.warn('TODO ADD METHOD:', fn);
584
+ return "rtiUnhandled(\"".concat(node.type, "\");\n");
585
+ }
586
+ }, {
587
+ key: "toSourceCurly",
588
+ value: function toSourceCurly(node) {
589
+ if (!this.forceCurly) {
590
+ return this.toSource(node);
591
+ }
592
+ var type = node.type;
593
+ var needCurly = type != 'BlockStatement' && type != 'EmptyStatement';
594
+ var spaces = this.spaces;
595
+ var out = '';
596
+ if (needCurly) {
597
+ out += ' {\n';
598
+ }
599
+ out += this.toSource(node);
600
+ if (needCurly) {
601
+ out += '\n';
602
+ out += spaces;
603
+ out += '}';
604
+ }
605
+ return out;
606
+ }
607
+ }, {
608
+ key: "parent",
609
+ get: function get() {
610
+ return this.parents[this.parents.length - 2];
611
+ }
612
+ }, {
613
+ key: "parentType",
614
+ get: function get() {
615
+ var _this$parent;
616
+ return (_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.type;
617
+ }
618
+ }, {
619
+ key: "needSpaces",
620
+ get: function get() {
621
+ var t = this.parentType;
622
+ return t != "ForStatement" && t != "ForInStatement" && t != "ForOfStatement";
623
+ }
624
+ }, {
625
+ key: "mapToSource",
626
+ value: function mapToSource(arr) {
627
+ var _this = this;
628
+ return arr.map(function (_) {
629
+ return _this.toSource(_);
630
+ });
631
+ }
632
+ }, {
633
+ key: "generateTypeChecks",
634
+ value: function generateTypeChecks(node) {
635
+ var spaces = this.spaces;
636
+ return "".concat(spaces, "/* Stub of Stringifier#generateTypeChecks({type=").concat(node.type, "}); */\n");
637
+ }
638
+ }, {
639
+ key: "spaces",
640
+ get: function get() {
641
+ var _parents$, _parents$2;
642
+ var parents = this.parents;
643
+ var n = parents.length;
644
+ if (((_parents$ = parents[0]) === null || _parents$ === void 0 ? void 0 : _parents$.type) == 'File') {
645
+ n--;
646
+ }
647
+ if (((_parents$2 = parents[1]) === null || _parents$2 === void 0 ? void 0 : _parents$2.type) == 'Program') {
648
+ n--;
649
+ }
650
+ n -= parents.filter(function (_) {
651
+ return _.type === 'BlockStatement' || _.type === 'VariableDeclarator' || _.type === 'ObjectProperty' || _.type === 'ExportNamedDeclaration' || _.type === 'NewExpression' || _.type === 'AssignmentExpression' || _.type === 'ArrayExpression' || _.type === 'ClassBody';
652
+ }).length;
653
+ n -= 1;
654
+ if (parents.some(function (_) {
655
+ return _.type === 'CallExpression';
656
+ }) && parents.some(function (_) {
657
+ return _.type === 'ObjectExpression';
658
+ })) {
659
+ n--;
660
+ }
661
+ if (n < 0) {
662
+ n = 0;
663
+ }
664
+ return ' '.repeat(n);
665
+ }
666
+ }, {
667
+ key: "AwaitExpression",
668
+ value: function AwaitExpression(node) {
669
+ var argument = node.argument;
670
+ return "await ".concat(this.toSource(argument));
671
+ }
672
+ }, {
673
+ key: "ClassProperty",
674
+ value: function ClassProperty(node) {
675
+ var key = node.key,
676
+ computed = node.computed,
677
+ value = node.value;
678
+ node.leadingComments;
679
+ var static_ = node.static;
680
+ var a = this.toSource(key);
681
+ var b = this.toSource(value);
682
+ var out = this.spaces;
683
+ if (static_) {
684
+ out += 'static ';
685
+ }
686
+ if (computed) {
687
+ out += "[".concat(a, "]");
688
+ } else {
689
+ out += a;
690
+ }
691
+ if (b) {
692
+ out += " = ".concat(b);
693
+ }
694
+ out += ';';
695
+ return out;
696
+ }
697
+ }, {
698
+ key: "FunctionDeclarationParams",
699
+ value: function FunctionDeclarationParams(params) {
700
+ return '(' + this.mapToSource(params).join(', ') + ')';
701
+ }
702
+ }, {
703
+ key: "FunctionDeclaration",
704
+ value: function FunctionDeclaration(node) {
705
+ var async = node.async,
706
+ body = node.body,
707
+ generator = node.generator,
708
+ id = node.id;
709
+ node.leadingComments;
710
+ var params = node.params;
711
+ var out = '';
712
+ if (this.parentType !== 'ExportNamedDeclaration') {
713
+ out += this.spaces;
714
+ }
715
+ if (async) {
716
+ out += 'async ';
717
+ }
718
+ var asterix = generator ? '*' : '';
719
+ out += 'function' + asterix + ' ' + this.toSource(id) + this.FunctionDeclarationParams(params);
720
+ out += this.toSource(body);
721
+ return out;
722
+ }
723
+ }, {
724
+ key: "FunctionExpression",
725
+ value: function FunctionExpression(node) {
726
+ var async = node.async,
727
+ body = node.body,
728
+ generator = node.generator,
729
+ id = node.id,
730
+ params = node.params;
731
+ var out = '';
732
+ if (async) {
733
+ out += 'async ';
734
+ }
735
+ var star = generator ? '*' : '';
736
+ out += 'function' + star + ' ' + this.toSource(id) + this.FunctionDeclarationParams(params);
737
+ out += this.toSource(body);
738
+ return out;
739
+ }
740
+ }, {
741
+ key: "ArrowFunctionExpression",
742
+ value: function ArrowFunctionExpression(node) {
743
+ var async = node.async,
744
+ body = node.body,
745
+ generator = node.generator,
746
+ params = node.params;
747
+ var out = '';
748
+ if (async) {
749
+ out += 'async ';
750
+ }
751
+ if (generator) {
752
+ out += ' * ';
753
+ }
754
+ out += this.FunctionDeclarationParams(params);
755
+ out += ' => ';
756
+ out += this.toSource(body);
757
+ return out;
758
+ }
759
+ }, {
760
+ key: "BigIntLiteral",
761
+ value: function BigIntLiteral(node) {
762
+ var extra = node.extra;
763
+ node.value;
764
+ return extra.raw;
765
+ }
766
+ }, {
767
+ key: "BlockStatement",
768
+ value: function BlockStatement(node) {
769
+ var body = node.body,
770
+ directives = node.directives;
771
+ var spaces = this.spaces;
772
+ var out = '';
773
+ out += ' {\n';
774
+ if (directives && directives.length) {
775
+ out += this.mapToSource(directives).join('\n') + '\n';
776
+ }
777
+ out += this.generateTypeChecks(node);
778
+ out += this.mapToSource(body).join('\n') + '\n';
779
+ out += spaces;
780
+ out += '}';
781
+ return out;
782
+ }
783
+ }, {
784
+ key: "ClassExpression",
785
+ value: function ClassExpression(node) {
786
+ var id = node.id,
787
+ superClass = node.superClass,
788
+ body = node.body;
789
+ var out = 'class ';
790
+ if (id !== null) {
791
+ out += this.toSource(id) + ' ';
792
+ }
793
+ if (superClass !== null) {
794
+ var s = this.toSource(superClass);
795
+ out += "extends ".concat(s, " ");
796
+ }
797
+ var c = this.toSource(body);
798
+ out += "{\n".concat(c, "\n}");
799
+ return out;
800
+ }
801
+ }, {
802
+ key: "Directive",
803
+ value: function Directive(node) {
804
+ var value = node.value;
805
+ return this.toSource(value);
806
+ }
807
+ }, {
808
+ key: "DirectiveLiteral",
809
+ value: function DirectiveLiteral(node) {
810
+ var extra = node.extra;
811
+ node.value;
812
+ var spaces = this.spaces;
813
+ return "".concat(spaces).concat(extra.raw, ";");
814
+ }
815
+ }, {
816
+ key: "ReturnStatement",
817
+ value: function ReturnStatement(node) {
818
+ var argument = node.argument;
819
+ var spaces = this.spaces;
820
+ if (!argument) {
821
+ return spaces + 'return;';
822
+ }
823
+ return spaces + 'return ' + this.toSource(argument) + ';';
824
+ }
825
+ }, {
826
+ key: "Identifier",
827
+ value: function Identifier(node) {
828
+ return node.name;
829
+ }
830
+ }, {
831
+ key: "IfStatement",
832
+ value: function IfStatement(node) {
833
+ var alternate = node.alternate,
834
+ consequent = node.consequent,
835
+ test = node.test;
836
+ var spaces = this.spaces;
837
+ var out = '';
838
+ {
839
+ out += spaces;
840
+ }
841
+ out += "if (".concat(this.toSource(test), ")");
842
+ out += this.toSourceCurly(consequent);
843
+ if (alternate) {
844
+ out += " else".concat(this.toSourceCurly(alternate));
845
+ }
846
+ return out;
847
+ }
848
+ }, {
849
+ key: "LabeledStatement",
850
+ value: function LabeledStatement(node) {
851
+ var body = node.body,
852
+ label = node.label;
853
+ var spaces = this.spaces;
854
+ var out = '';
855
+ out += spaces + this.toSource(label);
856
+ out += ': \n';
857
+ out += spaces + this.toSource(body);
858
+ return out;
859
+ }
860
+ }, {
861
+ key: "UnaryExpression",
862
+ value: function UnaryExpression(node) {
863
+ var out = '';
864
+ var argument = node.argument,
865
+ operator = node.operator,
866
+ prefix = node.prefix;
867
+ if (!prefix) {
868
+ console.warn("Stringifier#UnaryExpression> never considered !prefix", node);
869
+ }
870
+ var c = operator.charCodeAt(0);
871
+ var op = operator;
872
+ if (c >= 97 && c <= 122) {
873
+ op += ' ';
874
+ }
875
+ out += op;
876
+ out += this.toSource(argument);
877
+ return out;
878
+ }
879
+ }, {
880
+ key: "MemberExpression",
881
+ value: function MemberExpression(node) {
882
+ var computed = node.computed,
883
+ object = node.object,
884
+ property = node.property;
885
+ if (computed) {
886
+ return "".concat(this.toSource(object), "[").concat(this.toSource(property), "]");
887
+ }
888
+ return "".concat(this.toSource(object), ".").concat(this.toSource(property));
889
+ }
890
+ }, {
891
+ key: "MetaProperty",
892
+ value: function MetaProperty(node) {
893
+ var meta = node.meta,
894
+ property = node.property;
895
+ var lhs = this.toSource(meta);
896
+ var rhs = this.toSource(property);
897
+ return "".concat(lhs, ".").concat(rhs);
898
+ }
899
+ }, {
900
+ key: "ExpressionStatement",
901
+ value: function ExpressionStatement(node) {
902
+ var expression = node.expression;
903
+ return this.spaces + this.toSource(expression) + ';';
904
+ }
905
+ }, {
906
+ key: "CallExpression",
907
+ value: function CallExpression(node) {
908
+ var callee = node.callee,
909
+ args = node.arguments,
910
+ extra = node.extra;
911
+ var out = this.toSource(callee);
912
+ out += "(";
913
+ out += this.mapToSource(args).join(', ');
914
+ if (!!(extra !== null && extra !== void 0 && extra.trailingComma)) {
915
+ out += ',';
916
+ }
917
+ out += ")";
918
+ return out;
919
+ }
920
+ }, {
921
+ key: "ObjectExpression",
922
+ value: function ObjectExpression(node) {
923
+ var properties = node.properties,
924
+ extra = node.extra;
925
+ var out = '{';
926
+ if (properties.length) {
927
+ var props = this.mapToSource(properties).join(',\n');
928
+ out += '\n';
929
+ out += props;
930
+ if (extra !== null && extra !== void 0 && extra.trailingComma) {
931
+ out += ',';
932
+ }
933
+ out += '\n';
934
+ out += this.spaces.slice(2);
935
+ }
936
+ out += '}';
937
+ return out;
938
+ }
939
+ }, {
940
+ key: "ObjectProperty",
941
+ value: function ObjectProperty(node) {
942
+ var computed = node.computed,
943
+ key = node.key,
944
+ method = node.method,
945
+ shorthand = node.shorthand,
946
+ value = node.value;
947
+ if (method) {
948
+ console.warn("ObjectProperty> unhandled method property:", {
949
+ method: method,
950
+ shorthand: shorthand
951
+ });
952
+ }
953
+ var spaces = this.spaces;
954
+ var left = this.toSource(key);
955
+ var right = this.toSource(value);
956
+ var out = spaces;
957
+ if (computed) {
958
+ left = "[".concat(left, "]");
959
+ }
960
+ if (!shorthand) {
961
+ out += left + ': ';
962
+ }
963
+ out += right;
964
+ return out;
965
+ }
966
+ }, {
967
+ key: "BooleanLiteral",
968
+ value: function BooleanLiteral(node) {
969
+ return node.value.toString();
970
+ }
971
+ }, {
972
+ key: "AssignmentExpression",
973
+ value: function AssignmentExpression(node) {
974
+ var left = node.left,
975
+ operator = node.operator,
976
+ right = node.right;
977
+ var left_ = this.toSource(left);
978
+ var right_ = this.toSource(right);
979
+ return "".concat(left_, " ").concat(operator, " ").concat(right_);
980
+ }
981
+ }, {
982
+ key: "BinaryExpression",
983
+ value: function BinaryExpression(node) {
984
+ var left = node.left,
985
+ operator = node.operator,
986
+ right = node.right;
987
+ var left_ = this.toSource(left);
988
+ var right_ = this.toSource(right);
989
+ return "".concat(left_, " ").concat(operator, " ").concat(right_);
990
+ }
991
+ }, {
992
+ key: "ThisExpression",
993
+ value: function ThisExpression(node) {
994
+ return 'this';
995
+ }
996
+ }, {
997
+ key: "ArrayExpression",
998
+ value: function ArrayExpression(node) {
999
+ var elements = node.elements,
1000
+ extra = node.extra;
1001
+ var e = !!(extra !== null && extra !== void 0 && extra.trailingComma) ? ',' : '';
1002
+ return '[' + this.mapToSource(elements).join(', ') + e + ']';
1003
+ }
1004
+ }, {
1005
+ key: "VariableDeclaration",
1006
+ value: function VariableDeclaration(node) {
1007
+ var declarations = node.declarations,
1008
+ kind = node.kind;
1009
+ var spaces = '';
1010
+ var semicolon = '';
1011
+ if (this.needSpaces) {
1012
+ semicolon = ';';
1013
+ spaces = this.spaces;
1014
+ }
1015
+ return "".concat(spaces).concat(kind, " ").concat(this.mapToSource(declarations).join(', ')).concat(semicolon);
1016
+ }
1017
+ }, {
1018
+ key: "VariableDeclarator",
1019
+ value: function VariableDeclarator(node) {
1020
+ var id = node.id,
1021
+ init = node.init;
1022
+ if (init) {
1023
+ return this.toSource(id) + ' = ' + this.toSource(init);
1024
+ }
1025
+ return this.toSource(id);
1026
+ }
1027
+ }, {
1028
+ key: "ConditionalExpression",
1029
+ value: function ConditionalExpression(node) {
1030
+ var alternate = node.alternate,
1031
+ consequent = node.consequent,
1032
+ test = node.test;
1033
+ return "".concat(this.toSource(test), " ? ").concat(this.toSource(consequent), " : ").concat(this.toSource(alternate));
1034
+ }
1035
+ }, {
1036
+ key: "LogicalExpression",
1037
+ value: function LogicalExpression(node) {
1038
+ var left = node.left,
1039
+ operator = node.operator,
1040
+ right = node.right;
1041
+ var l = this.toSource(left);
1042
+ var r = this.toSource(right);
1043
+ if (l === '') {
1044
+ console.warn("LogicalExpression> unhandled empty left", node);
1045
+ }
1046
+ if (r === '') {
1047
+ console.warn("LogicalExpression> unhandled empty right", node);
1048
+ }
1049
+ return "".concat(l, " ").concat(operator, " ").concat(r);
1050
+ }
1051
+ }, {
1052
+ key: "ForStatement",
1053
+ value: function ForStatement(node) {
1054
+ var init = node.init,
1055
+ test = node.test,
1056
+ update = node.update,
1057
+ body = node.body;
1058
+ var spaces = this.spaces;
1059
+ var i = this.toSource(init);
1060
+ var t = this.toSource(test);
1061
+ var u = this.toSource(update);
1062
+ var b = this.toSourceCurly(body);
1063
+ return "".concat(spaces, "for (").concat(i, "; ").concat(t, "; ").concat(u, ")").concat(b);
1064
+ }
1065
+ }, {
1066
+ key: "UpdateExpression",
1067
+ value: function UpdateExpression(node) {
1068
+ var operator = node.operator,
1069
+ prefix = node.prefix,
1070
+ argument = node.argument;
1071
+ if (prefix) {
1072
+ return "".concat(operator).concat(this.toSource(argument));
1073
+ } else {
1074
+ return "".concat(this.toSource(argument)).concat(operator);
1075
+ }
1076
+ }
1077
+ }, {
1078
+ key: "NewExpression",
1079
+ value: function NewExpression(node) {
1080
+ var callee = node.callee,
1081
+ args_ = node.arguments;
1082
+ var c = this.toSource(callee);
1083
+ var args = this.mapToSource(args_).join(', ');
1084
+ return "new ".concat(c, "(").concat(args, ")");
1085
+ }
1086
+ }, {
1087
+ key: "ClassDeclaration",
1088
+ value: function ClassDeclaration(node) {
1089
+ var id = node.id,
1090
+ superClass = node.superClass,
1091
+ body = node.body;
1092
+ node.trailingComments;
1093
+ node.leadingComments;
1094
+ var spaces = this.spaces;
1095
+ var out = spaces + 'class ' + this.toSource(id);
1096
+ if (superClass) {
1097
+ out += " extends ".concat(this.toSource(superClass));
1098
+ }
1099
+ out += ' {\n';
1100
+ out += this.toSource(body);
1101
+ out += spaces;
1102
+ out += '\n}\n';
1103
+ return out;
1104
+ }
1105
+ }, {
1106
+ key: "TemplateLiteral",
1107
+ value: function TemplateLiteral(node) {
1108
+ var expressions = node.expressions,
1109
+ quasis = node.quasis;
1110
+ var out = '`';
1111
+ for (var i = 0; i < quasis.length; i++) {
1112
+ out += this.toSource(quasis[i]);
1113
+ if (expressions[i]) {
1114
+ out += '${' + this.toSource(expressions[i]) + '}';
1115
+ }
1116
+ }
1117
+ out += '`';
1118
+ return out;
1119
+ }
1120
+ }, {
1121
+ key: "TemplateElement",
1122
+ value: function TemplateElement(node) {
1123
+ var value = node.value;
1124
+ return value.raw;
1125
+ }
1126
+ }, {
1127
+ key: "ContinueStatement",
1128
+ value: function ContinueStatement(node) {
1129
+ var label = node.label;
1130
+ var out = this.spaces + 'continue';
1131
+ if (label) {
1132
+ out += ' ' + this.toSource(label);
1133
+ }
1134
+ out += ';';
1135
+ return out;
1136
+ }
1137
+ }, {
1138
+ key: "ClassBody",
1139
+ value: function ClassBody(node) {
1140
+ var body = node.body;
1141
+ return this.mapToSource(body).join('\n');
1142
+ }
1143
+ }, {
1144
+ key: "ClassMethod",
1145
+ value: function ClassMethod(node) {
1146
+ var static_ = node.static,
1147
+ key = node.key,
1148
+ computed = node.computed,
1149
+ kind = node.kind,
1150
+ id = node.id,
1151
+ generator = node.generator,
1152
+ async = node.async,
1153
+ params = node.params,
1154
+ body = node.body;
1155
+ var out = this.spaces;
1156
+ if (generator) {
1157
+ out += '*';
1158
+ }
1159
+ if (id) {
1160
+ console.warn('Stringifier#ClassMethod> unhandled id', node);
1161
+ }
1162
+ if (static_) {
1163
+ out += 'static ';
1164
+ }
1165
+ if (async) {
1166
+ out += 'async ';
1167
+ }
1168
+ if (kind === 'get') {
1169
+ out += 'get ';
1170
+ } else if (kind === 'set') {
1171
+ out += 'set ';
1172
+ } else if (kind === 'constructor' || kind === 'method') ; else {
1173
+ console.warn("unhandled kind", kind, "for", node);
1174
+ }
1175
+ var methodName = this.toSource(key);
1176
+ if (computed) {
1177
+ methodName = "[".concat(methodName, "]");
1178
+ }
1179
+ out += methodName;
1180
+ out += "(".concat(this.mapToSource(params).join(', '), ")");
1181
+ out += this.toSource(body);
1182
+ return out;
1183
+ }
1184
+ }, {
1185
+ key: "ExportAllDeclaration",
1186
+ value: function ExportAllDeclaration(node) {
1187
+ return "export * from ".concat(node.source.extra.raw, ";");
1188
+ }
1189
+ }, {
1190
+ key: "ExportNamedDeclaration",
1191
+ value: function ExportNamedDeclaration(node) {
1192
+ var _this2 = this;
1193
+ var declaration = node.declaration,
1194
+ source = node.source,
1195
+ specifiers = node.specifiers;
1196
+ var out = this.spaces;
1197
+ if (specifiers.length) {
1198
+ out += 'export {\n';
1199
+ out += this.mapToSource(specifiers).map(function (_) {
1200
+ return " ".concat(_this2.spaces).concat(_);
1201
+ }).join(',\n');
1202
+ out += '\n';
1203
+ out += this.spaces;
1204
+ out += '}';
1205
+ if (source) {
1206
+ out += " from " + this.toSource(source);
1207
+ }
1208
+ out += ";\n";
1209
+ } else if (declaration) {
1210
+ out += 'export ' + this.toSource(declaration);
1211
+ } else {
1212
+ console.warn("ExportNamedDeclaration> unhandled case for", node);
1213
+ }
1214
+ return out;
1215
+ }
1216
+ }, {
1217
+ key: "ExportDefaultDeclaration",
1218
+ value: function ExportDefaultDeclaration(node) {
1219
+ var declaration = node.declaration;
1220
+ var a = this.toSource(declaration);
1221
+ return "export default ".concat(a, ";");
1222
+ }
1223
+ }, {
1224
+ key: "ExportSpecifier",
1225
+ value: function ExportSpecifier(node) {
1226
+ var exported = node.exported,
1227
+ local = node.local;
1228
+ var left = this.toSource(exported);
1229
+ var right = this.toSource(local);
1230
+ var spaces = this.spaces;
1231
+ if (left === right) {
1232
+ return "".concat(spaces).concat(left);
1233
+ }
1234
+ return "".concat(spaces).concat(right, " as ").concat(left);
1235
+ }
1236
+ }, {
1237
+ key: "Super",
1238
+ value: function Super(node) {
1239
+ return 'super';
1240
+ }
1241
+ }, {
1242
+ key: "ForInStatement",
1243
+ value: function ForInStatement(node) {
1244
+ var left = node.left,
1245
+ right = node.right,
1246
+ body = node.body;
1247
+ var spaces = this.spaces;
1248
+ var l = this.toSource(left);
1249
+ var r = this.toSource(right);
1250
+ var b = this.toSourceCurly(body);
1251
+ return "".concat(spaces, "for (").concat(l, " in ").concat(r, ")").concat(b);
1252
+ }
1253
+ }, {
1254
+ key: "ThrowStatement",
1255
+ value: function ThrowStatement(node) {
1256
+ var argument = node.argument;
1257
+ var spaces = this.spaces;
1258
+ var arg = this.toSource(argument);
1259
+ return "".concat(spaces, "throw ").concat(arg, ";");
1260
+ }
1261
+ }, {
1262
+ key: "WhileStatement",
1263
+ value: function WhileStatement(node) {
1264
+ var test = node.test,
1265
+ body = node.body;
1266
+ var spaces = this.spaces;
1267
+ var t = this.toSource(test);
1268
+ var b = this.toSourceCurly(body);
1269
+ return "".concat(spaces, "while (").concat(t, ")").concat(b);
1270
+ }
1271
+ }, {
1272
+ key: "BreakStatement",
1273
+ value: function BreakStatement(node) {
1274
+ var label = node.label;
1275
+ var out = this.spaces + 'break';
1276
+ if (label) {
1277
+ out += ' ' + this.toSource(label);
1278
+ }
1279
+ out += ';';
1280
+ return out;
1281
+ }
1282
+ }, {
1283
+ key: "ForOfStatement",
1284
+ value: function ForOfStatement(node) {
1285
+ var await_ = node.await,
1286
+ left = node.left,
1287
+ right = node.right,
1288
+ body = node.body;
1289
+ var spaces = this.spaces;
1290
+ var l = this.toSource(left);
1291
+ var r = this.toSource(right);
1292
+ var b = this.toSourceCurly(body);
1293
+ var a = await_ ? 'await ' : '';
1294
+ return "".concat(spaces, "for ").concat(a, "(").concat(l, " of ").concat(r, ")").concat(b);
1295
+ }
1296
+ }, {
1297
+ key: "ArrayPattern",
1298
+ value: function ArrayPattern(node) {
1299
+ var elements = node.elements;
1300
+ var e = this.mapToSource(elements).join(', ');
1301
+ return "[".concat(e, "]");
1302
+ }
1303
+ }, {
1304
+ key: "SwitchStatement",
1305
+ value: function SwitchStatement(node) {
1306
+ var discriminant = node.discriminant,
1307
+ cases = node.cases;
1308
+ var spaces = this.spaces;
1309
+ var out = '';
1310
+ out += spaces + "switch (".concat(this.toSource(discriminant), ") {\n");
1311
+ out += this.mapToSource(cases).join('\n') + '\n';
1312
+ out += spaces + '}';
1313
+ return out;
1314
+ }
1315
+ }, {
1316
+ key: "SwitchCase",
1317
+ value: function SwitchCase(node) {
1318
+ var consequent = node.consequent,
1319
+ test = node.test;
1320
+ var spaces = this.spaces;
1321
+ var c = this.mapToSource(consequent).join('\n');
1322
+ if (test) {
1323
+ var t = this.toSource(test);
1324
+ return "".concat(spaces, "case ").concat(t, ":\n").concat(c);
1325
+ }
1326
+ return "".concat(spaces, "default:\n").concat(c);
1327
+ }
1328
+ }, {
1329
+ key: "RegExpLiteral",
1330
+ value: function RegExpLiteral(node) {
1331
+ var extra = node.extra;
1332
+ node.value;
1333
+ node.pattern;
1334
+ node.flags;
1335
+ return extra.raw;
1336
+ }
1337
+ }, {
1338
+ key: "SequenceExpression",
1339
+ value: function SequenceExpression(node) {
1340
+ var expressions = node.expressions;
1341
+ return this.mapToSource(expressions).join(', ');
1342
+ }
1343
+ }, {
1344
+ key: "EmptyStatement",
1345
+ value: function EmptyStatement(node) {
1346
+ return ';';
1347
+ }
1348
+ }, {
1349
+ key: "SpreadElement",
1350
+ value: function SpreadElement(node) {
1351
+ var argument = node.argument;
1352
+ var a = this.toSource(argument);
1353
+ return "...".concat(a);
1354
+ }
1355
+ }, {
1356
+ key: "ObjectMethod",
1357
+ value: function ObjectMethod(node) {
1358
+ node.method;
1359
+ var key = node.key,
1360
+ computed = node.computed,
1361
+ kind = node.kind,
1362
+ id = node.id,
1363
+ generator = node.generator,
1364
+ async = node.async,
1365
+ params = node.params,
1366
+ body = node.body;
1367
+ if (id !== null) {
1368
+ console.warn("ObjectMethod> id !== null is unhandled", node);
1369
+ debugger;
1370
+ }
1371
+ var out = this.spaces;
1372
+ if (generator) {
1373
+ out += '*';
1374
+ }
1375
+ if (id) {
1376
+ console.warn('Stringifier#ClassMethod> unhandled id', node);
1377
+ }
1378
+ if (async) {
1379
+ out += 'async ';
1380
+ }
1381
+ if (kind === 'get') {
1382
+ out += 'get ';
1383
+ } else if (kind === 'set') {
1384
+ out += 'set ';
1385
+ } else if (kind === 'method') ; else {
1386
+ console.warn("unhandled kind", kind, "for", node);
1387
+ }
1388
+ var methodName = this.toSource(key);
1389
+ if (computed) {
1390
+ methodName = "[".concat(methodName, "]");
1391
+ }
1392
+ out += methodName;
1393
+ out += "(".concat(this.mapToSource(params).join(', '), ")");
1394
+ out += this.toSource(body);
1395
+ return out;
1396
+ }
1397
+ }, {
1398
+ key: "ObjectPattern",
1399
+ value: function ObjectPattern(node) {
1400
+ var properties = node.properties,
1401
+ extra = node.extra;
1402
+ var out = '{';
1403
+ if (properties.length) {
1404
+ out += '\n';
1405
+ out += this.mapToSource(properties).join(',\n');
1406
+ if (!!(extra !== null && extra !== void 0 && extra.trailingComma)) {
1407
+ out += ',';
1408
+ }
1409
+ out += '\n';
1410
+ out += this.spaces.slice(2);
1411
+ }
1412
+ out += '}';
1413
+ return out;
1414
+ }
1415
+ }, {
1416
+ key: "OptionalMemberExpression",
1417
+ value: function OptionalMemberExpression(node) {
1418
+ var object = node.object,
1419
+ computed = node.computed,
1420
+ property = node.property,
1421
+ optional = node.optional;
1422
+ var a = this.toSource(object);
1423
+ var b = this.toSource(property);
1424
+ if (!computed && !optional) {
1425
+ return "".concat(a, ".").concat(b);
1426
+ } else if (!computed && optional) {
1427
+ return "".concat(a, "?.").concat(b);
1428
+ } else if (computed && !optional) {
1429
+ return "".concat(a, "[").concat(b, "]");
1430
+ }
1431
+ return "".concat(a, "?.[").concat(b, "]");
1432
+ }
1433
+ }, {
1434
+ key: "OptionalCallExpression",
1435
+ value: function OptionalCallExpression(node) {
1436
+ var callee = node.callee,
1437
+ optional = node.optional,
1438
+ args = node.arguments;
1439
+ var a = this.toSource(callee);
1440
+ var b = this.mapToSource(args);
1441
+ if (optional) {
1442
+ return "".concat(a, "?.(").concat(b, ")");
1443
+ }
1444
+ return "".concat(a, "(").concat(b, ")");
1445
+ }
1446
+ }, {
1447
+ key: "DoWhileStatement",
1448
+ value: function DoWhileStatement(node) {
1449
+ var body = node.body,
1450
+ test = node.test;
1451
+ node.trailingComments;
1452
+ var spaces = this.spaces;
1453
+ var out = spaces;
1454
+ out += "do ";
1455
+ out += this.toSourceCurly(body);
1456
+ out += " while (".concat(this.toSource(test), ");");
1457
+ return out;
1458
+ }
1459
+ }, {
1460
+ key: "TryStatement",
1461
+ value: function TryStatement(node) {
1462
+ var block = node.block,
1463
+ handler = node.handler,
1464
+ finalizer = node.finalizer;
1465
+ var out = this.spaces;
1466
+ var b = this.toSource(block);
1467
+ out += "try ".concat(b);
1468
+ if (handler) {
1469
+ out += this.toSource(handler);
1470
+ }
1471
+ if (finalizer) {
1472
+ out += 'finally' + this.toSource(finalizer);
1473
+ }
1474
+ return out;
1475
+ }
1476
+ }, {
1477
+ key: "CatchClause",
1478
+ value: function CatchClause(node) {
1479
+ var param = node.param,
1480
+ body = node.body;
1481
+ var b = this.toSource(body);
1482
+ if (param) {
1483
+ var p = this.toSource(param);
1484
+ return " catch (".concat(p, ") ").concat(b);
1485
+ }
1486
+ return " catch ".concat(b);
1487
+ }
1488
+ }, {
1489
+ key: "RestElement",
1490
+ value: function RestElement(node) {
1491
+ var argument = node.argument;
1492
+ return "...".concat(this.toSource(argument));
1493
+ }
1494
+ }, {
1495
+ key: "DebuggerStatement",
1496
+ value: function DebuggerStatement(node) {
1497
+ return "".concat(this.spaces, "debugger;");
1498
+ }
1499
+ }, {
1500
+ key: "CommentBlock",
1501
+ value: function CommentBlock(node) {
1502
+ var value = node.value;
1503
+ var par = this.parents[this.parents.length - 3];
1504
+ var out = "".concat(this.spaces.slice(2), "/*").concat(value, "*/");
1505
+ if ((par === null || par === void 0 ? void 0 : par.type) === 'BlockStatement' || (par === null || par === void 0 ? void 0 : par.type) === 'Program') {
1506
+ return out + '\n';
1507
+ }
1508
+ return ' ' + out + ' ';
1509
+ }
1510
+ }, {
1511
+ key: "CommentLine",
1512
+ value: function CommentLine(node) {
1513
+ var value = node.value;
1514
+ return "".concat(this.spaces.slice(2), "//").concat(value, "\n");
1515
+ }
1516
+ }, {
1517
+ key: "Import",
1518
+ value: function Import(node) {
1519
+ return 'import';
1520
+ }
1521
+ }, {
1522
+ key: "ImportDeclaration",
1523
+ value: function ImportDeclaration(node) {
1524
+ var specifiers = node.specifiers,
1525
+ source = node.source;
1526
+ var a = this.mapToSource(specifiers).join(', ');
1527
+ var b = this.toSource(source);
1528
+ if (specifiers.length === 0) {
1529
+ return "import ".concat(b);
1530
+ }
1531
+ if (specifiers[0].type !== 'ImportSpecifier') {
1532
+ return "import ".concat(a, " from ").concat(b, ";");
1533
+ }
1534
+ return "import {".concat(a, "} from ").concat(b, ";");
1535
+ }
1536
+ }, {
1537
+ key: "ImportSpecifier",
1538
+ value: function ImportSpecifier(node) {
1539
+ var imported = node.imported,
1540
+ local = node.local;
1541
+ var a = this.toSource(imported);
1542
+ var b = this.toSource(local);
1543
+ if (a !== b) {
1544
+ return "".concat(a, " as ").concat(b);
1545
+ }
1546
+ return a;
1547
+ }
1548
+ }, {
1549
+ key: "ImportDefaultSpecifier",
1550
+ value: function ImportDefaultSpecifier(node) {
1551
+ var local = node.local;
1552
+ return this.toSource(local);
1553
+ }
1554
+ }, {
1555
+ key: "ImportNamespaceSpecifier",
1556
+ value: function ImportNamespaceSpecifier(node) {
1557
+ var local = node.local;
1558
+ return "* as ".concat(this.toSource(local));
1559
+ }
1560
+ }, {
1561
+ key: "File",
1562
+ value: function File(node) {
1563
+ return this.toSource(node.program) + '\n';
1564
+ }
1565
+ }, {
1566
+ key: "Program",
1567
+ value: function Program(node) {
1568
+ node.sourceType;
1569
+ node.interpreter;
1570
+ var body = node.body,
1571
+ directives = node.directives;
1572
+ var out = '';
1573
+ out += this.mapToSource(directives);
1574
+ out += this.mapToSource(body).join('\n');
1575
+ return out;
1576
+ }
1577
+ }, {
1578
+ key: "StringLiteral",
1579
+ value: function StringLiteral(node) {
1580
+ var extra = node.extra;
1581
+ if (!extra) {
1582
+ debugger;
1583
+ return '';
1584
+ }
1585
+ if (typeof extra.raw !== 'string') {
1586
+ debugger;
1587
+ return '';
1588
+ }
1589
+ return extra.raw;
1590
+ }
1591
+ }, {
1592
+ key: "NumericLiteral",
1593
+ value: function NumericLiteral(node) {
1594
+ return node.extra.raw;
1595
+ }
1596
+ }, {
1597
+ key: "AssignmentPattern",
1598
+ value: function AssignmentPattern(node) {
1599
+ var left = node.left,
1600
+ right = node.right;
1601
+ return "".concat(this.toSource(left), " = ").concat(this.toSource(right));
1602
+ }
1603
+ }, {
1604
+ key: "NullLiteral",
1605
+ value: function NullLiteral(node) {
1606
+ return 'null';
1607
+ }
1608
+ }, {
1609
+ key: "TaggedTemplateExpression",
1610
+ value: function TaggedTemplateExpression(node) {
1611
+ var tag = node.tag,
1612
+ quasi = node.quasi;
1613
+ var t = this.toSource(tag);
1614
+ var q = this.toSource(quasi);
1615
+ return t + q;
1616
+ }
1617
+ }, {
1618
+ key: "YieldExpression",
1619
+ value: function YieldExpression(node) {
1620
+ var delegate = node.delegate,
1621
+ argument = node.argument;
1622
+ var keyword = 'yield';
1623
+ if (delegate) {
1624
+ keyword += '*';
1625
+ }
1626
+ var a = this.toSource(argument);
1627
+ return "".concat(keyword, " ").concat(a);
1628
+ }
1629
+ }]);
1630
+ return Stringifier;
1631
+ }();
1632
+
1633
+ var Asserter = function (_Stringifier) {
1634
+ _inherits(Asserter, _Stringifier);
1635
+ var _super = _createSuper(Asserter);
1636
+ function Asserter() {
1637
+ var _this;
1638
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
1639
+ _ref$forceCurly = _ref.forceCurly,
1640
+ forceCurly = _ref$forceCurly === void 0 ? true : _ref$forceCurly,
1641
+ _ref$validateDivision = _ref.validateDivision,
1642
+ validateDivision = _ref$validateDivision === void 0 ? true : _ref$validateDivision,
1643
+ _ref$expandType = _ref.expandType,
1644
+ expandType = _ref$expandType === void 0 ? expandTypeDepFree : _ref$expandType,
1645
+ filename = _ref.filename;
1646
+ _classCallCheck(this, Asserter);
1647
+ _this = _super.call(this);
1648
+ _defineProperty(_assertThisInitialized(_this), "stats", {
1649
+ 'FunctionDeclaration': {
1650
+ checked: 0,
1651
+ unchecked: 0
1652
+ },
1653
+ 'FunctionExpression': {
1654
+ checked: 0,
1655
+ unchecked: 0
1656
+ },
1657
+ 'ArrowFunctionExpression': {
1658
+ checked: 0,
1659
+ unchecked: 0
1660
+ },
1661
+ 'ClassMethod#constructor': {
1662
+ checked: 0,
1663
+ unchecked: 0
1664
+ },
1665
+ 'ClassMethod#method': {
1666
+ checked: 0,
1667
+ unchecked: 0
1668
+ },
1669
+ 'ClassMethod#set': {
1670
+ checked: 0,
1671
+ unchecked: 0
1672
+ },
1673
+ 'ClassMethod#get': {
1674
+ checked: 0,
1675
+ unchecked: 0
1676
+ }
1677
+ });
1678
+ _defineProperty(_assertThisInitialized(_this), "typedefs", {});
1679
+ _this.forceCurly = forceCurly;
1680
+ _this.validateDivision = validateDivision;
1681
+ _this.expandType = expandType;
1682
+ _this.filename = filename;
1683
+ return _this;
1684
+ }
1685
+ _createClass(Asserter, [{
1686
+ key: "ArrowFunctionExpression",
1687
+ value: function ArrowFunctionExpression(node) {
1688
+ var async = node.async,
1689
+ body = node.body,
1690
+ generator = node.generator,
1691
+ params = node.params;
1692
+ var out = '';
1693
+ if (async) {
1694
+ out += 'async ';
1695
+ }
1696
+ if (generator) {
1697
+ out += ' * ';
1698
+ }
1699
+ out += this.FunctionDeclarationParams(params);
1700
+ out += ' =>';
1701
+ if (body.type === 'BlockStatement') {
1702
+ out += this.toSource(body);
1703
+ } else {
1704
+ out += ' {\n';
1705
+ out += this.generateTypeChecks(node);
1706
+ out += this.spaces + 'return ' + this.toSource(body) + ';\n';
1707
+ out += '}';
1708
+ }
1709
+ return out;
1710
+ }
1711
+ }, {
1712
+ key: "ClassDeclaration",
1713
+ value: function ClassDeclaration(node) {
1714
+ var id = node.id;
1715
+ var id_ = this.toSource(id);
1716
+ var out = _get(_getPrototypeOf(Asserter.prototype), "ClassDeclaration", this).call(this, node);
1717
+ out += "".concat(this.spaces, "registerClass(").concat(id_, ");");
1718
+ return out;
1719
+ }
1720
+ }, {
1721
+ key: "findParentOfType",
1722
+ value: function findParentOfType(node, type) {
1723
+ var currentIndex = this.parents.findLastIndex(function (_) {
1724
+ return _ == node;
1725
+ });
1726
+ return this.parents.findLast(function (_, i) {
1727
+ if (i > currentIndex) {
1728
+ return false;
1729
+ }
1730
+ return _.type === type;
1731
+ });
1732
+ }
1733
+ }, {
1734
+ key: "findParentOfTypeWithPredicate",
1735
+ value: function findParentOfTypeWithPredicate(node, type, predicate) {
1736
+ var parents = this.parents;
1737
+ var i = parents.findLastIndex(function (_) {
1738
+ return _ == node;
1739
+ });
1740
+ i--;
1741
+ while (i >= 0) {
1742
+ var parent = parents[i];
1743
+ if (predicate(parent)) {
1744
+ return;
1745
+ }
1746
+ if (parent.type === type) {
1747
+ return parent;
1748
+ }
1749
+ i--;
1750
+ }
1751
+ }
1752
+ }, {
1753
+ key: "warn",
1754
+ value: function warn() {
1755
+ var _console;
1756
+ if (this.filename) {
1757
+ console.warn("[WARN]", this.filename);
1758
+ }
1759
+ (_console = console).warn.apply(_console, arguments);
1760
+ }
1761
+ }, {
1762
+ key: "getJSDoc",
1763
+ value: function getJSDoc(node) {
1764
+ if (node.type === 'BlockStatement') {
1765
+ node = this.parent;
1766
+ }
1767
+ if (node.type === 'FunctionExpression') {
1768
+ if (node.leadingComments) {
1769
+ this.warn("Case of FunctionExpression containing its own leadingComments is not handled");
1770
+ return;
1771
+ }
1772
+ node = this.findParentOfType(node, 'ExpressionStatement');
1773
+ if (!node) {
1774
+ node = this.parents.findLast(function (_) {
1775
+ return _.type == 'VariableDeclaration';
1776
+ });
1777
+ if (!node) {
1778
+ return;
1779
+ }
1780
+ }
1781
+ }
1782
+ var _node = node,
1783
+ leadingComments = _node.leadingComments;
1784
+ if (!leadingComments) {
1785
+ if (node.type === 'FunctionDeclaration') {
1786
+ var exportNamedDeclaration = this.findParentOfType(node, 'ExportNamedDeclaration');
1787
+ leadingComments = exportNamedDeclaration === null || exportNamedDeclaration === void 0 ? void 0 : exportNamedDeclaration.leadingComments;
1788
+ }
1789
+ if (node.type === 'ArrowFunctionExpression') {
1790
+ var _tmp, _tmp2;
1791
+ var tmp = this.findParentOfType(node, 'VariableDeclaration');
1792
+ if (!((_tmp = tmp) !== null && _tmp !== void 0 && _tmp.leadingComments)) {
1793
+ function isFunc(node) {
1794
+ return node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration';
1795
+ }
1796
+ tmp = this.findParentOfTypeWithPredicate(node, 'ExportNamedDeclaration', isFunc);
1797
+ }
1798
+ leadingComments = (_tmp2 = tmp) === null || _tmp2 === void 0 ? void 0 : _tmp2.leadingComments;
1799
+ }
1800
+ }
1801
+ if (leadingComments && leadingComments.length) {
1802
+ var lastComment = leadingComments[leadingComments.length - 1];
1803
+ if (lastComment.type == "CommentBlock") {
1804
+ if (lastComment.value.includes('@event')) {
1805
+ return;
1806
+ }
1807
+ if (node.type === 'ClassMethod' && node.kind === 'set') {
1808
+ var paramName = this.getNameOfParam(node.params[0]);
1809
+ if (node.params.length !== 1) {
1810
+ this.warn("getJSDoc> setters require exactly one argument");
1811
+ }
1812
+ return _defineProperty({}, paramName, parseJSDocSetter(lastComment.value, this.expandType));
1813
+ }
1814
+ return parseJSDoc(lastComment.value, this.expandType);
1815
+ }
1816
+ }
1817
+ }
1818
+ }, {
1819
+ key: "getNameOfParam",
1820
+ value: function getNameOfParam(param) {
1821
+ if (param.type === 'Identifier') {
1822
+ return param.name;
1823
+ } else if (param.type === 'AssignmentPattern') {
1824
+ if (param.left.type === 'Identifier') {
1825
+ return param.left.name;
1826
+ }
1827
+ }
1828
+ debugger;
1829
+ this.warn("unable to extra name from param in specified way - may contain too much information");
1830
+ return this.toSource(param);
1831
+ }
1832
+ }, {
1833
+ key: "statsReset",
1834
+ value: function statsReset() {
1835
+ Object.values(this.stats).forEach(statReset);
1836
+ }
1837
+ }, {
1838
+ key: "statsPrint",
1839
+ value: function statsPrint() {
1840
+ console.table(this.stats);
1841
+ }
1842
+ }, {
1843
+ key: "getStatsForNode",
1844
+ value: function getStatsForNode(node) {
1845
+ var parentType = this.parentType,
1846
+ stats = this.stats;
1847
+ if (parentType == 'ClassMethod') {
1848
+ var kind = this.parent.kind;
1849
+ return stats["ClassMethod#".concat(kind)];
1850
+ } else if (node.type === 'ArrowFunctionExpression') {
1851
+ return stats.ArrowFunctionExpression;
1852
+ }
1853
+ var stat = stats[parentType];
1854
+ if (!stat) {
1855
+ this.warn("getStatsForNode> dummy, but unhandled... fix for node type", node);
1856
+ return {
1857
+ checked: 0,
1858
+ unchecked: 0
1859
+ };
1860
+ }
1861
+ return stat;
1862
+ }
1863
+ }, {
1864
+ key: "nodeHasParamName",
1865
+ value: function nodeHasParamName(node, name) {
1866
+ if (node.type === 'BlockStatement') {
1867
+ node = this.parent;
1868
+ }
1869
+ var _node2 = node,
1870
+ params = _node2.params;
1871
+ if (!params) {
1872
+ this.warn("nodeHasParamName> Expected params for", {
1873
+ node: node,
1874
+ name: name
1875
+ });
1876
+ return false;
1877
+ }
1878
+ return params.some(function (node) {
1879
+ var type = node.type;
1880
+ if (type === "AssignmentPattern") {
1881
+ var left = node.left;
1882
+ console.assert(left.type === 'Identifier' || left.type === 'ObjectPattern' || left.type === 'ArrayPattern' || left.type === 'AssignmentPattern', 'Expected Identifier or ObjectPattern');
1883
+ return left.name === name;
1884
+ } else if (type === 'Identifier') {
1885
+ return node.name === name;
1886
+ } else if (type === 'ArrayPattern' || type === 'ObjectPattern' || type === 'RestElement') {
1887
+ return false;
1888
+ } else {
1889
+ var _ = new Stringifier();
1890
+ var code = _.toSource(node);
1891
+ console.log("Unknown type to test params for", type, code);
1892
+ }
1893
+ return false;
1894
+ });
1895
+ }
1896
+ }, {
1897
+ key: "generateTypeChecks",
1898
+ value: function generateTypeChecks(node) {
1899
+ var _this2 = this;
1900
+ var parentType = this.parentType;
1901
+ if (node.type === 'BlockStatement' && parentType != 'FunctionDeclaration' && parentType != 'ClassMethod' && parentType != 'FunctionExpression') {
1902
+ return '';
1903
+ }
1904
+ var jsdoc = this.getJSDoc(node);
1905
+ var stat = this.getStatsForNode(node);
1906
+ if (!jsdoc) {
1907
+ stat.unchecked++;
1908
+ return '';
1909
+ }
1910
+ stat.checked++;
1911
+ var spaces = this.spaces;
1912
+ if (node.type !== 'ArrowFunctionExpression') {
1913
+ spaces += ' ';
1914
+ }
1915
+ var out = '';
1916
+ var _loop = function _loop(name) {
1917
+ var type = jsdoc[name];
1918
+ var hasParam = _this2.nodeHasParamName(node, name);
1919
+ if (!hasParam) {
1920
+ var testNode = node;
1921
+ if (node.type === 'BlockStatement') {
1922
+ testNode = _this2.parent;
1923
+ }
1924
+ var paramIndex = Object.keys(jsdoc).findIndex(function (_) {
1925
+ return _ === name;
1926
+ });
1927
+ var param = testNode.params[paramIndex];
1928
+ if (param) {
1929
+ var isObjectPattern = param.type === 'ObjectPattern';
1930
+ var isArrayPattern = param.type === 'ArrayPattern';
1931
+ var isSupportedPattern = isObjectPattern || isArrayPattern;
1932
+ if (isSupportedPattern) {
1933
+ name = "arguments[".concat(paramIndex, "]");
1934
+ } else if (param.type === 'AssignmentPattern') {
1935
+ var _loc = _this2.getName(node);
1936
+ if (param.left.type === 'ArrayPattern' && type.type === 'array') {
1937
+ var _iterator = _createForOfIteratorHelper(param.left.elements),
1938
+ _step;
1939
+ try {
1940
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1941
+ var element = _step.value;
1942
+ if (element.type !== 'Identifier') {
1943
+ _this2.warn('Only Identifier case handled right now');
1944
+ continue;
1945
+ }
1946
+ var _t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
1947
+ out += "".concat(spaces, "if (!assertType(").concat(element.name, ", ").concat(_t, ", '").concat(_loc, "', '").concat(name, "')) {\n");
1948
+ out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
1949
+ }
1950
+ } catch (err) {
1951
+ _iterator.e(err);
1952
+ } finally {
1953
+ _iterator.f();
1954
+ }
1955
+ return 0;
1956
+ } else if (param.left.type === 'ObjectPattern' && type.type === 'object') {
1957
+ var _iterator2 = _createForOfIteratorHelper(param.left.properties),
1958
+ _step2;
1959
+ try {
1960
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1961
+ var property = _step2.value;
1962
+ if (property.key.type !== 'Identifier') {
1963
+ _this2.warn('ObjectPattern> Only Identifier case handled right now');
1964
+ continue;
1965
+ }
1966
+ var keyName = property.key.name;
1967
+ var subType = type.properties[keyName];
1968
+ if (!subType) {
1969
+ _this2.warn("missing subtype information in JSDoc");
1970
+ continue;
1971
+ }
1972
+ var _t2 = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
1973
+ out += "".concat(spaces, "if (!assertType(").concat(keyName, ", ").concat(_t2, ", '").concat(_loc, "', '").concat(name, "')) {\n");
1974
+ out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
1975
+ }
1976
+ } catch (err) {
1977
+ _iterator2.e(err);
1978
+ } finally {
1979
+ _iterator2.f();
1980
+ }
1981
+ return 0;
1982
+ }
1983
+ _this2.warn("generateTypeChecks> ".concat(_loc, "> todo implement"), "AssignmentPattern for parameter ".concat(name));
1984
+ return 0;
1985
+ }
1986
+ } else {
1987
+ var _loc2 = _this2.getName(node);
1988
+ _this2.warn("generateTypeChecks> ".concat(_loc2, "> Missing param: ").concat(name));
1989
+ return 0;
1990
+ }
1991
+ }
1992
+ var t = JSON.stringify(type, null, 2).replaceAll('\n', '\n' + spaces);
1993
+ if (type === 'this') {
1994
+ var classDecl = _this2.findParentOfType(node, 'ClassDeclaration');
1995
+ if (!(classDecl !== null && classDecl !== void 0 && classDecl.id)) {
1996
+ _this2.warn('generateTypeChecks> !classDecl?.id');
1997
+ }
1998
+ t = '"' + _this2.toSource(classDecl.id) + '"';
1999
+ }
2000
+ var loc = _this2.getName(node);
2001
+ var prevCheck = '';
2002
+ if (loc === 'ContactPoint#constructor' || loc === 'ContactResult#constructor' || loc === 'SingleContactResult#constructor') {
2003
+ prevCheck = 'arguments.length !== 0 && ';
2004
+ }
2005
+ out += "".concat(spaces, "if (").concat(prevCheck, "!assertType(").concat(name, ", ").concat(t, ", '").concat(loc, "', '").concat(name, "')) {\n");
2006
+ out += "".concat(spaces, " youCanAddABreakpointHere();\n").concat(spaces, "}\n");
2007
+ },
2008
+ _ret;
2009
+ for (var name in jsdoc) {
2010
+ _ret = _loop(name);
2011
+ if (_ret === 0) continue;
2012
+ }
2013
+ return out;
2014
+ }
2015
+ }, {
2016
+ key: "getName",
2017
+ value: function getName(node) {
2018
+ var toSource = this.toSource.bind(this);
2019
+ if (node.type === 'BlockStatement') {
2020
+ node = this.parent;
2021
+ }
2022
+ var _node3 = node,
2023
+ type = _node3.type,
2024
+ key = _node3.key,
2025
+ id = _node3.id;
2026
+ switch (type) {
2027
+ case 'FunctionDeclaration':
2028
+ return toSource(id);
2029
+ case "ClassMethod":
2030
+ var classDecl = this.parents.findLast(function (_) {
2031
+ return _.type == 'ClassDeclaration';
2032
+ });
2033
+ var out = '';
2034
+ if (classDecl) {
2035
+ out += toSource(classDecl.id) + '#';
2036
+ }
2037
+ out += toSource(key);
2038
+ return out;
2039
+ case 'FunctionExpression':
2040
+ if (id) {
2041
+ return toSource(id);
2042
+ }
2043
+ var expressionStatement = this.findParentOfType(node, 'ExpressionStatement');
2044
+ if (!expressionStatement) {
2045
+ return "unnamed function expression";
2046
+ }
2047
+ return toSource(expressionStatement.expression.left);
2048
+ case 'ArrowFunctionExpression':
2049
+ var parent = this.findParentOfType(node, 'VariableDeclarator');
2050
+ if (parent) {
2051
+ return toSource(parent.id);
2052
+ } else {
2053
+ return "getName> missing parent for " + node.type;
2054
+ }
2055
+ default:
2056
+ this.warn("getName> unhandled type", type, "for", node);
2057
+ return '/*MISSING*/';
2058
+ }
2059
+ }
2060
+ }, {
2061
+ key: "BinaryExpression",
2062
+ value: function BinaryExpression(node) {
2063
+ if (!this.validateDivision) {
2064
+ return _get(_getPrototypeOf(Asserter.prototype), "BinaryExpression", this).call(this, node);
2065
+ }
2066
+ var left = node.left,
2067
+ operator = node.operator,
2068
+ right = node.right;
2069
+ var left_ = this.toSource(left);
2070
+ var right_ = this.toSource(right);
2071
+ if (operator === '/') {
2072
+ return "validateDivision(".concat(left_, ", ").concat(right_, ")");
2073
+ }
2074
+ return "".concat(left_, " ").concat(operator, " ").concat(right_);
2075
+ }
2076
+ }, {
2077
+ key: "File",
2078
+ value: function File(node) {
2079
+ node.errors;
2080
+ var program = node.program,
2081
+ comments = node.comments;
2082
+ if (comments) {
2083
+ var _iterator3 = _createForOfIteratorHelper(comments),
2084
+ _step3;
2085
+ try {
2086
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
2087
+ var comment = _step3.value;
2088
+ var warn = this.warn.bind(this);
2089
+ parseJSDocTypedef(this.typedefs, warn, comment, this.expandType);
2090
+ }
2091
+ } catch (err) {
2092
+ _iterator3.e(err);
2093
+ } finally {
2094
+ _iterator3.f();
2095
+ }
2096
+ }
2097
+ var out = '';
2098
+ for (var name in this.typedefs) {
2099
+ var typedef = this.typedefs[name];
2100
+ var json = JSON.stringify(typedef, null, 2);
2101
+ out += "registerTypedef('".concat(name, "', ").concat(json, ");\n");
2102
+ }
2103
+ var code = this.toSource(program) + '\n';
2104
+ out += code;
2105
+ return out;
2106
+ }
2107
+ }]);
2108
+ return Asserter;
2109
+ }(Stringifier);
2110
+
2111
+ function addTypeChecks(src, options) {
2112
+ try {
2113
+ var asserter = new Asserter(options);
2114
+ var ast = parser.parse(src, {
2115
+ sourceType: 'module'
2116
+ });
2117
+ var out = asserter.toSource(ast);
2118
+ return out;
2119
+ } catch (e) {
2120
+ console.error(e);
2121
+ return "/*<addTypeChecks-error>*/".concat(src, "/*</addTypeChecks-error>*/");
2122
+ }
2123
+ }
2124
+
2125
+ function ast2json(ast) {
2126
+ return JSON.stringify(ast, function (name, val) {
2127
+ if (name == "loc" || name == "start" || name == "end") {
2128
+ return undefined;
2129
+ }
2130
+ return val;
2131
+ }, 2);
2132
+ }
2133
+
2134
+ var drop = ['loc', 'start', 'end', 'leadingComments', 'trailingComments', 'innerComments', 'innerComments', 'comments'];
2135
+ function ast2jsonForComparison(ast) {
2136
+ return JSON.stringify(ast, function (name, val) {
2137
+ if (name === 'trailingComma' || name === 'parenStart') {
2138
+ return 'offset removed for better comparison';
2139
+ }
2140
+ if (drop.includes(name)) {
2141
+ return undefined;
2142
+ }
2143
+ return val;
2144
+ }, 2);
2145
+ }
2146
+
2147
+ function code2ast2code(code) {
2148
+ var stringifier = new Stringifier();
2149
+ var ast = parser.parse(code, {
2150
+ sourceType: 'module'
2151
+ });
2152
+ if (!ast) {
2153
+ return;
2154
+ }
2155
+ var out = stringifier.toSource(ast);
2156
+ return out;
2157
+ }
2158
+
2159
+ function expandType(type) {
2160
+ var ast = parseType(type);
2161
+ return toSourceTS(ast);
2162
+ }
2163
+ function parseType(str) {
2164
+ if (str.startsWith('...')) {
2165
+ str = str.slice(3);
2166
+ str += '[]';
2167
+ }
2168
+ str = "type tmp = ".concat(str, ";");
2169
+ var ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, true);
2170
+ return ast.statements[0].type;
2171
+ }
2172
+ function toSourceTS(node) {
2173
+ var typeArguments = node.typeArguments,
2174
+ typeName = node.typeName;
2175
+ var kind_ = ts.SyntaxKind[node.kind];
2176
+ var _ts$SyntaxKind = ts.SyntaxKind,
2177
+ AnyKeyword = _ts$SyntaxKind.AnyKeyword,
2178
+ ArrayType = _ts$SyntaxKind.ArrayType,
2179
+ BooleanKeyword = _ts$SyntaxKind.BooleanKeyword,
2180
+ FunctionType = _ts$SyntaxKind.FunctionType,
2181
+ Identifier = _ts$SyntaxKind.Identifier,
2182
+ IntersectionType = _ts$SyntaxKind.IntersectionType,
2183
+ JSDocAllType = _ts$SyntaxKind.JSDocAllType,
2184
+ LastTypeNode = _ts$SyntaxKind.LastTypeNode,
2185
+ LiteralType = _ts$SyntaxKind.LiteralType,
2186
+ NullKeyword = _ts$SyntaxKind.NullKeyword,
2187
+ NumberKeyword = _ts$SyntaxKind.NumberKeyword,
2188
+ NumericLiteral = _ts$SyntaxKind.NumericLiteral,
2189
+ ObjectKeyword = _ts$SyntaxKind.ObjectKeyword,
2190
+ Parameter = _ts$SyntaxKind.Parameter,
2191
+ ParenthesizedType = _ts$SyntaxKind.ParenthesizedType,
2192
+ PropertySignature = _ts$SyntaxKind.PropertySignature,
2193
+ StringKeyword = _ts$SyntaxKind.StringKeyword,
2194
+ StringLiteral = _ts$SyntaxKind.StringLiteral,
2195
+ ThisType = _ts$SyntaxKind.ThisType,
2196
+ TupleType = _ts$SyntaxKind.TupleType,
2197
+ TypeLiteral = _ts$SyntaxKind.TypeLiteral,
2198
+ TypeReference = _ts$SyntaxKind.TypeReference,
2199
+ UndefinedKeyword = _ts$SyntaxKind.UndefinedKeyword,
2200
+ UnionType = _ts$SyntaxKind.UnionType,
2201
+ JSDocNullableType = _ts$SyntaxKind.JSDocNullableType;
2202
+ switch (node.kind) {
2203
+ case FunctionType:
2204
+ var parameters = node.parameters.map(toSourceTS);
2205
+ return {
2206
+ type: 'function',
2207
+ parameters: parameters
2208
+ };
2209
+ case JSDocNullableType:
2210
+ var t = toSourceTS(node.type);
2211
+ return {
2212
+ type: 'union',
2213
+ members: [t, 'null']
2214
+ };
2215
+ case Parameter:
2216
+ var type = node.type ? toSourceTS(node.type) : 'any';
2217
+ var name = toSourceTS(node.name);
2218
+ var ret = {
2219
+ type: type,
2220
+ name: name
2221
+ };
2222
+ if (node.dotDotDotToken) {
2223
+ return {
2224
+ type: 'array',
2225
+ elementType: ret
2226
+ };
2227
+ }
2228
+ return ret;
2229
+ case TypeReference:
2230
+ if ((typeName.text === 'Object' || typeName.text === 'Record') && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 2) {
2231
+ return {
2232
+ type: 'record',
2233
+ key: toSourceTS(typeArguments[0]),
2234
+ val: toSourceTS(typeArguments[1])
2235
+ };
2236
+ } else if (typeName.text === 'Object' && (!typeArguments || (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 0)) {
2237
+ return {
2238
+ type: 'object',
2239
+ properties: {}
2240
+ };
2241
+ } else if (typeName.text === 'Map' && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 2) {
2242
+ return {
2243
+ type: 'map',
2244
+ key: toSourceTS(typeArguments[0]),
2245
+ val: toSourceTS(typeArguments[1])
2246
+ };
2247
+ } else if (typeName.text === 'Array' && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 1) {
2248
+ var elementType = toSourceTS(typeArguments[0]);
2249
+ return {
2250
+ type: 'array',
2251
+ elementType: elementType
2252
+ };
2253
+ } else if (typeName.text === 'Set' && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 1) {
2254
+ var _elementType = toSourceTS(typeArguments[0]);
2255
+ return {
2256
+ type: 'set',
2257
+ elementType: _elementType
2258
+ };
2259
+ } else if (typeName.text === 'Class' && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 1) {
2260
+ return {
2261
+ type: 'class',
2262
+ elementType: toSourceTS(typeArguments[0])
2263
+ };
2264
+ } else {
2265
+ if (!typeArguments) {
2266
+ return typeName.getText();
2267
+ }
2268
+ console.warn('unhandled TypeReference', kind_, node);
2269
+ return {
2270
+ type: 'unhandled TypeReference'
2271
+ };
2272
+ }
2273
+ case StringKeyword:
2274
+ return node.getText();
2275
+ case NumberKeyword:
2276
+ return node.getText();
2277
+ case IntersectionType:
2278
+ return {
2279
+ type: 'intersection',
2280
+ members: node.types.map(toSourceTS)
2281
+ };
2282
+ case TupleType:
2283
+ return {
2284
+ type: 'tuple',
2285
+ elements: node.elements.map(toSourceTS)
2286
+ };
2287
+ case UnionType:
2288
+ return {
2289
+ type: 'union',
2290
+ members: node.types.map(toSourceTS)
2291
+ };
2292
+ case TypeLiteral:
2293
+ var properties = {};
2294
+ node.members.forEach(function (member) {
2295
+ var name = toSourceTS(member.name);
2296
+ var type = toSourceTS(member.type);
2297
+ properties[name] = type;
2298
+ });
2299
+ return {
2300
+ type: 'object',
2301
+ properties: properties
2302
+ };
2303
+ case PropertySignature:
2304
+ console.warn('toSourceTS> should not happen, handled by TypeLiteral directly');
2305
+ return "".concat(toSourceTS(node.name), ": ").concat(toSourceTS(node.type));
2306
+ case Identifier:
2307
+ return node.text;
2308
+ case ArrayType:
2309
+ return {
2310
+ type: 'array',
2311
+ elementType: toSourceTS(node.elementType)
2312
+ };
2313
+ case LiteralType:
2314
+ return toSourceTS(node.literal);
2315
+ case AnyKeyword:
2316
+ case BooleanKeyword:
2317
+ case JSDocAllType:
2318
+ case NullKeyword:
2319
+ case NumericLiteral:
2320
+ case StringLiteral:
2321
+ case ThisType:
2322
+ case UndefinedKeyword:
2323
+ return node.getText();
2324
+ case ObjectKeyword:
2325
+ return {
2326
+ type: 'object',
2327
+ properties: {}
2328
+ };
2329
+ case ParenthesizedType:
2330
+ return toSourceTS(node.type);
2331
+ case LastTypeNode:
2332
+ return toSourceTS(node.qualifier);
2333
+ default:
2334
+ console.warn('toSourceTS> unhandled kind - make sure to understand you cannot reverse TS enums');
2335
+ console.warn('if they contain range aliases, for example:');
2336
+ console.warn('ts.SyntaxKind.NumericLiteral === ts.SyntaxKind.FirstLiteralToken');
2337
+ console.warn('so this "kind" could be wrong, but requires handling anyway:', kind_, node);
2338
+ debugger;
2339
+ }
2340
+ }
2341
+
2342
+ exports.Asserter = Asserter;
2343
+ exports.Stringifier = Stringifier;
2344
+ exports.addTypeChecks = addTypeChecks;
2345
+ exports.ast2json = ast2json;
2346
+ exports.ast2jsonForComparison = ast2jsonForComparison;
2347
+ exports.code2ast2code = code2ast2code;
2348
+ exports.expandType = expandType;
2349
+ exports.expandTypeDepFree = expandTypeDepFree;
2350
+ exports.extractCurlyContent = extractCurlyContent;
2351
+ exports.extractNameAndOptionality = extractNameAndOptionality;
2352
+ exports.parseJSDoc = parseJSDoc;
2353
+ exports.parseJSDocSetter = parseJSDocSetter;
2354
+ exports.parseJSDocTypedef = parseJSDocTypedef;
2355
+ exports.parseType = parseType;
2356
+ exports.simplifyType = simplifyType;
2357
+ exports.toSourceTS = toSourceTS;
2358
+
2359
+ }));