@runtime-type-inspector/transpiler 2.0.9 → 2.0.10
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.
- package/index.cjs +207 -136
- package/index.mjs +190 -128
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/parser'), require('typescript')) :
|
|
3
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
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.rtiTranspiler = {}, global.parser, global.ts));
|
|
5
|
+
})(this, (function (exports, parser, ts) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function _iterableToArrayLimit(r, l) {
|
|
8
8
|
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
@@ -260,7 +260,7 @@
|
|
|
260
260
|
|
|
261
261
|
function expandTypeDepFree(type) {
|
|
262
262
|
type = type.trim();
|
|
263
|
-
while (!type.includes('|') && type[0]
|
|
263
|
+
while (!type.includes('|') && type[0] === '(' && type[type.length - 1] === ')') {
|
|
264
264
|
type = type.slice(1, -1).trim();
|
|
265
265
|
}
|
|
266
266
|
if (type[0] === '.' && type[1] === '.' && type[2] === '.') {
|
|
@@ -290,7 +290,7 @@
|
|
|
290
290
|
val: expandTypeDepFree(val)
|
|
291
291
|
};
|
|
292
292
|
}
|
|
293
|
-
if (type[0]
|
|
293
|
+
if (type[0] === '{' && type[type.length - 1] === '}') {
|
|
294
294
|
var propertiesArray = type.slice(1, -1).split(',');
|
|
295
295
|
var properties = {};
|
|
296
296
|
propertiesArray.forEach(function (_) {
|
|
@@ -328,7 +328,7 @@
|
|
|
328
328
|
elementType: expandTypeDepFree(_typeSlice)
|
|
329
329
|
};
|
|
330
330
|
}
|
|
331
|
-
if (type[0]
|
|
331
|
+
if (type[0] === '[' && type[type.length - 1] === ']') {
|
|
332
332
|
var elements = type.slice(1, -1).split(',');
|
|
333
333
|
return {
|
|
334
334
|
type: 'tuple',
|
|
@@ -362,7 +362,7 @@
|
|
|
362
362
|
type.optional = optional;
|
|
363
363
|
} else if (typeof type === 'string') {
|
|
364
364
|
type = type.trim();
|
|
365
|
-
if (type
|
|
365
|
+
if (type !== 'object' && type !== 'object[]' && type !== 'union' && !optional) {
|
|
366
366
|
return type;
|
|
367
367
|
}
|
|
368
368
|
type = {
|
|
@@ -373,7 +373,7 @@
|
|
|
373
373
|
debugger;
|
|
374
374
|
console.warn("simplifyType> neither object nor string for type", type);
|
|
375
375
|
}
|
|
376
|
-
if (type.type
|
|
376
|
+
if (type.type === 'object' && type.properties && Object.keys(type.properties).length === 0) {
|
|
377
377
|
delete type.properties;
|
|
378
378
|
}
|
|
379
379
|
return type;
|
|
@@ -396,28 +396,28 @@
|
|
|
396
396
|
name = name.split(' ')[0].split('=')[0].trim();
|
|
397
397
|
var simplifiedType = simplifyType(type, optional);
|
|
398
398
|
var parts = name.split(".");
|
|
399
|
-
if (parts.length
|
|
399
|
+
if (parts.length === 3) {
|
|
400
400
|
var parts0 = parts[0];
|
|
401
401
|
var parts1 = parts[1];
|
|
402
402
|
var parts2 = parts[2];
|
|
403
403
|
var toptype = params[parts0];
|
|
404
404
|
toptype.properties[parts1].properties = toptype.properties[parts1].properties || {};
|
|
405
405
|
toptype.properties[parts1].properties[parts2] = simplifiedType;
|
|
406
|
-
} else if (parts.length
|
|
406
|
+
} else if (parts.length === 2) {
|
|
407
407
|
var _parts = parts[0];
|
|
408
408
|
var _parts2 = parts[1];
|
|
409
409
|
if (_parts.endsWith('[]')) {
|
|
410
410
|
_parts = _parts.slice(0, -2);
|
|
411
411
|
}
|
|
412
412
|
var _toptype = params[_parts];
|
|
413
|
-
if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type)
|
|
413
|
+
if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) === "union") {
|
|
414
414
|
var typeObject = _toptype.members.find(function (_) {
|
|
415
|
-
return (_ === null || _ === void 0 ? void 0 : _.type)
|
|
415
|
+
return (_ === null || _ === void 0 ? void 0 : _.type) === 'object';
|
|
416
416
|
});
|
|
417
417
|
typeObject.properties[_parts2] = simplifiedType;
|
|
418
|
-
} else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type)
|
|
418
|
+
} else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) === "array") {
|
|
419
419
|
_toptype.elementType.properties[_parts2] = simplifiedType;
|
|
420
|
-
} else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type)
|
|
420
|
+
} else if ((_toptype === null || _toptype === void 0 ? void 0 : _toptype.type) === "object") {
|
|
421
421
|
_toptype.properties = _toptype.properties || {};
|
|
422
422
|
_toptype.properties[_parts2] = simplifiedType;
|
|
423
423
|
} else {
|
|
@@ -491,7 +491,7 @@
|
|
|
491
491
|
return;
|
|
492
492
|
}
|
|
493
493
|
var lines = value.split('\n');
|
|
494
|
-
var lastTypedef
|
|
494
|
+
var lastTypedef;
|
|
495
495
|
var _iterator = _createForOfIteratorHelper(lines),
|
|
496
496
|
_step;
|
|
497
497
|
try {
|
|
@@ -556,8 +556,9 @@
|
|
|
556
556
|
_classCallCheck(this, Stringifier);
|
|
557
557
|
_defineProperty(this, "forceCurly", false);
|
|
558
558
|
_defineProperty(this, "parents", []);
|
|
559
|
-
_defineProperty(this, "
|
|
559
|
+
_defineProperty(this, "lastCommentBlockIndex", -1);
|
|
560
560
|
_defineProperty(this, "lastCommentLineIndex", -1);
|
|
561
|
+
_defineProperty(this, "numSpaces", 0);
|
|
561
562
|
}
|
|
562
563
|
_createClass(Stringifier, [{
|
|
563
564
|
key: "toSource",
|
|
@@ -575,7 +576,7 @@
|
|
|
575
576
|
var out = '';
|
|
576
577
|
var comments = '';
|
|
577
578
|
if (leadingComments) {
|
|
578
|
-
comments
|
|
579
|
+
comments = this.leadingCommentsToSource(leadingComments);
|
|
579
580
|
}
|
|
580
581
|
if (node !== null && node !== void 0 && (_node$extra = node.extra) !== null && _node$extra !== void 0 && _node$extra.parenthesized) {
|
|
581
582
|
out += "(".concat(comments).concat(this.toSource_(node), ")");
|
|
@@ -583,21 +584,7 @@
|
|
|
583
584
|
out += comments + this.toSource_(node);
|
|
584
585
|
}
|
|
585
586
|
if (trailingComments) {
|
|
586
|
-
|
|
587
|
-
_step;
|
|
588
|
-
try {
|
|
589
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
590
|
-
var trailingComment = _step.value;
|
|
591
|
-
if (trailingComment.type === 'CommentBlock') {
|
|
592
|
-
out += ' ';
|
|
593
|
-
}
|
|
594
|
-
out += this.toSource(trailingComment);
|
|
595
|
-
}
|
|
596
|
-
} catch (err) {
|
|
597
|
-
_iterator.e(err);
|
|
598
|
-
} finally {
|
|
599
|
-
_iterator.f();
|
|
600
|
-
}
|
|
587
|
+
out += this.trailingCommentsToSource(trailingComments);
|
|
601
588
|
}
|
|
602
589
|
this.parents.pop();
|
|
603
590
|
return out;
|
|
@@ -625,6 +612,120 @@
|
|
|
625
612
|
console.warn('TODO ADD METHOD:', fn);
|
|
626
613
|
return "rtiUnhandled(\"".concat(node.type, "\");\n");
|
|
627
614
|
}
|
|
615
|
+
}, {
|
|
616
|
+
key: "parentProvidesSpaces",
|
|
617
|
+
value: function parentProvidesSpaces(reverseIndex) {
|
|
618
|
+
var parents = this.parents;
|
|
619
|
+
var parent = parents[parents.length - reverseIndex];
|
|
620
|
+
return (parent === null || parent === void 0 ? void 0 : parent.type) === 'ArrayExpression';
|
|
621
|
+
}
|
|
622
|
+
}, {
|
|
623
|
+
key: "CommentBlock",
|
|
624
|
+
value: function CommentBlock(node) {
|
|
625
|
+
var loc = node.loc;
|
|
626
|
+
var value = node.value;
|
|
627
|
+
if (this.lastCommentBlockIndex === loc.start.index) {
|
|
628
|
+
return '';
|
|
629
|
+
}
|
|
630
|
+
this.lastCommentBlockIndex = loc.start.index;
|
|
631
|
+
var spaces = this.spaces;
|
|
632
|
+
var out = '';
|
|
633
|
+
var dedicatedLine = spaces.length === loc.start.column;
|
|
634
|
+
if (dedicatedLine) {
|
|
635
|
+
out += spaces;
|
|
636
|
+
}
|
|
637
|
+
out += '/*';
|
|
638
|
+
if (value.includes('\n')) {
|
|
639
|
+
value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(function (_) {
|
|
640
|
+
return _.length;
|
|
641
|
+
}).map(function (line, i) {
|
|
642
|
+
return (i ? spaces + ' ' : '') + line;
|
|
643
|
+
}).join('\n');
|
|
644
|
+
out += "".concat(value, "\n").concat(spaces, " */");
|
|
645
|
+
} else {
|
|
646
|
+
out += "".concat(value, "*/");
|
|
647
|
+
}
|
|
648
|
+
if (dedicatedLine) {
|
|
649
|
+
out += '\n';
|
|
650
|
+
} else {
|
|
651
|
+
out += ' ';
|
|
652
|
+
}
|
|
653
|
+
return out;
|
|
654
|
+
}
|
|
655
|
+
}, {
|
|
656
|
+
key: "CommentLine",
|
|
657
|
+
value: function CommentLine(node) {
|
|
658
|
+
var value = node.value,
|
|
659
|
+
loc = node.loc;
|
|
660
|
+
if (this.lastCommentLineIndex === loc.start.index) {
|
|
661
|
+
return '';
|
|
662
|
+
}
|
|
663
|
+
this.lastCommentLineIndex = loc.start.index;
|
|
664
|
+
var out = '';
|
|
665
|
+
var spaces = this.spaces;
|
|
666
|
+
var dedicatedLine = spaces.length === loc.start.column;
|
|
667
|
+
if (dedicatedLine) {
|
|
668
|
+
if (!this.parentProvidesSpaces(2)) {
|
|
669
|
+
out += '\n';
|
|
670
|
+
out += spaces;
|
|
671
|
+
}
|
|
672
|
+
} else {
|
|
673
|
+
out += ' ';
|
|
674
|
+
}
|
|
675
|
+
out += "//".concat(value, "\n");
|
|
676
|
+
return out;
|
|
677
|
+
}
|
|
678
|
+
}, {
|
|
679
|
+
key: "leadingCommentsToSource",
|
|
680
|
+
value: function leadingCommentsToSource(leadingComments) {
|
|
681
|
+
var _this = this;
|
|
682
|
+
var out = leadingComments.map(function (_) {
|
|
683
|
+
return _this.commentToSource(_, 'leading');
|
|
684
|
+
}).join('');
|
|
685
|
+
if (this.parentProvidesSpaces(2)) {
|
|
686
|
+
out += this.spaces;
|
|
687
|
+
}
|
|
688
|
+
return out;
|
|
689
|
+
}
|
|
690
|
+
}, {
|
|
691
|
+
key: "trailingCommentsToSource",
|
|
692
|
+
value: function trailingCommentsToSource(trailingComments) {
|
|
693
|
+
var out = '';
|
|
694
|
+
var _iterator = _createForOfIteratorHelper(trailingComments),
|
|
695
|
+
_step;
|
|
696
|
+
try {
|
|
697
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
698
|
+
var trailingComment = _step.value;
|
|
699
|
+
if (trailingComment.type === 'CommentBlock') {
|
|
700
|
+
var loc = trailingComment.loc;
|
|
701
|
+
var dedicatedLine = this.spaces.length === loc.start.column;
|
|
702
|
+
if (dedicatedLine) {
|
|
703
|
+
out += '\n';
|
|
704
|
+
} else {
|
|
705
|
+
out += ' ';
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
out += this.commentToSource(trailingComment, 'trailing');
|
|
709
|
+
}
|
|
710
|
+
} catch (err) {
|
|
711
|
+
_iterator.e(err);
|
|
712
|
+
} finally {
|
|
713
|
+
_iterator.f();
|
|
714
|
+
}
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
}, {
|
|
718
|
+
key: "commentToSource",
|
|
719
|
+
value: function commentToSource(comment, pos) {
|
|
720
|
+
if (comment.type === 'CommentBlock') {
|
|
721
|
+
return this.CommentBlock(comment);
|
|
722
|
+
} else if (comment.type === 'CommentLine') {
|
|
723
|
+
return this.CommentLine(comment);
|
|
724
|
+
}
|
|
725
|
+
console.warn("Unknown comment type", comment);
|
|
726
|
+
debugger;
|
|
727
|
+
return '';
|
|
728
|
+
}
|
|
628
729
|
}, {
|
|
629
730
|
key: "toSourceCurly",
|
|
630
731
|
value: function toSourceCurly(node) {
|
|
@@ -632,7 +733,7 @@
|
|
|
632
733
|
return this.toSource(node);
|
|
633
734
|
}
|
|
634
735
|
var type = node.type;
|
|
635
|
-
var needCurly = type
|
|
736
|
+
var needCurly = type !== 'BlockStatement' && type !== 'EmptyStatement';
|
|
636
737
|
var spaces = this.spaces;
|
|
637
738
|
var out = '';
|
|
638
739
|
if (needCurly) {
|
|
@@ -661,21 +762,20 @@
|
|
|
661
762
|
key: "needSpaces",
|
|
662
763
|
get: function get() {
|
|
663
764
|
var t = this.parentType;
|
|
664
|
-
return t
|
|
765
|
+
return t !== "ForStatement" && t !== "ForInStatement" && t !== "ForOfStatement";
|
|
665
766
|
}
|
|
666
767
|
}, {
|
|
667
768
|
key: "mapToSource",
|
|
668
769
|
value: function mapToSource(arr) {
|
|
669
|
-
var
|
|
770
|
+
var _this2 = this;
|
|
670
771
|
return arr.map(function (_) {
|
|
671
|
-
return
|
|
772
|
+
return _this2.toSource(_);
|
|
672
773
|
});
|
|
673
774
|
}
|
|
674
775
|
}, {
|
|
675
776
|
key: "generateTypeChecks",
|
|
676
777
|
value: function generateTypeChecks(node) {
|
|
677
|
-
|
|
678
|
-
return "".concat(spaces, "/* Stub of Stringifier#generateTypeChecks({type=").concat(node.type, "}); */\n");
|
|
778
|
+
return '';
|
|
679
779
|
}
|
|
680
780
|
}, {
|
|
681
781
|
key: "spaces",
|
|
@@ -988,9 +1088,7 @@
|
|
|
988
1088
|
test = node.test;
|
|
989
1089
|
var spaces = this.spaces;
|
|
990
1090
|
var out = '';
|
|
991
|
-
|
|
992
|
-
out += spaces;
|
|
993
|
-
}
|
|
1091
|
+
out += spaces;
|
|
994
1092
|
out += "if (".concat(this.toSource(test), ")");
|
|
995
1093
|
out += this.toSourceCurly(consequent);
|
|
996
1094
|
if (alternate) {
|
|
@@ -1152,9 +1250,23 @@
|
|
|
1152
1250
|
key: "ArrayExpression",
|
|
1153
1251
|
value: function ArrayExpression(node) {
|
|
1154
1252
|
var elements = node.elements,
|
|
1155
|
-
extra = node.extra
|
|
1253
|
+
extra = node.extra,
|
|
1254
|
+
loc = node.loc;
|
|
1156
1255
|
var e = !!(extra !== null && extra !== void 0 && extra.trailingComma) ? ',' : '';
|
|
1157
|
-
|
|
1256
|
+
var sameLine = loc.start.line === loc.end.line;
|
|
1257
|
+
if (sameLine) {
|
|
1258
|
+
return '[' + this.mapToSource(elements).join(', ') + e + ']';
|
|
1259
|
+
}
|
|
1260
|
+
var out = '[\n';
|
|
1261
|
+
this.numSpaces++;
|
|
1262
|
+
var spaces = this.spaces;
|
|
1263
|
+
out += this.mapToSource(elements).map(function (_) {
|
|
1264
|
+
return spaces + _;
|
|
1265
|
+
}).join(',\n');
|
|
1266
|
+
this.numSpaces--;
|
|
1267
|
+
out += e;
|
|
1268
|
+
out += '\n' + this.spaces + ']';
|
|
1269
|
+
return out;
|
|
1158
1270
|
}
|
|
1159
1271
|
}, {
|
|
1160
1272
|
key: "VariableDeclaration",
|
|
@@ -1225,9 +1337,8 @@
|
|
|
1225
1337
|
argument = node.argument;
|
|
1226
1338
|
if (prefix) {
|
|
1227
1339
|
return "".concat(operator).concat(this.toSource(argument));
|
|
1228
|
-
} else {
|
|
1229
|
-
return "".concat(this.toSource(argument)).concat(operator);
|
|
1230
1340
|
}
|
|
1341
|
+
return "".concat(this.toSource(argument)).concat(operator);
|
|
1231
1342
|
}
|
|
1232
1343
|
}, {
|
|
1233
1344
|
key: "NewExpression",
|
|
@@ -1273,7 +1384,7 @@
|
|
|
1273
1384
|
}, {
|
|
1274
1385
|
key: "ExportNamedDeclaration",
|
|
1275
1386
|
value: function ExportNamedDeclaration(node) {
|
|
1276
|
-
var
|
|
1387
|
+
var _this3 = this;
|
|
1277
1388
|
var declaration = node.declaration,
|
|
1278
1389
|
source = node.source,
|
|
1279
1390
|
specifiers = node.specifiers;
|
|
@@ -1281,7 +1392,7 @@
|
|
|
1281
1392
|
if (specifiers.length) {
|
|
1282
1393
|
out += 'export {\n';
|
|
1283
1394
|
out += this.mapToSource(specifiers).map(function (_) {
|
|
1284
|
-
return " ".concat(
|
|
1395
|
+
return " ".concat(_this3.spaces).concat(_);
|
|
1285
1396
|
}).join(',\n');
|
|
1286
1397
|
out += '\n';
|
|
1287
1398
|
out += this.spaces;
|
|
@@ -1582,56 +1693,6 @@
|
|
|
1582
1693
|
value: function DebuggerStatement(node) {
|
|
1583
1694
|
return "".concat(this.spaces, "debugger;");
|
|
1584
1695
|
}
|
|
1585
|
-
}, {
|
|
1586
|
-
key: "CommentBlock",
|
|
1587
|
-
value: function CommentBlock(node) {
|
|
1588
|
-
var value = node.value,
|
|
1589
|
-
loc = node.loc;
|
|
1590
|
-
var spaces = this.spaces;
|
|
1591
|
-
this.parents;
|
|
1592
|
-
var out = '';
|
|
1593
|
-
var dedicatedLine = spaces.length === loc.start.column;
|
|
1594
|
-
if (dedicatedLine) {
|
|
1595
|
-
out += spaces;
|
|
1596
|
-
}
|
|
1597
|
-
out += '/*';
|
|
1598
|
-
if (value.includes('\n')) {
|
|
1599
|
-
value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(function (_) {
|
|
1600
|
-
return _.length;
|
|
1601
|
-
}).map(function (line, i) {
|
|
1602
|
-
return (i ? spaces + ' ' : '') + line;
|
|
1603
|
-
}).join('\n');
|
|
1604
|
-
out += "".concat(value, "\n").concat(spaces, " */");
|
|
1605
|
-
} else {
|
|
1606
|
-
out += "".concat(value, "*/");
|
|
1607
|
-
}
|
|
1608
|
-
if (dedicatedLine) {
|
|
1609
|
-
out += '\n';
|
|
1610
|
-
} else {
|
|
1611
|
-
out += ' ';
|
|
1612
|
-
}
|
|
1613
|
-
return out;
|
|
1614
|
-
}
|
|
1615
|
-
}, {
|
|
1616
|
-
key: "CommentLine",
|
|
1617
|
-
value: function CommentLine(node) {
|
|
1618
|
-
var value = node.value,
|
|
1619
|
-
loc = node.loc;
|
|
1620
|
-
if (this.lastCommentLineIndex === loc.start.index) {
|
|
1621
|
-
return '';
|
|
1622
|
-
}
|
|
1623
|
-
this.lastCommentLineIndex = loc.start.index;
|
|
1624
|
-
var out = '';
|
|
1625
|
-
var spaces = this.spaces;
|
|
1626
|
-
var dedicatedLine = spaces.length === loc.start.column;
|
|
1627
|
-
if (dedicatedLine) {
|
|
1628
|
-
out += '\n' + spaces;
|
|
1629
|
-
} else {
|
|
1630
|
-
out += ' ';
|
|
1631
|
-
}
|
|
1632
|
-
out += "//".concat(value, "\n");
|
|
1633
|
-
return out;
|
|
1634
|
-
}
|
|
1635
1696
|
}, {
|
|
1636
1697
|
key: "Import",
|
|
1637
1698
|
value: function Import(node) {
|
|
@@ -1684,9 +1745,7 @@
|
|
|
1684
1745
|
}, {
|
|
1685
1746
|
key: "Program",
|
|
1686
1747
|
value: function Program(node) {
|
|
1687
|
-
node.
|
|
1688
|
-
node.interpreter;
|
|
1689
|
-
var body = node.body,
|
|
1748
|
+
var body = node.body,
|
|
1690
1749
|
directives = node.directives;
|
|
1691
1750
|
var out = '';
|
|
1692
1751
|
out += this.mapToSource(directives);
|
|
@@ -1856,7 +1915,7 @@
|
|
|
1856
1915
|
key: "findParentOfType",
|
|
1857
1916
|
value: function findParentOfType(node, type) {
|
|
1858
1917
|
var currentIndex = this.parents.findLastIndex(function (_) {
|
|
1859
|
-
return _
|
|
1918
|
+
return _ === node;
|
|
1860
1919
|
});
|
|
1861
1920
|
return this.parents.findLast(function (_, i) {
|
|
1862
1921
|
if (i > currentIndex) {
|
|
@@ -1870,7 +1929,7 @@
|
|
|
1870
1929
|
value: function getLeadingCommentsNodeForArrowFunctionExpression(node) {
|
|
1871
1930
|
var parents = this.parents;
|
|
1872
1931
|
var i = parents.findLastIndex(function (_) {
|
|
1873
|
-
return _
|
|
1932
|
+
return _ === node;
|
|
1874
1933
|
});
|
|
1875
1934
|
var parent = parents[i];
|
|
1876
1935
|
if (parent.leadingComments) {
|
|
@@ -1914,7 +1973,7 @@
|
|
|
1914
1973
|
node = this.findParentOfType(node, 'ExpressionStatement');
|
|
1915
1974
|
if (!node) {
|
|
1916
1975
|
node = this.parents.findLast(function (_) {
|
|
1917
|
-
return _.type
|
|
1976
|
+
return _.type === 'VariableDeclaration';
|
|
1918
1977
|
});
|
|
1919
1978
|
if (!node) {
|
|
1920
1979
|
return;
|
|
@@ -1935,7 +1994,7 @@
|
|
|
1935
1994
|
}
|
|
1936
1995
|
if (leadingComments && leadingComments.length) {
|
|
1937
1996
|
var lastComment = leadingComments[leadingComments.length - 1];
|
|
1938
|
-
if (lastComment.type
|
|
1997
|
+
if (lastComment.type === "CommentBlock") {
|
|
1939
1998
|
if (lastComment.value.includes('@event')) {
|
|
1940
1999
|
return;
|
|
1941
2000
|
}
|
|
@@ -2023,11 +2082,10 @@
|
|
|
2023
2082
|
return node.name === name;
|
|
2024
2083
|
} else if (type === 'ArrayPattern' || type === 'ObjectPattern' || type === 'RestElement') {
|
|
2025
2084
|
return false;
|
|
2026
|
-
} else {
|
|
2027
|
-
var _ = new Stringifier();
|
|
2028
|
-
var code = _.toSource(node);
|
|
2029
|
-
console.log("Unknown type to test params for", type, code);
|
|
2030
2085
|
}
|
|
2086
|
+
var _ = new Stringifier();
|
|
2087
|
+
var code = _.toSource(node);
|
|
2088
|
+
console.log("Unknown type to test params for", type, code);
|
|
2031
2089
|
return false;
|
|
2032
2090
|
});
|
|
2033
2091
|
}
|
|
@@ -2169,7 +2227,7 @@
|
|
|
2169
2227
|
case 'ClassMethod':
|
|
2170
2228
|
case 'ClassPrivateMethod':
|
|
2171
2229
|
var classDecl = this.parents.findLast(function (_) {
|
|
2172
|
-
return _.type
|
|
2230
|
+
return _.type === 'ClassDeclaration';
|
|
2173
2231
|
});
|
|
2174
2232
|
var out = '';
|
|
2175
2233
|
if (classDecl) {
|
|
@@ -2190,9 +2248,8 @@
|
|
|
2190
2248
|
var parent = this.findParentOfType(node, 'VariableDeclarator');
|
|
2191
2249
|
if (parent) {
|
|
2192
2250
|
return toSource(parent.id);
|
|
2193
|
-
} else {
|
|
2194
|
-
return 'getName> missing parent for ' + node.type;
|
|
2195
2251
|
}
|
|
2252
|
+
return 'getName> missing parent for ' + node.type;
|
|
2196
2253
|
case 'ObjectMethod':
|
|
2197
2254
|
return toSource(key);
|
|
2198
2255
|
default:
|
|
@@ -2219,8 +2276,7 @@
|
|
|
2219
2276
|
}, {
|
|
2220
2277
|
key: "File",
|
|
2221
2278
|
value: function File(node) {
|
|
2222
|
-
node.
|
|
2223
|
-
var program = node.program,
|
|
2279
|
+
var program = node.program,
|
|
2224
2280
|
comments = node.comments;
|
|
2225
2281
|
if (comments) {
|
|
2226
2282
|
var _iterator3 = _createForOfIteratorHelper(comments),
|
|
@@ -2267,8 +2323,8 @@
|
|
|
2267
2323
|
|
|
2268
2324
|
function ast2json(ast) {
|
|
2269
2325
|
return JSON.stringify(ast, function (name, val) {
|
|
2270
|
-
if (name
|
|
2271
|
-
return
|
|
2326
|
+
if (name === "loc" || name === "start" || name === "end") {
|
|
2327
|
+
return;
|
|
2272
2328
|
}
|
|
2273
2329
|
return val;
|
|
2274
2330
|
}, 2);
|
|
@@ -2309,14 +2365,14 @@
|
|
|
2309
2365
|
str += '[]';
|
|
2310
2366
|
}
|
|
2311
2367
|
str = "type tmp = ".concat(str, ";");
|
|
2312
|
-
var ast = ts
|
|
2368
|
+
var ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, true);
|
|
2313
2369
|
return ast.statements[0].type;
|
|
2314
2370
|
}
|
|
2315
2371
|
function toSourceTS(node) {
|
|
2316
2372
|
var typeArguments = node.typeArguments,
|
|
2317
2373
|
typeName = node.typeName;
|
|
2318
|
-
var kind_ = ts
|
|
2319
|
-
var _ts$SyntaxKind = ts
|
|
2374
|
+
var kind_ = ts.SyntaxKind[node.kind];
|
|
2375
|
+
var _ts$SyntaxKind = ts.SyntaxKind,
|
|
2320
2376
|
AnyKeyword = _ts$SyntaxKind.AnyKeyword,
|
|
2321
2377
|
ArrayType = _ts$SyntaxKind.ArrayType,
|
|
2322
2378
|
BooleanKeyword = _ts$SyntaxKind.BooleanKeyword,
|
|
@@ -2344,8 +2400,21 @@
|
|
|
2344
2400
|
JSDocNullableType = _ts$SyntaxKind.JSDocNullableType,
|
|
2345
2401
|
TrueKeyword = _ts$SyntaxKind.TrueKeyword,
|
|
2346
2402
|
FalseKeyword = _ts$SyntaxKind.FalseKeyword,
|
|
2347
|
-
VoidKeyword = _ts$SyntaxKind.VoidKeyword
|
|
2403
|
+
VoidKeyword = _ts$SyntaxKind.VoidKeyword,
|
|
2404
|
+
UnknownKeyword = _ts$SyntaxKind.UnknownKeyword,
|
|
2405
|
+
NeverKeyword = _ts$SyntaxKind.NeverKeyword,
|
|
2406
|
+
ConstructorType = _ts$SyntaxKind.ConstructorType;
|
|
2348
2407
|
switch (node.kind) {
|
|
2408
|
+
case ConstructorType:
|
|
2409
|
+
{
|
|
2410
|
+
var _parameters = node.parameters.map(toSourceTS);
|
|
2411
|
+
var _ret = toSourceTS(node.type);
|
|
2412
|
+
return {
|
|
2413
|
+
type: 'new',
|
|
2414
|
+
parameters: _parameters,
|
|
2415
|
+
ret: _ret
|
|
2416
|
+
};
|
|
2417
|
+
}
|
|
2349
2418
|
case FunctionType:
|
|
2350
2419
|
var parameters = node.parameters.map(toSourceTS);
|
|
2351
2420
|
return {
|
|
@@ -2407,15 +2476,14 @@
|
|
|
2407
2476
|
type: 'class',
|
|
2408
2477
|
elementType: toSourceTS(typeArguments[0])
|
|
2409
2478
|
};
|
|
2410
|
-
} else {
|
|
2411
|
-
if (!typeArguments) {
|
|
2412
|
-
return typeName.getText();
|
|
2413
|
-
}
|
|
2414
|
-
console.warn('unhandled TypeReference', kind_, node);
|
|
2415
|
-
return {
|
|
2416
|
-
type: 'unhandled TypeReference'
|
|
2417
|
-
};
|
|
2418
2479
|
}
|
|
2480
|
+
if (!typeArguments) {
|
|
2481
|
+
return typeName.getText();
|
|
2482
|
+
}
|
|
2483
|
+
console.warn('unhandled TypeReference', kind_, node);
|
|
2484
|
+
return {
|
|
2485
|
+
type: 'unhandled TypeReference'
|
|
2486
|
+
};
|
|
2419
2487
|
case StringKeyword:
|
|
2420
2488
|
return node.getText();
|
|
2421
2489
|
case NumberKeyword:
|
|
@@ -2469,6 +2537,8 @@
|
|
|
2469
2537
|
case TrueKeyword:
|
|
2470
2538
|
case FalseKeyword:
|
|
2471
2539
|
case VoidKeyword:
|
|
2540
|
+
case UnknownKeyword:
|
|
2541
|
+
case NeverKeyword:
|
|
2472
2542
|
return node.getText();
|
|
2473
2543
|
case ObjectKeyword:
|
|
2474
2544
|
return {
|
|
@@ -2500,12 +2570,13 @@
|
|
|
2500
2570
|
return ast.program.body[0].typeAnnotation;
|
|
2501
2571
|
}
|
|
2502
2572
|
function toSourceBabelTS(node) {
|
|
2503
|
-
var typeName = node.typeName;
|
|
2504
|
-
var kind_ = ts.SyntaxKind[node.kind];
|
|
2505
2573
|
switch (node.type) {
|
|
2506
2574
|
case 'TSTypeReference':
|
|
2507
2575
|
{
|
|
2508
2576
|
var name = toSourceBabelTS(node.typeName);
|
|
2577
|
+
if (!node.typeParameters) {
|
|
2578
|
+
return node.typeName.name;
|
|
2579
|
+
}
|
|
2509
2580
|
console.assert(node.typeParameters.type === 'TSTypeParameterInstantiation');
|
|
2510
2581
|
var typeArguments = node.typeParameters.params;
|
|
2511
2582
|
if ((name === 'Object' || name === 'Record') && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 2) {
|
|
@@ -2542,15 +2613,11 @@
|
|
|
2542
2613
|
type: 'class',
|
|
2543
2614
|
elementType: toSourceBabelTS(typeArguments[0])
|
|
2544
2615
|
};
|
|
2545
|
-
} else {
|
|
2546
|
-
if (!typeArguments) {
|
|
2547
|
-
return typeName.name;
|
|
2548
|
-
}
|
|
2549
|
-
console.warn('unhandled TypeReference', kind_, node);
|
|
2550
|
-
return {
|
|
2551
|
-
type: 'unhandled TypeReference'
|
|
2552
|
-
};
|
|
2553
2616
|
}
|
|
2617
|
+
console.warn('unhandled TypeReference', node);
|
|
2618
|
+
return {
|
|
2619
|
+
type: 'unhandled TypeReference'
|
|
2620
|
+
};
|
|
2554
2621
|
}
|
|
2555
2622
|
case 'TSStringKeyword':
|
|
2556
2623
|
return 'string';
|
|
@@ -2604,6 +2671,10 @@
|
|
|
2604
2671
|
return node.extra.raw;
|
|
2605
2672
|
case 'TSUndefinedKeyword':
|
|
2606
2673
|
return 'undefined';
|
|
2674
|
+
case 'TSUnknownKeyword':
|
|
2675
|
+
return 'unknown';
|
|
2676
|
+
case 'TSNeverKeyword':
|
|
2677
|
+
return 'never';
|
|
2607
2678
|
case 'TSVoidKeyword':
|
|
2608
2679
|
return 'void';
|
|
2609
2680
|
case 'TSThisType':
|
package/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { parse } from '@babel/parser';
|
|
2
|
-
import ts
|
|
2
|
+
import ts from 'typescript';
|
|
3
3
|
|
|
4
4
|
function expandTypeDepFree(type) {
|
|
5
5
|
type = type.trim();
|
|
6
|
-
while (!type.includes('|') && type[0]
|
|
6
|
+
while (!type.includes('|') && type[0] === '(' && type[type.length - 1] === ')') {
|
|
7
7
|
type = type.slice(1, -1).trim();
|
|
8
8
|
}
|
|
9
9
|
if (type[0] === '.' && type[1] === '.' && type[2] === '.') {
|
|
@@ -33,7 +33,7 @@ function expandTypeDepFree(type) {
|
|
|
33
33
|
val: expandTypeDepFree(val)
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
if (type[0]
|
|
36
|
+
if (type[0] === '{' && type[type.length - 1] === '}') {
|
|
37
37
|
const propertiesArray = type.slice(1, -1).split(',');
|
|
38
38
|
const properties = {};
|
|
39
39
|
propertiesArray.forEach(_ => {
|
|
@@ -64,7 +64,7 @@ function expandTypeDepFree(type) {
|
|
|
64
64
|
elementType: expandTypeDepFree(typeSlice)
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
-
if (type[0]
|
|
67
|
+
if (type[0] === '[' && type[type.length - 1] === ']') {
|
|
68
68
|
const elements = type.slice(1, -1).split(',');
|
|
69
69
|
return {
|
|
70
70
|
type: 'tuple',
|
|
@@ -98,7 +98,7 @@ function simplifyType(type, optional) {
|
|
|
98
98
|
type.optional = optional;
|
|
99
99
|
} else if (typeof type === 'string') {
|
|
100
100
|
type = type.trim();
|
|
101
|
-
if (type
|
|
101
|
+
if (type !== 'object' && type !== 'object[]' && type !== 'union' && !optional) {
|
|
102
102
|
return type;
|
|
103
103
|
}
|
|
104
104
|
type = {
|
|
@@ -109,7 +109,7 @@ function simplifyType(type, optional) {
|
|
|
109
109
|
debugger;
|
|
110
110
|
console.warn("simplifyType> neither object nor string for type", type);
|
|
111
111
|
}
|
|
112
|
-
if (type.type
|
|
112
|
+
if (type.type === 'object' && type.properties && Object.keys(type.properties).length === 0) {
|
|
113
113
|
delete type.properties;
|
|
114
114
|
}
|
|
115
115
|
return type;
|
|
@@ -131,26 +131,26 @@ function parseJSDoc(src, expandType = expandTypeDepFree) {
|
|
|
131
131
|
name = name.split(' ')[0].split('=')[0].trim();
|
|
132
132
|
const simplifiedType = simplifyType(type, optional);
|
|
133
133
|
const parts = name.split(".");
|
|
134
|
-
if (parts.length
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
if (parts.length === 3) {
|
|
135
|
+
const parts0 = parts[0];
|
|
136
|
+
const parts1 = parts[1];
|
|
137
|
+
const parts2 = parts[2];
|
|
138
138
|
const toptype = params[parts0];
|
|
139
139
|
toptype.properties[parts1].properties = toptype.properties[parts1].properties || {};
|
|
140
140
|
toptype.properties[parts1].properties[parts2] = simplifiedType;
|
|
141
|
-
} else if (parts.length
|
|
141
|
+
} else if (parts.length === 2) {
|
|
142
142
|
let parts0 = parts[0];
|
|
143
|
-
|
|
143
|
+
const parts1 = parts[1];
|
|
144
144
|
if (parts0.endsWith('[]')) {
|
|
145
145
|
parts0 = parts0.slice(0, -2);
|
|
146
146
|
}
|
|
147
147
|
const toptype = params[parts0];
|
|
148
|
-
if ((toptype == null ? void 0 : toptype.type)
|
|
149
|
-
const typeObject = toptype.members.find(_ => (_ == null ? void 0 : _.type)
|
|
148
|
+
if ((toptype == null ? void 0 : toptype.type) === "union") {
|
|
149
|
+
const typeObject = toptype.members.find(_ => (_ == null ? void 0 : _.type) === 'object');
|
|
150
150
|
typeObject.properties[parts1] = simplifiedType;
|
|
151
|
-
} else if ((toptype == null ? void 0 : toptype.type)
|
|
151
|
+
} else if ((toptype == null ? void 0 : toptype.type) === "array") {
|
|
152
152
|
toptype.elementType.properties[parts1] = simplifiedType;
|
|
153
|
-
} else if ((toptype == null ? void 0 : toptype.type)
|
|
153
|
+
} else if ((toptype == null ? void 0 : toptype.type) === "object") {
|
|
154
154
|
toptype.properties = toptype.properties || {};
|
|
155
155
|
toptype.properties[parts1] = simplifiedType;
|
|
156
156
|
} else {
|
|
@@ -225,7 +225,7 @@ function parseJSDocTypedef(typedefs, warn, comment, expandType) {
|
|
|
225
225
|
return;
|
|
226
226
|
}
|
|
227
227
|
const lines = value.split('\n');
|
|
228
|
-
let lastTypedef
|
|
228
|
+
let lastTypedef;
|
|
229
229
|
for (let line of lines) {
|
|
230
230
|
line = line.trim();
|
|
231
231
|
if (line[0] === '*') {
|
|
@@ -279,8 +279,9 @@ class Stringifier {
|
|
|
279
279
|
constructor() {
|
|
280
280
|
this.forceCurly = false;
|
|
281
281
|
this.parents = [];
|
|
282
|
-
this.
|
|
282
|
+
this.lastCommentBlockIndex = -1;
|
|
283
283
|
this.lastCommentLineIndex = -1;
|
|
284
|
+
this.numSpaces = 0;
|
|
284
285
|
}
|
|
285
286
|
toSource(node) {
|
|
286
287
|
var _node$extra;
|
|
@@ -298,7 +299,7 @@ class Stringifier {
|
|
|
298
299
|
let out = '';
|
|
299
300
|
let comments = '';
|
|
300
301
|
if (leadingComments) {
|
|
301
|
-
comments
|
|
302
|
+
comments = this.leadingCommentsToSource(leadingComments);
|
|
302
303
|
}
|
|
303
304
|
if (node != null && (_node$extra = node.extra) != null && _node$extra.parenthesized) {
|
|
304
305
|
out += `(${comments}${this.toSource_(node)})`;
|
|
@@ -306,12 +307,7 @@ class Stringifier {
|
|
|
306
307
|
out += comments + this.toSource_(node);
|
|
307
308
|
}
|
|
308
309
|
if (trailingComments) {
|
|
309
|
-
|
|
310
|
-
if (trailingComment.type === 'CommentBlock') {
|
|
311
|
-
out += ' ';
|
|
312
|
-
}
|
|
313
|
-
out += this.toSource(trailingComment);
|
|
314
|
-
}
|
|
310
|
+
out += this.trailingCommentsToSource(trailingComments);
|
|
315
311
|
}
|
|
316
312
|
this.parents.pop();
|
|
317
313
|
return out;
|
|
@@ -337,6 +333,106 @@ class Stringifier {
|
|
|
337
333
|
console.warn('TODO ADD METHOD:', fn);
|
|
338
334
|
return `rtiUnhandled("${node.type}");\n`;
|
|
339
335
|
}
|
|
336
|
+
parentProvidesSpaces(reverseIndex) {
|
|
337
|
+
const {
|
|
338
|
+
parents
|
|
339
|
+
} = this;
|
|
340
|
+
const parent = parents[parents.length - reverseIndex];
|
|
341
|
+
return (parent == null ? void 0 : parent.type) === 'ArrayExpression';
|
|
342
|
+
}
|
|
343
|
+
CommentBlock(node) {
|
|
344
|
+
const {
|
|
345
|
+
loc
|
|
346
|
+
} = node;
|
|
347
|
+
let {
|
|
348
|
+
value
|
|
349
|
+
} = node;
|
|
350
|
+
if (this.lastCommentBlockIndex === loc.start.index) {
|
|
351
|
+
return '';
|
|
352
|
+
}
|
|
353
|
+
this.lastCommentBlockIndex = loc.start.index;
|
|
354
|
+
const {
|
|
355
|
+
spaces
|
|
356
|
+
} = this;
|
|
357
|
+
let out = '';
|
|
358
|
+
const dedicatedLine = spaces.length === loc.start.column;
|
|
359
|
+
if (dedicatedLine) {
|
|
360
|
+
out += spaces;
|
|
361
|
+
}
|
|
362
|
+
out += '/*';
|
|
363
|
+
if (value.includes('\n')) {
|
|
364
|
+
value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(_ => _.length).map((line, i) => (i ? spaces + ' ' : '') + line).join('\n');
|
|
365
|
+
out += `${value}\n${spaces} */`;
|
|
366
|
+
} else {
|
|
367
|
+
out += `${value}*/`;
|
|
368
|
+
}
|
|
369
|
+
if (dedicatedLine) {
|
|
370
|
+
out += '\n';
|
|
371
|
+
} else {
|
|
372
|
+
out += ' ';
|
|
373
|
+
}
|
|
374
|
+
return out;
|
|
375
|
+
}
|
|
376
|
+
CommentLine(node) {
|
|
377
|
+
const {
|
|
378
|
+
value,
|
|
379
|
+
loc
|
|
380
|
+
} = node;
|
|
381
|
+
if (this.lastCommentLineIndex === loc.start.index) {
|
|
382
|
+
return '';
|
|
383
|
+
}
|
|
384
|
+
this.lastCommentLineIndex = loc.start.index;
|
|
385
|
+
let out = '';
|
|
386
|
+
const {
|
|
387
|
+
spaces
|
|
388
|
+
} = this;
|
|
389
|
+
const dedicatedLine = spaces.length === loc.start.column;
|
|
390
|
+
if (dedicatedLine) {
|
|
391
|
+
if (!this.parentProvidesSpaces(2)) {
|
|
392
|
+
out += '\n';
|
|
393
|
+
out += spaces;
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
out += ' ';
|
|
397
|
+
}
|
|
398
|
+
out += `//${value}\n`;
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
leadingCommentsToSource(leadingComments) {
|
|
402
|
+
let out = leadingComments.map(_ => this.commentToSource(_, 'leading')).join('');
|
|
403
|
+
if (this.parentProvidesSpaces(2)) {
|
|
404
|
+
out += this.spaces;
|
|
405
|
+
}
|
|
406
|
+
return out;
|
|
407
|
+
}
|
|
408
|
+
trailingCommentsToSource(trailingComments) {
|
|
409
|
+
let out = '';
|
|
410
|
+
for (const trailingComment of trailingComments) {
|
|
411
|
+
if (trailingComment.type === 'CommentBlock') {
|
|
412
|
+
const {
|
|
413
|
+
loc
|
|
414
|
+
} = trailingComment;
|
|
415
|
+
const dedicatedLine = this.spaces.length === loc.start.column;
|
|
416
|
+
if (dedicatedLine) {
|
|
417
|
+
out += '\n';
|
|
418
|
+
} else {
|
|
419
|
+
out += ' ';
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
out += this.commentToSource(trailingComment, 'trailing');
|
|
423
|
+
}
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
commentToSource(comment, pos) {
|
|
427
|
+
if (comment.type === 'CommentBlock') {
|
|
428
|
+
return this.CommentBlock(comment);
|
|
429
|
+
} else if (comment.type === 'CommentLine') {
|
|
430
|
+
return this.CommentLine(comment);
|
|
431
|
+
}
|
|
432
|
+
console.warn("Unknown comment type", comment);
|
|
433
|
+
debugger;
|
|
434
|
+
return '';
|
|
435
|
+
}
|
|
340
436
|
toSourceCurly(node) {
|
|
341
437
|
if (!this.forceCurly) {
|
|
342
438
|
return this.toSource(node);
|
|
@@ -344,7 +440,7 @@ class Stringifier {
|
|
|
344
440
|
const {
|
|
345
441
|
type
|
|
346
442
|
} = node;
|
|
347
|
-
const needCurly = type
|
|
443
|
+
const needCurly = type !== 'BlockStatement' && type !== 'EmptyStatement';
|
|
348
444
|
const spaces = this.spaces;
|
|
349
445
|
let out = '';
|
|
350
446
|
if (needCurly) {
|
|
@@ -367,16 +463,13 @@ class Stringifier {
|
|
|
367
463
|
}
|
|
368
464
|
get needSpaces() {
|
|
369
465
|
const t = this.parentType;
|
|
370
|
-
return t
|
|
466
|
+
return t !== "ForStatement" && t !== "ForInStatement" && t !== "ForOfStatement";
|
|
371
467
|
}
|
|
372
468
|
mapToSource(arr) {
|
|
373
469
|
return arr.map(_ => this.toSource(_));
|
|
374
470
|
}
|
|
375
471
|
generateTypeChecks(node) {
|
|
376
|
-
|
|
377
|
-
spaces
|
|
378
|
-
} = this;
|
|
379
|
-
return `${spaces}/* Stub of Stringifier#generateTypeChecks({type=${node.type}}); */\n`;
|
|
472
|
+
return '';
|
|
380
473
|
}
|
|
381
474
|
get spaces() {
|
|
382
475
|
return ' '.repeat(this.numSpaces);
|
|
@@ -685,9 +778,7 @@ class Stringifier {
|
|
|
685
778
|
} = node;
|
|
686
779
|
const spaces = this.spaces;
|
|
687
780
|
let out = '';
|
|
688
|
-
|
|
689
|
-
out += spaces;
|
|
690
|
-
}
|
|
781
|
+
out += spaces;
|
|
691
782
|
out += `if (${this.toSource(test)})`;
|
|
692
783
|
out += this.toSourceCurly(consequent);
|
|
693
784
|
if (alternate) {
|
|
@@ -846,10 +937,24 @@ class Stringifier {
|
|
|
846
937
|
ArrayExpression(node) {
|
|
847
938
|
const {
|
|
848
939
|
elements,
|
|
849
|
-
extra
|
|
940
|
+
extra,
|
|
941
|
+
loc
|
|
850
942
|
} = node;
|
|
851
943
|
const e = !!(extra != null && extra.trailingComma) ? ',' : '';
|
|
852
|
-
|
|
944
|
+
const sameLine = loc.start.line === loc.end.line;
|
|
945
|
+
if (sameLine) {
|
|
946
|
+
return '[' + this.mapToSource(elements).join(', ') + e + ']';
|
|
947
|
+
}
|
|
948
|
+
let out = '[\n';
|
|
949
|
+
this.numSpaces++;
|
|
950
|
+
const {
|
|
951
|
+
spaces
|
|
952
|
+
} = this;
|
|
953
|
+
out += this.mapToSource(elements).map(_ => spaces + _).join(',\n');
|
|
954
|
+
this.numSpaces--;
|
|
955
|
+
out += e;
|
|
956
|
+
out += '\n' + this.spaces + ']';
|
|
957
|
+
return out;
|
|
853
958
|
}
|
|
854
959
|
VariableDeclaration(node) {
|
|
855
960
|
const {
|
|
@@ -920,9 +1025,8 @@ class Stringifier {
|
|
|
920
1025
|
} = node;
|
|
921
1026
|
if (prefix) {
|
|
922
1027
|
return `${operator}${this.toSource(argument)}`;
|
|
923
|
-
} else {
|
|
924
|
-
return `${this.toSource(argument)}${operator}`;
|
|
925
1028
|
}
|
|
1029
|
+
return `${this.toSource(argument)}${operator}`;
|
|
926
1030
|
}
|
|
927
1031
|
NewExpression(node) {
|
|
928
1032
|
const {
|
|
@@ -1282,56 +1386,6 @@ class Stringifier {
|
|
|
1282
1386
|
DebuggerStatement(node) {
|
|
1283
1387
|
return `${this.spaces}debugger;`;
|
|
1284
1388
|
}
|
|
1285
|
-
CommentBlock(node) {
|
|
1286
|
-
let {
|
|
1287
|
-
value,
|
|
1288
|
-
loc
|
|
1289
|
-
} = node;
|
|
1290
|
-
const {
|
|
1291
|
-
spaces,
|
|
1292
|
-
parents
|
|
1293
|
-
} = this;
|
|
1294
|
-
let out = '';
|
|
1295
|
-
const dedicatedLine = spaces.length === loc.start.column;
|
|
1296
|
-
if (dedicatedLine) {
|
|
1297
|
-
out += spaces;
|
|
1298
|
-
}
|
|
1299
|
-
out += '/*';
|
|
1300
|
-
if (value.includes('\n')) {
|
|
1301
|
-
value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(_ => _.length).map((line, i) => (i ? spaces + ' ' : '') + line).join('\n');
|
|
1302
|
-
out += `${value}\n${spaces} */`;
|
|
1303
|
-
} else {
|
|
1304
|
-
out += `${value}*/`;
|
|
1305
|
-
}
|
|
1306
|
-
if (dedicatedLine) {
|
|
1307
|
-
out += '\n';
|
|
1308
|
-
} else {
|
|
1309
|
-
out += ' ';
|
|
1310
|
-
}
|
|
1311
|
-
return out;
|
|
1312
|
-
}
|
|
1313
|
-
CommentLine(node) {
|
|
1314
|
-
const {
|
|
1315
|
-
value,
|
|
1316
|
-
loc
|
|
1317
|
-
} = node;
|
|
1318
|
-
if (this.lastCommentLineIndex === loc.start.index) {
|
|
1319
|
-
return '';
|
|
1320
|
-
}
|
|
1321
|
-
this.lastCommentLineIndex = loc.start.index;
|
|
1322
|
-
let out = '';
|
|
1323
|
-
const {
|
|
1324
|
-
spaces
|
|
1325
|
-
} = this;
|
|
1326
|
-
const dedicatedLine = spaces.length === loc.start.column;
|
|
1327
|
-
if (dedicatedLine) {
|
|
1328
|
-
out += '\n' + spaces;
|
|
1329
|
-
} else {
|
|
1330
|
-
out += ' ';
|
|
1331
|
-
}
|
|
1332
|
-
out += `//${value}\n`;
|
|
1333
|
-
return out;
|
|
1334
|
-
}
|
|
1335
1389
|
Import(node) {
|
|
1336
1390
|
return 'import';
|
|
1337
1391
|
}
|
|
@@ -1379,8 +1433,6 @@ class Stringifier {
|
|
|
1379
1433
|
}
|
|
1380
1434
|
Program(node) {
|
|
1381
1435
|
const {
|
|
1382
|
-
sourceType,
|
|
1383
|
-
interpreter,
|
|
1384
1436
|
body,
|
|
1385
1437
|
directives
|
|
1386
1438
|
} = node;
|
|
@@ -1535,7 +1587,7 @@ class Asserter extends Stringifier {
|
|
|
1535
1587
|
return out;
|
|
1536
1588
|
}
|
|
1537
1589
|
findParentOfType(node, type) {
|
|
1538
|
-
const currentIndex = this.parents.findLastIndex(_ => _
|
|
1590
|
+
const currentIndex = this.parents.findLastIndex(_ => _ === node);
|
|
1539
1591
|
return this.parents.findLast((_, i) => {
|
|
1540
1592
|
if (i > currentIndex) {
|
|
1541
1593
|
return false;
|
|
@@ -1547,7 +1599,7 @@ class Asserter extends Stringifier {
|
|
|
1547
1599
|
const {
|
|
1548
1600
|
parents
|
|
1549
1601
|
} = this;
|
|
1550
|
-
let i = parents.findLastIndex(_ => _
|
|
1602
|
+
let i = parents.findLastIndex(_ => _ === node);
|
|
1551
1603
|
let parent = parents[i];
|
|
1552
1604
|
if (parent.leadingComments) {
|
|
1553
1605
|
return parent;
|
|
@@ -1584,7 +1636,7 @@ class Asserter extends Stringifier {
|
|
|
1584
1636
|
}
|
|
1585
1637
|
node = this.findParentOfType(node, 'ExpressionStatement');
|
|
1586
1638
|
if (!node) {
|
|
1587
|
-
node = this.parents.findLast(_ => _.type
|
|
1639
|
+
node = this.parents.findLast(_ => _.type === 'VariableDeclaration');
|
|
1588
1640
|
if (!node) {
|
|
1589
1641
|
return;
|
|
1590
1642
|
}
|
|
@@ -1605,7 +1657,7 @@ class Asserter extends Stringifier {
|
|
|
1605
1657
|
}
|
|
1606
1658
|
if (leadingComments && leadingComments.length) {
|
|
1607
1659
|
const lastComment = leadingComments[leadingComments.length - 1];
|
|
1608
|
-
if (lastComment.type
|
|
1660
|
+
if (lastComment.type === "CommentBlock") {
|
|
1609
1661
|
if (lastComment.value.includes('@event')) {
|
|
1610
1662
|
return;
|
|
1611
1663
|
}
|
|
@@ -1696,11 +1748,10 @@ class Asserter extends Stringifier {
|
|
|
1696
1748
|
return node.name === name;
|
|
1697
1749
|
} else if (type === 'ArrayPattern' || type === 'ObjectPattern' || type === 'RestElement') {
|
|
1698
1750
|
return false;
|
|
1699
|
-
} else {
|
|
1700
|
-
const _ = new Stringifier();
|
|
1701
|
-
const code = _.toSource(node);
|
|
1702
|
-
console.log("Unknown type to test params for", type, code);
|
|
1703
1751
|
}
|
|
1752
|
+
const _ = new Stringifier();
|
|
1753
|
+
const code = _.toSource(node);
|
|
1754
|
+
console.log("Unknown type to test params for", type, code);
|
|
1704
1755
|
return false;
|
|
1705
1756
|
});
|
|
1706
1757
|
}
|
|
@@ -1816,7 +1867,7 @@ class Asserter extends Stringifier {
|
|
|
1816
1867
|
return toSource(id);
|
|
1817
1868
|
case 'ClassMethod':
|
|
1818
1869
|
case 'ClassPrivateMethod':
|
|
1819
|
-
const classDecl = this.parents.findLast(_ => _.type
|
|
1870
|
+
const classDecl = this.parents.findLast(_ => _.type === 'ClassDeclaration');
|
|
1820
1871
|
let out = '';
|
|
1821
1872
|
if (classDecl) {
|
|
1822
1873
|
out += toSource(classDecl.id) + '#';
|
|
@@ -1836,9 +1887,8 @@ class Asserter extends Stringifier {
|
|
|
1836
1887
|
const parent = this.findParentOfType(node, 'VariableDeclarator');
|
|
1837
1888
|
if (parent) {
|
|
1838
1889
|
return toSource(parent.id);
|
|
1839
|
-
} else {
|
|
1840
|
-
return 'getName> missing parent for ' + node.type;
|
|
1841
1890
|
}
|
|
1891
|
+
return 'getName> missing parent for ' + node.type;
|
|
1842
1892
|
case 'ObjectMethod':
|
|
1843
1893
|
return toSource(key);
|
|
1844
1894
|
default:
|
|
@@ -1864,7 +1914,6 @@ class Asserter extends Stringifier {
|
|
|
1864
1914
|
}
|
|
1865
1915
|
File(node) {
|
|
1866
1916
|
const {
|
|
1867
|
-
errors,
|
|
1868
1917
|
program,
|
|
1869
1918
|
comments
|
|
1870
1919
|
} = node;
|
|
@@ -1902,8 +1951,8 @@ function addTypeChecks(src, options) {
|
|
|
1902
1951
|
|
|
1903
1952
|
function ast2json(ast) {
|
|
1904
1953
|
return JSON.stringify(ast, function (name, val) {
|
|
1905
|
-
if (name
|
|
1906
|
-
return
|
|
1954
|
+
if (name === "loc" || name === "start" || name === "end") {
|
|
1955
|
+
return;
|
|
1907
1956
|
}
|
|
1908
1957
|
return val;
|
|
1909
1958
|
}, 2);
|
|
@@ -1944,7 +1993,7 @@ function parseType(str) {
|
|
|
1944
1993
|
str += '[]';
|
|
1945
1994
|
}
|
|
1946
1995
|
str = `type tmp = ${str};`;
|
|
1947
|
-
const ast = ts
|
|
1996
|
+
const ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, true);
|
|
1948
1997
|
return ast.statements[0].type;
|
|
1949
1998
|
}
|
|
1950
1999
|
function toSourceTS(node) {
|
|
@@ -1952,7 +2001,7 @@ function toSourceTS(node) {
|
|
|
1952
2001
|
typeArguments,
|
|
1953
2002
|
typeName
|
|
1954
2003
|
} = node;
|
|
1955
|
-
const kind_ = ts
|
|
2004
|
+
const kind_ = ts.SyntaxKind[node.kind];
|
|
1956
2005
|
const {
|
|
1957
2006
|
AnyKeyword,
|
|
1958
2007
|
ArrayType,
|
|
@@ -1981,9 +2030,22 @@ function toSourceTS(node) {
|
|
|
1981
2030
|
JSDocNullableType,
|
|
1982
2031
|
TrueKeyword,
|
|
1983
2032
|
FalseKeyword,
|
|
1984
|
-
VoidKeyword
|
|
1985
|
-
|
|
2033
|
+
VoidKeyword,
|
|
2034
|
+
UnknownKeyword,
|
|
2035
|
+
NeverKeyword,
|
|
2036
|
+
ConstructorType
|
|
2037
|
+
} = ts.SyntaxKind;
|
|
1986
2038
|
switch (node.kind) {
|
|
2039
|
+
case ConstructorType:
|
|
2040
|
+
{
|
|
2041
|
+
const _parameters = node.parameters.map(toSourceTS);
|
|
2042
|
+
const _ret = toSourceTS(node.type);
|
|
2043
|
+
return {
|
|
2044
|
+
type: 'new',
|
|
2045
|
+
parameters: _parameters,
|
|
2046
|
+
ret: _ret
|
|
2047
|
+
};
|
|
2048
|
+
}
|
|
1987
2049
|
case FunctionType:
|
|
1988
2050
|
const parameters = node.parameters.map(toSourceTS);
|
|
1989
2051
|
return {
|
|
@@ -2045,15 +2107,14 @@ function toSourceTS(node) {
|
|
|
2045
2107
|
type: 'class',
|
|
2046
2108
|
elementType: toSourceTS(typeArguments[0])
|
|
2047
2109
|
};
|
|
2048
|
-
} else {
|
|
2049
|
-
if (!typeArguments) {
|
|
2050
|
-
return typeName.getText();
|
|
2051
|
-
}
|
|
2052
|
-
console.warn('unhandled TypeReference', kind_, node);
|
|
2053
|
-
return {
|
|
2054
|
-
type: 'unhandled TypeReference'
|
|
2055
|
-
};
|
|
2056
2110
|
}
|
|
2111
|
+
if (!typeArguments) {
|
|
2112
|
+
return typeName.getText();
|
|
2113
|
+
}
|
|
2114
|
+
console.warn('unhandled TypeReference', kind_, node);
|
|
2115
|
+
return {
|
|
2116
|
+
type: 'unhandled TypeReference'
|
|
2117
|
+
};
|
|
2057
2118
|
case StringKeyword:
|
|
2058
2119
|
return node.getText();
|
|
2059
2120
|
case NumberKeyword:
|
|
@@ -2107,6 +2168,8 @@ function toSourceTS(node) {
|
|
|
2107
2168
|
case TrueKeyword:
|
|
2108
2169
|
case FalseKeyword:
|
|
2109
2170
|
case VoidKeyword:
|
|
2171
|
+
case UnknownKeyword:
|
|
2172
|
+
case NeverKeyword:
|
|
2110
2173
|
return node.getText();
|
|
2111
2174
|
case ObjectKeyword:
|
|
2112
2175
|
return {
|
|
@@ -2138,14 +2201,13 @@ function parseTypeBabelTS(str) {
|
|
|
2138
2201
|
return ast.program.body[0].typeAnnotation;
|
|
2139
2202
|
}
|
|
2140
2203
|
function toSourceBabelTS(node) {
|
|
2141
|
-
const {
|
|
2142
|
-
typeName
|
|
2143
|
-
} = node;
|
|
2144
|
-
const kind_ = ts.SyntaxKind[node.kind];
|
|
2145
2204
|
switch (node.type) {
|
|
2146
2205
|
case 'TSTypeReference':
|
|
2147
2206
|
{
|
|
2148
2207
|
const name = toSourceBabelTS(node.typeName);
|
|
2208
|
+
if (!node.typeParameters) {
|
|
2209
|
+
return node.typeName.name;
|
|
2210
|
+
}
|
|
2149
2211
|
console.assert(node.typeParameters.type === 'TSTypeParameterInstantiation');
|
|
2150
2212
|
const typeArguments = node.typeParameters.params;
|
|
2151
2213
|
if ((name === 'Object' || name === 'Record') && (typeArguments == null ? void 0 : typeArguments.length) === 2) {
|
|
@@ -2182,15 +2244,11 @@ function toSourceBabelTS(node) {
|
|
|
2182
2244
|
type: 'class',
|
|
2183
2245
|
elementType: toSourceBabelTS(typeArguments[0])
|
|
2184
2246
|
};
|
|
2185
|
-
} else {
|
|
2186
|
-
if (!typeArguments) {
|
|
2187
|
-
return typeName.name;
|
|
2188
|
-
}
|
|
2189
|
-
console.warn('unhandled TypeReference', kind_, node);
|
|
2190
|
-
return {
|
|
2191
|
-
type: 'unhandled TypeReference'
|
|
2192
|
-
};
|
|
2193
2247
|
}
|
|
2248
|
+
console.warn('unhandled TypeReference', node);
|
|
2249
|
+
return {
|
|
2250
|
+
type: 'unhandled TypeReference'
|
|
2251
|
+
};
|
|
2194
2252
|
}
|
|
2195
2253
|
case 'TSStringKeyword':
|
|
2196
2254
|
return 'string';
|
|
@@ -2244,6 +2302,10 @@ function toSourceBabelTS(node) {
|
|
|
2244
2302
|
return node.extra.raw;
|
|
2245
2303
|
case 'TSUndefinedKeyword':
|
|
2246
2304
|
return 'undefined';
|
|
2305
|
+
case 'TSUnknownKeyword':
|
|
2306
|
+
return 'unknown';
|
|
2307
|
+
case 'TSNeverKeyword':
|
|
2308
|
+
return 'never';
|
|
2247
2309
|
case 'TSVoidKeyword':
|
|
2248
2310
|
return 'void';
|
|
2249
2311
|
case 'TSThisType':
|
package/package.json
CHANGED