@runtime-type-inspector/transpiler 2.0.9 → 2.0.11
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 +221 -136
- package/index.mjs +204 -129
- 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);
|
|
@@ -2299,6 +2355,19 @@
|
|
|
2299
2355
|
return out;
|
|
2300
2356
|
}
|
|
2301
2357
|
|
|
2358
|
+
function compareAST(left, right) {
|
|
2359
|
+
var l = parser.parse(left, {
|
|
2360
|
+
sourceType: 'module'
|
|
2361
|
+
});
|
|
2362
|
+
var r = parser.parse(right, {
|
|
2363
|
+
sourceType: 'module'
|
|
2364
|
+
});
|
|
2365
|
+
var ljson = ast2jsonForComparison(l);
|
|
2366
|
+
var rjson = ast2jsonForComparison(r);
|
|
2367
|
+
var test = ljson === rjson;
|
|
2368
|
+
return test;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2302
2371
|
function expandType(type) {
|
|
2303
2372
|
var ast = parseType(type);
|
|
2304
2373
|
return toSourceTS(ast);
|
|
@@ -2309,14 +2378,14 @@
|
|
|
2309
2378
|
str += '[]';
|
|
2310
2379
|
}
|
|
2311
2380
|
str = "type tmp = ".concat(str, ";");
|
|
2312
|
-
var ast = ts
|
|
2381
|
+
var ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, true);
|
|
2313
2382
|
return ast.statements[0].type;
|
|
2314
2383
|
}
|
|
2315
2384
|
function toSourceTS(node) {
|
|
2316
2385
|
var typeArguments = node.typeArguments,
|
|
2317
2386
|
typeName = node.typeName;
|
|
2318
|
-
var kind_ = ts
|
|
2319
|
-
var _ts$SyntaxKind = ts
|
|
2387
|
+
var kind_ = ts.SyntaxKind[node.kind];
|
|
2388
|
+
var _ts$SyntaxKind = ts.SyntaxKind,
|
|
2320
2389
|
AnyKeyword = _ts$SyntaxKind.AnyKeyword,
|
|
2321
2390
|
ArrayType = _ts$SyntaxKind.ArrayType,
|
|
2322
2391
|
BooleanKeyword = _ts$SyntaxKind.BooleanKeyword,
|
|
@@ -2344,8 +2413,21 @@
|
|
|
2344
2413
|
JSDocNullableType = _ts$SyntaxKind.JSDocNullableType,
|
|
2345
2414
|
TrueKeyword = _ts$SyntaxKind.TrueKeyword,
|
|
2346
2415
|
FalseKeyword = _ts$SyntaxKind.FalseKeyword,
|
|
2347
|
-
VoidKeyword = _ts$SyntaxKind.VoidKeyword
|
|
2416
|
+
VoidKeyword = _ts$SyntaxKind.VoidKeyword,
|
|
2417
|
+
UnknownKeyword = _ts$SyntaxKind.UnknownKeyword,
|
|
2418
|
+
NeverKeyword = _ts$SyntaxKind.NeverKeyword,
|
|
2419
|
+
ConstructorType = _ts$SyntaxKind.ConstructorType;
|
|
2348
2420
|
switch (node.kind) {
|
|
2421
|
+
case ConstructorType:
|
|
2422
|
+
{
|
|
2423
|
+
var _parameters = node.parameters.map(toSourceTS);
|
|
2424
|
+
var _ret = toSourceTS(node.type);
|
|
2425
|
+
return {
|
|
2426
|
+
type: 'new',
|
|
2427
|
+
parameters: _parameters,
|
|
2428
|
+
ret: _ret
|
|
2429
|
+
};
|
|
2430
|
+
}
|
|
2349
2431
|
case FunctionType:
|
|
2350
2432
|
var parameters = node.parameters.map(toSourceTS);
|
|
2351
2433
|
return {
|
|
@@ -2407,15 +2489,14 @@
|
|
|
2407
2489
|
type: 'class',
|
|
2408
2490
|
elementType: toSourceTS(typeArguments[0])
|
|
2409
2491
|
};
|
|
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
2492
|
}
|
|
2493
|
+
if (!typeArguments) {
|
|
2494
|
+
return typeName.getText();
|
|
2495
|
+
}
|
|
2496
|
+
console.warn('unhandled TypeReference', kind_, node);
|
|
2497
|
+
return {
|
|
2498
|
+
type: 'unhandled TypeReference'
|
|
2499
|
+
};
|
|
2419
2500
|
case StringKeyword:
|
|
2420
2501
|
return node.getText();
|
|
2421
2502
|
case NumberKeyword:
|
|
@@ -2469,6 +2550,8 @@
|
|
|
2469
2550
|
case TrueKeyword:
|
|
2470
2551
|
case FalseKeyword:
|
|
2471
2552
|
case VoidKeyword:
|
|
2553
|
+
case UnknownKeyword:
|
|
2554
|
+
case NeverKeyword:
|
|
2472
2555
|
return node.getText();
|
|
2473
2556
|
case ObjectKeyword:
|
|
2474
2557
|
return {
|
|
@@ -2500,12 +2583,13 @@
|
|
|
2500
2583
|
return ast.program.body[0].typeAnnotation;
|
|
2501
2584
|
}
|
|
2502
2585
|
function toSourceBabelTS(node) {
|
|
2503
|
-
var typeName = node.typeName;
|
|
2504
|
-
var kind_ = ts.SyntaxKind[node.kind];
|
|
2505
2586
|
switch (node.type) {
|
|
2506
2587
|
case 'TSTypeReference':
|
|
2507
2588
|
{
|
|
2508
2589
|
var name = toSourceBabelTS(node.typeName);
|
|
2590
|
+
if (!node.typeParameters) {
|
|
2591
|
+
return node.typeName.name;
|
|
2592
|
+
}
|
|
2509
2593
|
console.assert(node.typeParameters.type === 'TSTypeParameterInstantiation');
|
|
2510
2594
|
var typeArguments = node.typeParameters.params;
|
|
2511
2595
|
if ((name === 'Object' || name === 'Record') && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 2) {
|
|
@@ -2542,15 +2626,11 @@
|
|
|
2542
2626
|
type: 'class',
|
|
2543
2627
|
elementType: toSourceBabelTS(typeArguments[0])
|
|
2544
2628
|
};
|
|
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
2629
|
}
|
|
2630
|
+
console.warn('unhandled TypeReference', node);
|
|
2631
|
+
return {
|
|
2632
|
+
type: 'unhandled TypeReference'
|
|
2633
|
+
};
|
|
2554
2634
|
}
|
|
2555
2635
|
case 'TSStringKeyword':
|
|
2556
2636
|
return 'string';
|
|
@@ -2604,6 +2684,10 @@
|
|
|
2604
2684
|
return node.extra.raw;
|
|
2605
2685
|
case 'TSUndefinedKeyword':
|
|
2606
2686
|
return 'undefined';
|
|
2687
|
+
case 'TSUnknownKeyword':
|
|
2688
|
+
return 'unknown';
|
|
2689
|
+
case 'TSNeverKeyword':
|
|
2690
|
+
return 'never';
|
|
2607
2691
|
case 'TSVoidKeyword':
|
|
2608
2692
|
return 'void';
|
|
2609
2693
|
case 'TSThisType':
|
|
@@ -2629,6 +2713,7 @@
|
|
|
2629
2713
|
exports.ast2json = ast2json;
|
|
2630
2714
|
exports.ast2jsonForComparison = ast2jsonForComparison;
|
|
2631
2715
|
exports.code2ast2code = code2ast2code;
|
|
2716
|
+
exports.compareAST = compareAST;
|
|
2632
2717
|
exports.expandType = expandType;
|
|
2633
2718
|
exports.expandTypeBabelTS = expandTypeBabelTS;
|
|
2634
2719
|
exports.expandTypeDepFree = expandTypeDepFree;
|
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);
|
|
@@ -1934,6 +1983,19 @@ function code2ast2code(code) {
|
|
|
1934
1983
|
return out;
|
|
1935
1984
|
}
|
|
1936
1985
|
|
|
1986
|
+
function compareAST(left, right) {
|
|
1987
|
+
const l = parse(left, {
|
|
1988
|
+
sourceType: 'module'
|
|
1989
|
+
});
|
|
1990
|
+
const r = parse(right, {
|
|
1991
|
+
sourceType: 'module'
|
|
1992
|
+
});
|
|
1993
|
+
const ljson = ast2jsonForComparison(l);
|
|
1994
|
+
const rjson = ast2jsonForComparison(r);
|
|
1995
|
+
const test = ljson === rjson;
|
|
1996
|
+
return test;
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1937
1999
|
function expandType(type) {
|
|
1938
2000
|
const ast = parseType(type);
|
|
1939
2001
|
return toSourceTS(ast);
|
|
@@ -1944,7 +2006,7 @@ function parseType(str) {
|
|
|
1944
2006
|
str += '[]';
|
|
1945
2007
|
}
|
|
1946
2008
|
str = `type tmp = ${str};`;
|
|
1947
|
-
const ast = ts
|
|
2009
|
+
const ast = ts.createSourceFile('repl.ts', str, ts.ScriptTarget.Latest, true);
|
|
1948
2010
|
return ast.statements[0].type;
|
|
1949
2011
|
}
|
|
1950
2012
|
function toSourceTS(node) {
|
|
@@ -1952,7 +2014,7 @@ function toSourceTS(node) {
|
|
|
1952
2014
|
typeArguments,
|
|
1953
2015
|
typeName
|
|
1954
2016
|
} = node;
|
|
1955
|
-
const kind_ = ts
|
|
2017
|
+
const kind_ = ts.SyntaxKind[node.kind];
|
|
1956
2018
|
const {
|
|
1957
2019
|
AnyKeyword,
|
|
1958
2020
|
ArrayType,
|
|
@@ -1981,9 +2043,22 @@ function toSourceTS(node) {
|
|
|
1981
2043
|
JSDocNullableType,
|
|
1982
2044
|
TrueKeyword,
|
|
1983
2045
|
FalseKeyword,
|
|
1984
|
-
VoidKeyword
|
|
1985
|
-
|
|
2046
|
+
VoidKeyword,
|
|
2047
|
+
UnknownKeyword,
|
|
2048
|
+
NeverKeyword,
|
|
2049
|
+
ConstructorType
|
|
2050
|
+
} = ts.SyntaxKind;
|
|
1986
2051
|
switch (node.kind) {
|
|
2052
|
+
case ConstructorType:
|
|
2053
|
+
{
|
|
2054
|
+
const _parameters = node.parameters.map(toSourceTS);
|
|
2055
|
+
const _ret = toSourceTS(node.type);
|
|
2056
|
+
return {
|
|
2057
|
+
type: 'new',
|
|
2058
|
+
parameters: _parameters,
|
|
2059
|
+
ret: _ret
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
1987
2062
|
case FunctionType:
|
|
1988
2063
|
const parameters = node.parameters.map(toSourceTS);
|
|
1989
2064
|
return {
|
|
@@ -2045,15 +2120,14 @@ function toSourceTS(node) {
|
|
|
2045
2120
|
type: 'class',
|
|
2046
2121
|
elementType: toSourceTS(typeArguments[0])
|
|
2047
2122
|
};
|
|
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
2123
|
}
|
|
2124
|
+
if (!typeArguments) {
|
|
2125
|
+
return typeName.getText();
|
|
2126
|
+
}
|
|
2127
|
+
console.warn('unhandled TypeReference', kind_, node);
|
|
2128
|
+
return {
|
|
2129
|
+
type: 'unhandled TypeReference'
|
|
2130
|
+
};
|
|
2057
2131
|
case StringKeyword:
|
|
2058
2132
|
return node.getText();
|
|
2059
2133
|
case NumberKeyword:
|
|
@@ -2107,6 +2181,8 @@ function toSourceTS(node) {
|
|
|
2107
2181
|
case TrueKeyword:
|
|
2108
2182
|
case FalseKeyword:
|
|
2109
2183
|
case VoidKeyword:
|
|
2184
|
+
case UnknownKeyword:
|
|
2185
|
+
case NeverKeyword:
|
|
2110
2186
|
return node.getText();
|
|
2111
2187
|
case ObjectKeyword:
|
|
2112
2188
|
return {
|
|
@@ -2138,14 +2214,13 @@ function parseTypeBabelTS(str) {
|
|
|
2138
2214
|
return ast.program.body[0].typeAnnotation;
|
|
2139
2215
|
}
|
|
2140
2216
|
function toSourceBabelTS(node) {
|
|
2141
|
-
const {
|
|
2142
|
-
typeName
|
|
2143
|
-
} = node;
|
|
2144
|
-
const kind_ = ts.SyntaxKind[node.kind];
|
|
2145
2217
|
switch (node.type) {
|
|
2146
2218
|
case 'TSTypeReference':
|
|
2147
2219
|
{
|
|
2148
2220
|
const name = toSourceBabelTS(node.typeName);
|
|
2221
|
+
if (!node.typeParameters) {
|
|
2222
|
+
return node.typeName.name;
|
|
2223
|
+
}
|
|
2149
2224
|
console.assert(node.typeParameters.type === 'TSTypeParameterInstantiation');
|
|
2150
2225
|
const typeArguments = node.typeParameters.params;
|
|
2151
2226
|
if ((name === 'Object' || name === 'Record') && (typeArguments == null ? void 0 : typeArguments.length) === 2) {
|
|
@@ -2182,15 +2257,11 @@ function toSourceBabelTS(node) {
|
|
|
2182
2257
|
type: 'class',
|
|
2183
2258
|
elementType: toSourceBabelTS(typeArguments[0])
|
|
2184
2259
|
};
|
|
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
2260
|
}
|
|
2261
|
+
console.warn('unhandled TypeReference', node);
|
|
2262
|
+
return {
|
|
2263
|
+
type: 'unhandled TypeReference'
|
|
2264
|
+
};
|
|
2194
2265
|
}
|
|
2195
2266
|
case 'TSStringKeyword':
|
|
2196
2267
|
return 'string';
|
|
@@ -2244,6 +2315,10 @@ function toSourceBabelTS(node) {
|
|
|
2244
2315
|
return node.extra.raw;
|
|
2245
2316
|
case 'TSUndefinedKeyword':
|
|
2246
2317
|
return 'undefined';
|
|
2318
|
+
case 'TSUnknownKeyword':
|
|
2319
|
+
return 'unknown';
|
|
2320
|
+
case 'TSNeverKeyword':
|
|
2321
|
+
return 'never';
|
|
2247
2322
|
case 'TSVoidKeyword':
|
|
2248
2323
|
return 'void';
|
|
2249
2324
|
case 'TSThisType':
|
|
@@ -2263,4 +2338,4 @@ function toSourceBabelTS(node) {
|
|
|
2263
2338
|
}
|
|
2264
2339
|
}
|
|
2265
2340
|
|
|
2266
|
-
export { Asserter, Stringifier, addTypeChecks, ast2json, ast2jsonForComparison, code2ast2code, expandType, expandTypeBabelTS, expandTypeDepFree, extractCurlyContent, extractNameAndOptionality, nodeIsFunction, parseJSDoc, parseJSDocSetter, parseJSDocTypedef, parseType, parseTypeBabelTS, simplifyType, toSourceBabelTS, toSourceTS, trimEndSpaces };
|
|
2341
|
+
export { Asserter, Stringifier, addTypeChecks, ast2json, ast2jsonForComparison, code2ast2code, compareAST, expandType, expandTypeBabelTS, expandTypeDepFree, extractCurlyContent, extractNameAndOptionality, nodeIsFunction, parseJSDoc, parseJSDocSetter, parseJSDocTypedef, parseType, parseTypeBabelTS, simplifyType, toSourceBabelTS, toSourceTS, trimEndSpaces };
|
package/package.json
CHANGED