@runtime-type-inspector/transpiler 4.0.0 → 4.0.2
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 +188 -29
- package/index.mjs +170 -27
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -320,11 +320,14 @@
|
|
|
320
320
|
* This function handles various TypeScript AST node types and converts them into a string
|
|
321
321
|
* or an object representing the type.
|
|
322
322
|
*
|
|
323
|
-
* @param {ts.TypeNode|ts.Identifier} node - The TypeScript AST node to convert.
|
|
323
|
+
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName|undefined} node - The TypeScript AST node to convert.
|
|
324
324
|
* @returns {string | number | boolean | {type: string, [key: string]: any} | undefined} The source string/number,
|
|
325
325
|
* or an object with type information based on the node, or `undefined` if the node kind is not handled.
|
|
326
326
|
*/
|
|
327
327
|
function toSourceTS(node) {
|
|
328
|
+
if (!node) {
|
|
329
|
+
return 'undefined';
|
|
330
|
+
}
|
|
328
331
|
var typeArguments = node.typeArguments,
|
|
329
332
|
typeName = node.typeName;
|
|
330
333
|
var kind_ = ts.SyntaxKind[node.kind];
|
|
@@ -333,6 +336,7 @@
|
|
|
333
336
|
ArrayType = _ts$SyntaxKind.ArrayType,
|
|
334
337
|
BooleanKeyword = _ts$SyntaxKind.BooleanKeyword,
|
|
335
338
|
FunctionType = _ts$SyntaxKind.FunctionType,
|
|
339
|
+
JSDocFunctionType = _ts$SyntaxKind.JSDocFunctionType,
|
|
336
340
|
Identifier = _ts$SyntaxKind.Identifier,
|
|
337
341
|
IntersectionType = _ts$SyntaxKind.IntersectionType,
|
|
338
342
|
JSDocAllType = _ts$SyntaxKind.JSDocAllType,
|
|
@@ -371,7 +375,8 @@
|
|
|
371
375
|
ConstructorType = _ts$SyntaxKind.ConstructorType,
|
|
372
376
|
NamedTupleMember = _ts$SyntaxKind.NamedTupleMember,
|
|
373
377
|
MappedType = _ts$SyntaxKind.MappedType,
|
|
374
|
-
TypeParameter = _ts$SyntaxKind.TypeParameter
|
|
378
|
+
TypeParameter = _ts$SyntaxKind.TypeParameter,
|
|
379
|
+
QualifiedName = _ts$SyntaxKind.QualifiedName;
|
|
375
380
|
// console.log({typeArguments, typeName, kind_, node});
|
|
376
381
|
switch (node.kind) {
|
|
377
382
|
case BigIntKeyword:
|
|
@@ -410,23 +415,36 @@
|
|
|
410
415
|
if (!ts.isConstructorTypeNode(node)) {
|
|
411
416
|
throw Error("Impossible");
|
|
412
417
|
}
|
|
413
|
-
var
|
|
418
|
+
var parameters = node.parameters.map(toSourceTS);
|
|
414
419
|
var _ret = toSourceTS(node.type);
|
|
415
420
|
return {
|
|
416
421
|
type: 'new',
|
|
417
|
-
parameters:
|
|
422
|
+
parameters: parameters,
|
|
418
423
|
ret: _ret
|
|
419
424
|
};
|
|
420
425
|
}
|
|
421
426
|
case FunctionType:
|
|
422
|
-
|
|
423
|
-
|
|
427
|
+
{
|
|
428
|
+
if (!ts.isFunctionTypeNode(node)) {
|
|
429
|
+
throw Error("Impossible");
|
|
430
|
+
}
|
|
431
|
+
var _parameters = node.parameters.map(toSourceTS);
|
|
432
|
+
return {
|
|
433
|
+
type: 'function',
|
|
434
|
+
parameters: _parameters
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
case JSDocFunctionType:
|
|
438
|
+
{
|
|
439
|
+
if (!ts.isJSDocFunctionType(node)) {
|
|
440
|
+
throw Error("Impossible");
|
|
441
|
+
}
|
|
442
|
+
var _parameters2 = node.parameters.map(toSourceTS);
|
|
443
|
+
return {
|
|
444
|
+
type: 'function',
|
|
445
|
+
parameters: _parameters2
|
|
446
|
+
};
|
|
424
447
|
}
|
|
425
|
-
var parameters = node.parameters.map(toSourceTS);
|
|
426
|
-
return {
|
|
427
|
-
type: 'function',
|
|
428
|
-
parameters: parameters
|
|
429
|
-
};
|
|
430
448
|
case IndexedAccessType:
|
|
431
449
|
if (!ts.isIndexedAccessTypeNode(node)) {
|
|
432
450
|
throw Error("Impossible");
|
|
@@ -721,6 +739,14 @@
|
|
|
721
739
|
}
|
|
722
740
|
// fall-through for parentheses
|
|
723
741
|
return toSourceTS(node.type);
|
|
742
|
+
// toSourceTS(parseType("import('abc').x.y.z"))
|
|
743
|
+
case QualifiedName:
|
|
744
|
+
if (!ts.isQualifiedName(node)) {
|
|
745
|
+
throw Error("Impossible");
|
|
746
|
+
}
|
|
747
|
+
var l = toSourceTS(node.left);
|
|
748
|
+
var r = toSourceTS(node.right);
|
|
749
|
+
return "".concat(l, ".").concat(r);
|
|
724
750
|
case ImportType:
|
|
725
751
|
if (!ts.isImportTypeNode(node)) {
|
|
726
752
|
throw Error("Impossible");
|
|
@@ -1235,6 +1261,16 @@
|
|
|
1235
1261
|
stat.unchecked = 0;
|
|
1236
1262
|
}
|
|
1237
1263
|
|
|
1264
|
+
/**
|
|
1265
|
+
* @example
|
|
1266
|
+
* capitalize("hello"); // Outputs: Hello
|
|
1267
|
+
* @param {string} _ - The input string.
|
|
1268
|
+
* @returns {string} The capitalized output string.
|
|
1269
|
+
*/
|
|
1270
|
+
function capitalize(_) {
|
|
1271
|
+
return _[0].toUpperCase() + _.slice(1);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1238
1274
|
/**
|
|
1239
1275
|
* @example
|
|
1240
1276
|
* trimEndSpaces('test \n '); // Returns: 'test \n'
|
|
@@ -1269,6 +1305,7 @@
|
|
|
1269
1305
|
_defineProperty(this, "addReactImport", false);
|
|
1270
1306
|
_defineProperty(this, "lastCommentBlockIndex", -1);
|
|
1271
1307
|
_defineProperty(this, "lastCommentLineIndex", -1);
|
|
1308
|
+
_defineProperty(this, "encounteredFragment", false);
|
|
1272
1309
|
_defineProperty(this, "numSpaces", 0);
|
|
1273
1310
|
}
|
|
1274
1311
|
_createClass(Stringifier, [{
|
|
@@ -1518,20 +1555,24 @@
|
|
|
1518
1555
|
out += this.toSource(node);
|
|
1519
1556
|
if (needCurly) {
|
|
1520
1557
|
out += '\n';
|
|
1521
|
-
out += spaces;
|
|
1558
|
+
out += spaces.slice(0, -2);
|
|
1522
1559
|
out += '}';
|
|
1523
1560
|
}
|
|
1524
1561
|
return out;
|
|
1525
1562
|
}
|
|
1563
|
+
}, {
|
|
1564
|
+
key: "getHeader",
|
|
1565
|
+
value:
|
|
1526
1566
|
/**
|
|
1527
1567
|
* @returns {string} The header.
|
|
1528
1568
|
*/
|
|
1529
|
-
|
|
1530
|
-
key: "getHeader",
|
|
1531
|
-
value: function getHeader() {
|
|
1569
|
+
function getHeader() {
|
|
1532
1570
|
if (!this.addReactImport) {
|
|
1533
1571
|
return '';
|
|
1534
1572
|
}
|
|
1573
|
+
if (this.encounteredFragment) {
|
|
1574
|
+
return "import {createElement, Fragment} from 'react';\n";
|
|
1575
|
+
}
|
|
1535
1576
|
return "import {createElement} from 'react';\n";
|
|
1536
1577
|
}
|
|
1537
1578
|
}, {
|
|
@@ -1945,7 +1986,10 @@
|
|
|
1945
1986
|
directives = node.directives;
|
|
1946
1987
|
var spaces = this.spaces;
|
|
1947
1988
|
var out = '';
|
|
1948
|
-
|
|
1989
|
+
if (this.parentType !== 'Program') {
|
|
1990
|
+
out += ' ';
|
|
1991
|
+
}
|
|
1992
|
+
out += '{';
|
|
1949
1993
|
this.numSpaces++;
|
|
1950
1994
|
// Handle Directive/DirectiveLiteral like 'use strict';
|
|
1951
1995
|
if (directives && directives.length) {
|
|
@@ -1954,8 +1998,7 @@
|
|
|
1954
1998
|
}
|
|
1955
1999
|
out += this.generateTypeChecks(node);
|
|
1956
2000
|
if (body.length) {
|
|
1957
|
-
if (out.length ===
|
|
1958
|
-
// 2 is ' {'
|
|
2001
|
+
if (out[out.length - 1] === '{') {
|
|
1959
2002
|
out += '\n';
|
|
1960
2003
|
}
|
|
1961
2004
|
// out += '/*'+out.length+'*/';
|
|
@@ -2025,16 +2068,26 @@
|
|
|
2025
2068
|
var alternate = node.alternate,
|
|
2026
2069
|
consequent = node.consequent,
|
|
2027
2070
|
test = node.test;
|
|
2028
|
-
var spaces = this.spaces;
|
|
2029
2071
|
var out = '';
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2072
|
+
if (this.forceCurly) {
|
|
2073
|
+
out += this.spaces;
|
|
2074
|
+
} else {
|
|
2075
|
+
if (this.parentType === 'BlockStatement') {
|
|
2076
|
+
out += this.spaces;
|
|
2077
|
+
} else if (this.parentType === 'IfStatement') {
|
|
2078
|
+
out += ' ';
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2034
2081
|
out += "if (".concat(this.toSource(test), ")");
|
|
2035
2082
|
out += this.toSourceCurly(consequent);
|
|
2036
2083
|
if (alternate) {
|
|
2084
|
+
if (this.forceCurly) {
|
|
2085
|
+
this.numSpaces++;
|
|
2086
|
+
}
|
|
2037
2087
|
out += " else".concat(this.toSourceCurly(alternate));
|
|
2088
|
+
if (this.forceCurly) {
|
|
2089
|
+
this.numSpaces--;
|
|
2090
|
+
}
|
|
2038
2091
|
}
|
|
2039
2092
|
return out;
|
|
2040
2093
|
}
|
|
@@ -2650,6 +2703,53 @@
|
|
|
2650
2703
|
out += ')';
|
|
2651
2704
|
return out;
|
|
2652
2705
|
}
|
|
2706
|
+
/**
|
|
2707
|
+
* @param {import("@babel/types").JSXFragment} node - The Babel AST node.
|
|
2708
|
+
* @returns {string} Stringification of the node.
|
|
2709
|
+
*/
|
|
2710
|
+
}, {
|
|
2711
|
+
key: "JSXFragment",
|
|
2712
|
+
value: function JSXFragment(node) {
|
|
2713
|
+
var children = node.children;
|
|
2714
|
+
this.encounteredFragment = true;
|
|
2715
|
+
// console.log('JSXFragment', {openingFragment, closingFragment, children});
|
|
2716
|
+
var spaces = this.spaces;
|
|
2717
|
+
var out = 'createElement(\n';
|
|
2718
|
+
this.numSpaces++;
|
|
2719
|
+
spaces = this.spaces;
|
|
2720
|
+
out += spaces;
|
|
2721
|
+
out += 'Fragment,\n';
|
|
2722
|
+
out += spaces;
|
|
2723
|
+
out += 'null';
|
|
2724
|
+
// Same code for children as in JSXElement
|
|
2725
|
+
if (children.length) {
|
|
2726
|
+
out += ',\n';
|
|
2727
|
+
var _iterator3 = _createForOfIteratorHelper(children),
|
|
2728
|
+
_step3;
|
|
2729
|
+
try {
|
|
2730
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
2731
|
+
var child = _step3.value;
|
|
2732
|
+
var source = this.toSource(child);
|
|
2733
|
+
if (!source.length) {
|
|
2734
|
+
continue;
|
|
2735
|
+
}
|
|
2736
|
+
out += spaces;
|
|
2737
|
+
out += source;
|
|
2738
|
+
out += ',\n';
|
|
2739
|
+
}
|
|
2740
|
+
} catch (err) {
|
|
2741
|
+
_iterator3.e(err);
|
|
2742
|
+
} finally {
|
|
2743
|
+
_iterator3.f();
|
|
2744
|
+
}
|
|
2745
|
+
} else {
|
|
2746
|
+
out += '\n';
|
|
2747
|
+
}
|
|
2748
|
+
this.numSpaces--;
|
|
2749
|
+
out += this.spaces;
|
|
2750
|
+
out += ')';
|
|
2751
|
+
return out;
|
|
2752
|
+
}
|
|
2653
2753
|
/**
|
|
2654
2754
|
* @param {import("@babel/types").JSXIdentifier} node - The Babel AST node.
|
|
2655
2755
|
* @returns {string} Stringification of the node.
|
|
@@ -2659,8 +2759,26 @@
|
|
|
2659
2759
|
value: function JSXIdentifier(node) {
|
|
2660
2760
|
var name = node.name;
|
|
2661
2761
|
// console.log('JSXIdentifier', {name});
|
|
2762
|
+
// https://github.com/kungfooman/RuntimeTypeInspector.js/issues/201
|
|
2763
|
+
// https://stackoverflow.com/questions/38998987/using-aria-attributes-on-elements-in-react
|
|
2764
|
+
if (name.includes('-')) {
|
|
2765
|
+
return JSON.stringify(name);
|
|
2766
|
+
}
|
|
2662
2767
|
return name;
|
|
2663
2768
|
}
|
|
2769
|
+
/**
|
|
2770
|
+
* @param {import("@babel/types").JSXNamespacedName} node - The Babel AST node.
|
|
2771
|
+
* @returns {string} Stringification of the node.
|
|
2772
|
+
*/
|
|
2773
|
+
}, {
|
|
2774
|
+
key: "JSXNamespacedName",
|
|
2775
|
+
value: function JSXNamespacedName(node) {
|
|
2776
|
+
var namespace = node.namespace,
|
|
2777
|
+
name = node.name;
|
|
2778
|
+
var a = this.toSource(namespace);
|
|
2779
|
+
var b = capitalize(this.toSource(name));
|
|
2780
|
+
return a + b;
|
|
2781
|
+
}
|
|
2664
2782
|
/**
|
|
2665
2783
|
* @param {import("@babel/types").JSXExpressionContainer} node - The Babel AST node.
|
|
2666
2784
|
* @returns {string} Stringification of the node.
|
|
@@ -3093,6 +3211,22 @@
|
|
|
3093
3211
|
value: function Import(node) {
|
|
3094
3212
|
return 'import';
|
|
3095
3213
|
}
|
|
3214
|
+
/**
|
|
3215
|
+
* @param {import("@babel/types").ImportExpression} node - The Babel AST node.
|
|
3216
|
+
* @returns {string} Stringification of the node.
|
|
3217
|
+
*/
|
|
3218
|
+
}, {
|
|
3219
|
+
key: "ImportExpression",
|
|
3220
|
+
value: function ImportExpression(node) {
|
|
3221
|
+
var source = node.source,
|
|
3222
|
+
options = node.options;
|
|
3223
|
+
var out = "import(" + this.toSource(source);
|
|
3224
|
+
if (options) {
|
|
3225
|
+
out += ', ' + this.toSource(options);
|
|
3226
|
+
}
|
|
3227
|
+
out += ')';
|
|
3228
|
+
return out;
|
|
3229
|
+
}
|
|
3096
3230
|
/**
|
|
3097
3231
|
* @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
|
|
3098
3232
|
* @returns {string} Stringification of the node.
|
|
@@ -3105,15 +3239,39 @@
|
|
|
3105
3239
|
// ImportNamespaceSpecifier: import * as a from "b";
|
|
3106
3240
|
// ImportDefaultSpecifier : import a from "b";
|
|
3107
3241
|
// ImportSpecifier : import { a } from "b";
|
|
3108
|
-
var
|
|
3109
|
-
var b = this.toSource(source);
|
|
3242
|
+
var from = this.toSource(source);
|
|
3110
3243
|
if (specifiers.length === 0) {
|
|
3111
|
-
return "import ".concat(
|
|
3244
|
+
return "import ".concat(from);
|
|
3245
|
+
}
|
|
3246
|
+
var out = 'import ';
|
|
3247
|
+
var n = specifiers.length;
|
|
3248
|
+
var inImportSpecifier = false;
|
|
3249
|
+
var i = 0;
|
|
3250
|
+
var _iterator4 = _createForOfIteratorHelper(specifiers),
|
|
3251
|
+
_step4;
|
|
3252
|
+
try {
|
|
3253
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
3254
|
+
var specifier = _step4.value;
|
|
3255
|
+
if (specifier.type === 'ImportSpecifier' && !inImportSpecifier) {
|
|
3256
|
+
out += "{";
|
|
3257
|
+
inImportSpecifier = true;
|
|
3258
|
+
}
|
|
3259
|
+
out += this.toSource(specifier);
|
|
3260
|
+
if (i !== n - 1) {
|
|
3261
|
+
out += ', ';
|
|
3262
|
+
}
|
|
3263
|
+
i++;
|
|
3264
|
+
}
|
|
3265
|
+
} catch (err) {
|
|
3266
|
+
_iterator4.e(err);
|
|
3267
|
+
} finally {
|
|
3268
|
+
_iterator4.f();
|
|
3112
3269
|
}
|
|
3113
|
-
if (
|
|
3114
|
-
|
|
3270
|
+
if (inImportSpecifier) {
|
|
3271
|
+
out += '}';
|
|
3115
3272
|
}
|
|
3116
|
-
|
|
3273
|
+
out += " from ".concat(from, ";");
|
|
3274
|
+
return out;
|
|
3117
3275
|
}
|
|
3118
3276
|
/**
|
|
3119
3277
|
* @param {import("@babel/types").ImportSpecifier} node - The Babel AST node.
|
|
@@ -4527,6 +4685,7 @@
|
|
|
4527
4685
|
exports.addTypeChecks = addTypeChecks;
|
|
4528
4686
|
exports.ast2json = ast2json;
|
|
4529
4687
|
exports.ast2jsonForComparison = ast2jsonForComparison;
|
|
4688
|
+
exports.capitalize = capitalize;
|
|
4530
4689
|
exports.code2ast2code = code2ast2code;
|
|
4531
4690
|
exports.compareAST = compareAST;
|
|
4532
4691
|
exports.expandType = expandType;
|
package/index.mjs
CHANGED
|
@@ -63,11 +63,14 @@ const requiredTypeofs = {};
|
|
|
63
63
|
* This function handles various TypeScript AST node types and converts them into a string
|
|
64
64
|
* or an object representing the type.
|
|
65
65
|
*
|
|
66
|
-
* @param {ts.TypeNode|ts.Identifier} node - The TypeScript AST node to convert.
|
|
66
|
+
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName|undefined} node - The TypeScript AST node to convert.
|
|
67
67
|
* @returns {string | number | boolean | {type: string, [key: string]: any} | undefined} The source string/number,
|
|
68
68
|
* or an object with type information based on the node, or `undefined` if the node kind is not handled.
|
|
69
69
|
*/
|
|
70
70
|
function toSourceTS(node) {
|
|
71
|
+
if (!node) {
|
|
72
|
+
return 'undefined';
|
|
73
|
+
}
|
|
71
74
|
const {
|
|
72
75
|
typeArguments,
|
|
73
76
|
typeName
|
|
@@ -82,6 +85,8 @@ function toSourceTS(node) {
|
|
|
82
85
|
// parseType("boolean" ).kind === ts.SyntaxKind.BooleanKeyword
|
|
83
86
|
FunctionType,
|
|
84
87
|
// parseType("() => void" ).kind === ts.SyntaxKind.FunctionType
|
|
88
|
+
JSDocFunctionType,
|
|
89
|
+
// parseType("function (number, number): number").kind === ts.SyntaxKind.JSDocFunctionType
|
|
85
90
|
Identifier,
|
|
86
91
|
// parseType("{a: 1, b: 2}" ).members[0].name.kind === ts.SyntaxKind.Identifier
|
|
87
92
|
IntersectionType,
|
|
@@ -158,7 +163,9 @@ function toSourceTS(node) {
|
|
|
158
163
|
// parseType('[a: 1]' ).elements[0].kind === ts.SyntaxKind.NamedTupleMember
|
|
159
164
|
MappedType,
|
|
160
165
|
// parseType('{[K in TaskType]: 123}' ).kind === ts.SyntaxKind.MappedType
|
|
161
|
-
TypeParameter
|
|
166
|
+
TypeParameter,
|
|
167
|
+
// parseType('{[K in TaskType]: 123}' ).typeParameter.kind === ts.SyntaxKind.TypeParameter
|
|
168
|
+
QualifiedName // parseType("import('abc').x.y" ).qualifier.kind === ts.SyntaxKind.QualifiedName
|
|
162
169
|
} = ts.SyntaxKind;
|
|
163
170
|
// console.log({typeArguments, typeName, kind_, node});
|
|
164
171
|
switch (node.kind) {
|
|
@@ -198,23 +205,36 @@ function toSourceTS(node) {
|
|
|
198
205
|
if (!ts.isConstructorTypeNode(node)) {
|
|
199
206
|
throw Error("Impossible");
|
|
200
207
|
}
|
|
201
|
-
const
|
|
208
|
+
const parameters = node.parameters.map(toSourceTS);
|
|
202
209
|
const _ret = toSourceTS(node.type);
|
|
203
210
|
return {
|
|
204
211
|
type: 'new',
|
|
205
|
-
parameters
|
|
212
|
+
parameters,
|
|
206
213
|
ret: _ret
|
|
207
214
|
};
|
|
208
215
|
}
|
|
209
216
|
case FunctionType:
|
|
210
|
-
|
|
211
|
-
|
|
217
|
+
{
|
|
218
|
+
if (!ts.isFunctionTypeNode(node)) {
|
|
219
|
+
throw Error("Impossible");
|
|
220
|
+
}
|
|
221
|
+
const parameters = node.parameters.map(toSourceTS);
|
|
222
|
+
return {
|
|
223
|
+
type: 'function',
|
|
224
|
+
parameters
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
case JSDocFunctionType:
|
|
228
|
+
{
|
|
229
|
+
if (!ts.isJSDocFunctionType(node)) {
|
|
230
|
+
throw Error("Impossible");
|
|
231
|
+
}
|
|
232
|
+
const parameters = node.parameters.map(toSourceTS);
|
|
233
|
+
return {
|
|
234
|
+
type: 'function',
|
|
235
|
+
parameters
|
|
236
|
+
};
|
|
212
237
|
}
|
|
213
|
-
const parameters = node.parameters.map(toSourceTS);
|
|
214
|
-
return {
|
|
215
|
-
type: 'function',
|
|
216
|
-
parameters
|
|
217
|
-
};
|
|
218
238
|
case IndexedAccessType:
|
|
219
239
|
if (!ts.isIndexedAccessTypeNode(node)) {
|
|
220
240
|
throw Error("Impossible");
|
|
@@ -509,6 +529,14 @@ function toSourceTS(node) {
|
|
|
509
529
|
}
|
|
510
530
|
// fall-through for parentheses
|
|
511
531
|
return toSourceTS(node.type);
|
|
532
|
+
// toSourceTS(parseType("import('abc').x.y.z"))
|
|
533
|
+
case QualifiedName:
|
|
534
|
+
if (!ts.isQualifiedName(node)) {
|
|
535
|
+
throw Error("Impossible");
|
|
536
|
+
}
|
|
537
|
+
const l = toSourceTS(node.left);
|
|
538
|
+
const r = toSourceTS(node.right);
|
|
539
|
+
return `${l}.${r}`;
|
|
512
540
|
case ImportType:
|
|
513
541
|
if (!ts.isImportTypeNode(node)) {
|
|
514
542
|
throw Error("Impossible");
|
|
@@ -994,6 +1022,16 @@ function statReset(stat) {
|
|
|
994
1022
|
stat.unchecked = 0;
|
|
995
1023
|
}
|
|
996
1024
|
|
|
1025
|
+
/**
|
|
1026
|
+
* @example
|
|
1027
|
+
* capitalize("hello"); // Outputs: Hello
|
|
1028
|
+
* @param {string} _ - The input string.
|
|
1029
|
+
* @returns {string} The capitalized output string.
|
|
1030
|
+
*/
|
|
1031
|
+
function capitalize(_) {
|
|
1032
|
+
return _[0].toUpperCase() + _.slice(1);
|
|
1033
|
+
}
|
|
1034
|
+
|
|
997
1035
|
/**
|
|
998
1036
|
* @example
|
|
999
1037
|
* trimEndSpaces('test \n '); // Returns: 'test \n'
|
|
@@ -1027,6 +1065,7 @@ class Stringifier {
|
|
|
1027
1065
|
this.addReactImport = false;
|
|
1028
1066
|
this.lastCommentBlockIndex = -1;
|
|
1029
1067
|
this.lastCommentLineIndex = -1;
|
|
1068
|
+
this.encounteredFragment = false;
|
|
1030
1069
|
this.numSpaces = 0;
|
|
1031
1070
|
}
|
|
1032
1071
|
/**
|
|
@@ -1257,7 +1296,7 @@ class Stringifier {
|
|
|
1257
1296
|
out += this.toSource(node);
|
|
1258
1297
|
if (needCurly) {
|
|
1259
1298
|
out += '\n';
|
|
1260
|
-
out += spaces;
|
|
1299
|
+
out += spaces.slice(0, -2);
|
|
1261
1300
|
out += '}';
|
|
1262
1301
|
}
|
|
1263
1302
|
return out;
|
|
@@ -1269,6 +1308,9 @@ class Stringifier {
|
|
|
1269
1308
|
if (!this.addReactImport) {
|
|
1270
1309
|
return '';
|
|
1271
1310
|
}
|
|
1311
|
+
if (this.encounteredFragment) {
|
|
1312
|
+
return "import {createElement, Fragment} from 'react';\n";
|
|
1313
|
+
}
|
|
1272
1314
|
return "import {createElement} from 'react';\n";
|
|
1273
1315
|
}
|
|
1274
1316
|
get parent() {
|
|
@@ -1660,7 +1702,10 @@ class Stringifier {
|
|
|
1660
1702
|
} = node;
|
|
1661
1703
|
const spaces = this.spaces;
|
|
1662
1704
|
let out = '';
|
|
1663
|
-
|
|
1705
|
+
if (this.parentType !== 'Program') {
|
|
1706
|
+
out += ' ';
|
|
1707
|
+
}
|
|
1708
|
+
out += '{';
|
|
1664
1709
|
this.numSpaces++;
|
|
1665
1710
|
// Handle Directive/DirectiveLiteral like 'use strict';
|
|
1666
1711
|
if (directives && directives.length) {
|
|
@@ -1669,8 +1714,7 @@ class Stringifier {
|
|
|
1669
1714
|
}
|
|
1670
1715
|
out += this.generateTypeChecks(node);
|
|
1671
1716
|
if (body.length) {
|
|
1672
|
-
if (out.length ===
|
|
1673
|
-
// 2 is ' {'
|
|
1717
|
+
if (out[out.length - 1] === '{') {
|
|
1674
1718
|
out += '\n';
|
|
1675
1719
|
}
|
|
1676
1720
|
// out += '/*'+out.length+'*/';
|
|
@@ -1742,16 +1786,26 @@ class Stringifier {
|
|
|
1742
1786
|
consequent,
|
|
1743
1787
|
test
|
|
1744
1788
|
} = node;
|
|
1745
|
-
const spaces = this.spaces;
|
|
1746
1789
|
let out = '';
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1790
|
+
if (this.forceCurly) {
|
|
1791
|
+
out += this.spaces;
|
|
1792
|
+
} else {
|
|
1793
|
+
if (this.parentType === 'BlockStatement') {
|
|
1794
|
+
out += this.spaces;
|
|
1795
|
+
} else if (this.parentType === 'IfStatement') {
|
|
1796
|
+
out += ' ';
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1751
1799
|
out += `if (${this.toSource(test)})`;
|
|
1752
1800
|
out += this.toSourceCurly(consequent);
|
|
1753
1801
|
if (alternate) {
|
|
1802
|
+
if (this.forceCurly) {
|
|
1803
|
+
this.numSpaces++;
|
|
1804
|
+
}
|
|
1754
1805
|
out += ` else${this.toSourceCurly(alternate)}`;
|
|
1806
|
+
if (this.forceCurly) {
|
|
1807
|
+
this.numSpaces--;
|
|
1808
|
+
}
|
|
1755
1809
|
}
|
|
1756
1810
|
return out;
|
|
1757
1811
|
}
|
|
@@ -2354,6 +2408,46 @@ class Stringifier {
|
|
|
2354
2408
|
out += ')';
|
|
2355
2409
|
return out;
|
|
2356
2410
|
}
|
|
2411
|
+
/**
|
|
2412
|
+
* @param {import("@babel/types").JSXFragment} node - The Babel AST node.
|
|
2413
|
+
* @returns {string} Stringification of the node.
|
|
2414
|
+
*/
|
|
2415
|
+
JSXFragment(node) {
|
|
2416
|
+
const {
|
|
2417
|
+
/*openingFragment, closingFragment,*/children
|
|
2418
|
+
} = node;
|
|
2419
|
+
this.encounteredFragment = true;
|
|
2420
|
+
// console.log('JSXFragment', {openingFragment, closingFragment, children});
|
|
2421
|
+
let {
|
|
2422
|
+
spaces
|
|
2423
|
+
} = this;
|
|
2424
|
+
let out = 'createElement(\n';
|
|
2425
|
+
this.numSpaces++;
|
|
2426
|
+
spaces = this.spaces;
|
|
2427
|
+
out += spaces;
|
|
2428
|
+
out += 'Fragment,\n';
|
|
2429
|
+
out += spaces;
|
|
2430
|
+
out += 'null';
|
|
2431
|
+
// Same code for children as in JSXElement
|
|
2432
|
+
if (children.length) {
|
|
2433
|
+
out += ',\n';
|
|
2434
|
+
for (const child of children) {
|
|
2435
|
+
const source = this.toSource(child);
|
|
2436
|
+
if (!source.length) {
|
|
2437
|
+
continue;
|
|
2438
|
+
}
|
|
2439
|
+
out += spaces;
|
|
2440
|
+
out += source;
|
|
2441
|
+
out += ',\n';
|
|
2442
|
+
}
|
|
2443
|
+
} else {
|
|
2444
|
+
out += '\n';
|
|
2445
|
+
}
|
|
2446
|
+
this.numSpaces--;
|
|
2447
|
+
out += this.spaces;
|
|
2448
|
+
out += ')';
|
|
2449
|
+
return out;
|
|
2450
|
+
}
|
|
2357
2451
|
/**
|
|
2358
2452
|
* @param {import("@babel/types").JSXIdentifier} node - The Babel AST node.
|
|
2359
2453
|
* @returns {string} Stringification of the node.
|
|
@@ -2363,8 +2457,26 @@ class Stringifier {
|
|
|
2363
2457
|
name
|
|
2364
2458
|
} = node;
|
|
2365
2459
|
// console.log('JSXIdentifier', {name});
|
|
2460
|
+
// https://github.com/kungfooman/RuntimeTypeInspector.js/issues/201
|
|
2461
|
+
// https://stackoverflow.com/questions/38998987/using-aria-attributes-on-elements-in-react
|
|
2462
|
+
if (name.includes('-')) {
|
|
2463
|
+
return JSON.stringify(name);
|
|
2464
|
+
}
|
|
2366
2465
|
return name;
|
|
2367
2466
|
}
|
|
2467
|
+
/**
|
|
2468
|
+
* @param {import("@babel/types").JSXNamespacedName} node - The Babel AST node.
|
|
2469
|
+
* @returns {string} Stringification of the node.
|
|
2470
|
+
*/
|
|
2471
|
+
JSXNamespacedName(node) {
|
|
2472
|
+
const {
|
|
2473
|
+
namespace,
|
|
2474
|
+
name
|
|
2475
|
+
} = node;
|
|
2476
|
+
const a = this.toSource(namespace);
|
|
2477
|
+
const b = capitalize(this.toSource(name));
|
|
2478
|
+
return a + b;
|
|
2479
|
+
}
|
|
2368
2480
|
/**
|
|
2369
2481
|
* @param {import("@babel/types").JSXExpressionContainer} node - The Babel AST node.
|
|
2370
2482
|
* @returns {string} Stringification of the node.
|
|
@@ -2803,6 +2915,22 @@ class Stringifier {
|
|
|
2803
2915
|
Import(node) {
|
|
2804
2916
|
return 'import';
|
|
2805
2917
|
}
|
|
2918
|
+
/**
|
|
2919
|
+
* @param {import("@babel/types").ImportExpression} node - The Babel AST node.
|
|
2920
|
+
* @returns {string} Stringification of the node.
|
|
2921
|
+
*/
|
|
2922
|
+
ImportExpression(node) {
|
|
2923
|
+
const {
|
|
2924
|
+
source,
|
|
2925
|
+
options
|
|
2926
|
+
} = node;
|
|
2927
|
+
let out = "import(" + this.toSource(source);
|
|
2928
|
+
if (options) {
|
|
2929
|
+
out += ', ' + this.toSource(options);
|
|
2930
|
+
}
|
|
2931
|
+
out += ')';
|
|
2932
|
+
return out;
|
|
2933
|
+
}
|
|
2806
2934
|
/**
|
|
2807
2935
|
* @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
|
|
2808
2936
|
* @returns {string} Stringification of the node.
|
|
@@ -2815,15 +2943,30 @@ class Stringifier {
|
|
|
2815
2943
|
// ImportNamespaceSpecifier: import * as a from "b";
|
|
2816
2944
|
// ImportDefaultSpecifier : import a from "b";
|
|
2817
2945
|
// ImportSpecifier : import { a } from "b";
|
|
2818
|
-
const
|
|
2819
|
-
const b = this.toSource(source);
|
|
2946
|
+
const from = this.toSource(source);
|
|
2820
2947
|
if (specifiers.length === 0) {
|
|
2821
|
-
return `import ${
|
|
2948
|
+
return `import ${from}`;
|
|
2949
|
+
}
|
|
2950
|
+
let out = 'import ';
|
|
2951
|
+
const n = specifiers.length;
|
|
2952
|
+
let inImportSpecifier = false;
|
|
2953
|
+
let i = 0;
|
|
2954
|
+
for (const specifier of specifiers) {
|
|
2955
|
+
if (specifier.type === 'ImportSpecifier' && !inImportSpecifier) {
|
|
2956
|
+
out += "{";
|
|
2957
|
+
inImportSpecifier = true;
|
|
2958
|
+
}
|
|
2959
|
+
out += this.toSource(specifier);
|
|
2960
|
+
if (i !== n - 1) {
|
|
2961
|
+
out += ', ';
|
|
2962
|
+
}
|
|
2963
|
+
i++;
|
|
2822
2964
|
}
|
|
2823
|
-
if (
|
|
2824
|
-
|
|
2965
|
+
if (inImportSpecifier) {
|
|
2966
|
+
out += '}';
|
|
2825
2967
|
}
|
|
2826
|
-
|
|
2968
|
+
out += ` from ${from};`;
|
|
2969
|
+
return out;
|
|
2827
2970
|
}
|
|
2828
2971
|
/**
|
|
2829
2972
|
* @param {import("@babel/types").ImportSpecifier} node - The Babel AST node.
|
|
@@ -4144,4 +4287,4 @@ function toSourceBabelTS(node) {
|
|
|
4144
4287
|
}
|
|
4145
4288
|
}
|
|
4146
4289
|
|
|
4147
|
-
export { Asserter, Stringifier, addTypeChecks, ast2json, ast2jsonForComparison, code2ast2code, compareAST, expandType, expandTypeBabelTS, expandTypeDepFree, extractCurlyContent, extractNameAndOptionality, nodeIsFunction, parseJSDoc, parseJSDocSetter, parseJSDocTemplates, parseJSDocTypedef, parseType, parseTypeBabelTS, parserOptions, requiredTypeofs, simplifyType, toSourceBabelTS, toSourceTS, trimEndSpaces };
|
|
4290
|
+
export { Asserter, Stringifier, addTypeChecks, ast2json, ast2jsonForComparison, capitalize, code2ast2code, compareAST, expandType, expandTypeBabelTS, expandTypeDepFree, extractCurlyContent, extractNameAndOptionality, nodeIsFunction, parseJSDoc, parseJSDocSetter, parseJSDocTemplates, parseJSDocTypedef, parseType, parseTypeBabelTS, parserOptions, requiredTypeofs, simplifyType, toSourceBabelTS, toSourceTS, trimEndSpaces };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"bin": "bin.js",
|
|
3
3
|
"name": "@runtime-type-inspector/transpiler",
|
|
4
|
-
"version": "4.0.
|
|
4
|
+
"version": "4.0.2",
|
|
5
5
|
"description": "Validating JSDoc types at runtime for high-quality types - Trust is good, control is better.",
|
|
6
6
|
"main": "index.cjs",
|
|
7
7
|
"module": "index.mjs",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"homepage": "https://github.com/kungfooman/RuntimeTypeInspector.js#readme",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.23.0",
|
|
22
|
-
"@runtime-type-inspector/runtime": "^4.0.
|
|
22
|
+
"@runtime-type-inspector/runtime": "^4.0.2",
|
|
23
23
|
"typescript": "^5.1.6"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|