@runtime-type-inspector/transpiler 4.0.0 → 4.0.1
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 +167 -20
- package/index.mjs +149 -18
- 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, [{
|
|
@@ -1523,15 +1560,19 @@
|
|
|
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
|
}, {
|
|
@@ -2650,6 +2691,53 @@
|
|
|
2650
2691
|
out += ')';
|
|
2651
2692
|
return out;
|
|
2652
2693
|
}
|
|
2694
|
+
/**
|
|
2695
|
+
* @param {import("@babel/types").JSXFragment} node - The Babel AST node.
|
|
2696
|
+
* @returns {string} Stringification of the node.
|
|
2697
|
+
*/
|
|
2698
|
+
}, {
|
|
2699
|
+
key: "JSXFragment",
|
|
2700
|
+
value: function JSXFragment(node) {
|
|
2701
|
+
var children = node.children;
|
|
2702
|
+
this.encounteredFragment = true;
|
|
2703
|
+
// console.log('JSXFragment', {openingFragment, closingFragment, children});
|
|
2704
|
+
var spaces = this.spaces;
|
|
2705
|
+
var out = 'createElement(\n';
|
|
2706
|
+
this.numSpaces++;
|
|
2707
|
+
spaces = this.spaces;
|
|
2708
|
+
out += spaces;
|
|
2709
|
+
out += 'Fragment,\n';
|
|
2710
|
+
out += spaces;
|
|
2711
|
+
out += 'null';
|
|
2712
|
+
// Same code for children as in JSXElement
|
|
2713
|
+
if (children.length) {
|
|
2714
|
+
out += ',\n';
|
|
2715
|
+
var _iterator3 = _createForOfIteratorHelper(children),
|
|
2716
|
+
_step3;
|
|
2717
|
+
try {
|
|
2718
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
2719
|
+
var child = _step3.value;
|
|
2720
|
+
var source = this.toSource(child);
|
|
2721
|
+
if (!source.length) {
|
|
2722
|
+
continue;
|
|
2723
|
+
}
|
|
2724
|
+
out += spaces;
|
|
2725
|
+
out += source;
|
|
2726
|
+
out += ',\n';
|
|
2727
|
+
}
|
|
2728
|
+
} catch (err) {
|
|
2729
|
+
_iterator3.e(err);
|
|
2730
|
+
} finally {
|
|
2731
|
+
_iterator3.f();
|
|
2732
|
+
}
|
|
2733
|
+
} else {
|
|
2734
|
+
out += '\n';
|
|
2735
|
+
}
|
|
2736
|
+
this.numSpaces--;
|
|
2737
|
+
out += this.spaces;
|
|
2738
|
+
out += ')';
|
|
2739
|
+
return out;
|
|
2740
|
+
}
|
|
2653
2741
|
/**
|
|
2654
2742
|
* @param {import("@babel/types").JSXIdentifier} node - The Babel AST node.
|
|
2655
2743
|
* @returns {string} Stringification of the node.
|
|
@@ -2659,8 +2747,26 @@
|
|
|
2659
2747
|
value: function JSXIdentifier(node) {
|
|
2660
2748
|
var name = node.name;
|
|
2661
2749
|
// console.log('JSXIdentifier', {name});
|
|
2750
|
+
// https://github.com/kungfooman/RuntimeTypeInspector.js/issues/201
|
|
2751
|
+
// https://stackoverflow.com/questions/38998987/using-aria-attributes-on-elements-in-react
|
|
2752
|
+
if (name.includes('-')) {
|
|
2753
|
+
return JSON.stringify(name);
|
|
2754
|
+
}
|
|
2662
2755
|
return name;
|
|
2663
2756
|
}
|
|
2757
|
+
/**
|
|
2758
|
+
* @param {import("@babel/types").JSXNamespacedName} node - The Babel AST node.
|
|
2759
|
+
* @returns {string} Stringification of the node.
|
|
2760
|
+
*/
|
|
2761
|
+
}, {
|
|
2762
|
+
key: "JSXNamespacedName",
|
|
2763
|
+
value: function JSXNamespacedName(node) {
|
|
2764
|
+
var namespace = node.namespace,
|
|
2765
|
+
name = node.name;
|
|
2766
|
+
var a = this.toSource(namespace);
|
|
2767
|
+
var b = capitalize(this.toSource(name));
|
|
2768
|
+
return a + b;
|
|
2769
|
+
}
|
|
2664
2770
|
/**
|
|
2665
2771
|
* @param {import("@babel/types").JSXExpressionContainer} node - The Babel AST node.
|
|
2666
2772
|
* @returns {string} Stringification of the node.
|
|
@@ -3093,6 +3199,22 @@
|
|
|
3093
3199
|
value: function Import(node) {
|
|
3094
3200
|
return 'import';
|
|
3095
3201
|
}
|
|
3202
|
+
/**
|
|
3203
|
+
* @param {import("@babel/types").ImportExpression} node - The Babel AST node.
|
|
3204
|
+
* @returns {string} Stringification of the node.
|
|
3205
|
+
*/
|
|
3206
|
+
}, {
|
|
3207
|
+
key: "ImportExpression",
|
|
3208
|
+
value: function ImportExpression(node) {
|
|
3209
|
+
var source = node.source,
|
|
3210
|
+
options = node.options;
|
|
3211
|
+
var out = "import(" + this.toSource(source);
|
|
3212
|
+
if (options) {
|
|
3213
|
+
out += ', ' + this.toSource(options);
|
|
3214
|
+
}
|
|
3215
|
+
out += ')';
|
|
3216
|
+
return out;
|
|
3217
|
+
}
|
|
3096
3218
|
/**
|
|
3097
3219
|
* @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
|
|
3098
3220
|
* @returns {string} Stringification of the node.
|
|
@@ -3105,15 +3227,39 @@
|
|
|
3105
3227
|
// ImportNamespaceSpecifier: import * as a from "b";
|
|
3106
3228
|
// ImportDefaultSpecifier : import a from "b";
|
|
3107
3229
|
// ImportSpecifier : import { a } from "b";
|
|
3108
|
-
var
|
|
3109
|
-
var b = this.toSource(source);
|
|
3230
|
+
var from = this.toSource(source);
|
|
3110
3231
|
if (specifiers.length === 0) {
|
|
3111
|
-
return "import ".concat(
|
|
3232
|
+
return "import ".concat(from);
|
|
3112
3233
|
}
|
|
3113
|
-
|
|
3114
|
-
|
|
3234
|
+
var out = 'import ';
|
|
3235
|
+
var n = specifiers.length;
|
|
3236
|
+
var inImportSpecifier = false;
|
|
3237
|
+
var i = 0;
|
|
3238
|
+
var _iterator4 = _createForOfIteratorHelper(specifiers),
|
|
3239
|
+
_step4;
|
|
3240
|
+
try {
|
|
3241
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
3242
|
+
var specifier = _step4.value;
|
|
3243
|
+
if (specifier.type === 'ImportSpecifier' && !inImportSpecifier) {
|
|
3244
|
+
out += "{";
|
|
3245
|
+
inImportSpecifier = true;
|
|
3246
|
+
}
|
|
3247
|
+
out += this.toSource(specifier);
|
|
3248
|
+
if (i !== n - 1) {
|
|
3249
|
+
out += ', ';
|
|
3250
|
+
}
|
|
3251
|
+
i++;
|
|
3252
|
+
}
|
|
3253
|
+
} catch (err) {
|
|
3254
|
+
_iterator4.e(err);
|
|
3255
|
+
} finally {
|
|
3256
|
+
_iterator4.f();
|
|
3115
3257
|
}
|
|
3116
|
-
|
|
3258
|
+
if (inImportSpecifier) {
|
|
3259
|
+
out += '}';
|
|
3260
|
+
}
|
|
3261
|
+
out += " from ".concat(from, ";");
|
|
3262
|
+
return out;
|
|
3117
3263
|
}
|
|
3118
3264
|
/**
|
|
3119
3265
|
* @param {import("@babel/types").ImportSpecifier} node - The Babel AST node.
|
|
@@ -4527,6 +4673,7 @@
|
|
|
4527
4673
|
exports.addTypeChecks = addTypeChecks;
|
|
4528
4674
|
exports.ast2json = ast2json;
|
|
4529
4675
|
exports.ast2jsonForComparison = ast2jsonForComparison;
|
|
4676
|
+
exports.capitalize = capitalize;
|
|
4530
4677
|
exports.code2ast2code = code2ast2code;
|
|
4531
4678
|
exports.compareAST = compareAST;
|
|
4532
4679
|
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
|
/**
|
|
@@ -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() {
|
|
@@ -2354,6 +2396,46 @@ class Stringifier {
|
|
|
2354
2396
|
out += ')';
|
|
2355
2397
|
return out;
|
|
2356
2398
|
}
|
|
2399
|
+
/**
|
|
2400
|
+
* @param {import("@babel/types").JSXFragment} node - The Babel AST node.
|
|
2401
|
+
* @returns {string} Stringification of the node.
|
|
2402
|
+
*/
|
|
2403
|
+
JSXFragment(node) {
|
|
2404
|
+
const {
|
|
2405
|
+
/*openingFragment, closingFragment,*/children
|
|
2406
|
+
} = node;
|
|
2407
|
+
this.encounteredFragment = true;
|
|
2408
|
+
// console.log('JSXFragment', {openingFragment, closingFragment, children});
|
|
2409
|
+
let {
|
|
2410
|
+
spaces
|
|
2411
|
+
} = this;
|
|
2412
|
+
let out = 'createElement(\n';
|
|
2413
|
+
this.numSpaces++;
|
|
2414
|
+
spaces = this.spaces;
|
|
2415
|
+
out += spaces;
|
|
2416
|
+
out += 'Fragment,\n';
|
|
2417
|
+
out += spaces;
|
|
2418
|
+
out += 'null';
|
|
2419
|
+
// Same code for children as in JSXElement
|
|
2420
|
+
if (children.length) {
|
|
2421
|
+
out += ',\n';
|
|
2422
|
+
for (const child of children) {
|
|
2423
|
+
const source = this.toSource(child);
|
|
2424
|
+
if (!source.length) {
|
|
2425
|
+
continue;
|
|
2426
|
+
}
|
|
2427
|
+
out += spaces;
|
|
2428
|
+
out += source;
|
|
2429
|
+
out += ',\n';
|
|
2430
|
+
}
|
|
2431
|
+
} else {
|
|
2432
|
+
out += '\n';
|
|
2433
|
+
}
|
|
2434
|
+
this.numSpaces--;
|
|
2435
|
+
out += this.spaces;
|
|
2436
|
+
out += ')';
|
|
2437
|
+
return out;
|
|
2438
|
+
}
|
|
2357
2439
|
/**
|
|
2358
2440
|
* @param {import("@babel/types").JSXIdentifier} node - The Babel AST node.
|
|
2359
2441
|
* @returns {string} Stringification of the node.
|
|
@@ -2363,8 +2445,26 @@ class Stringifier {
|
|
|
2363
2445
|
name
|
|
2364
2446
|
} = node;
|
|
2365
2447
|
// console.log('JSXIdentifier', {name});
|
|
2448
|
+
// https://github.com/kungfooman/RuntimeTypeInspector.js/issues/201
|
|
2449
|
+
// https://stackoverflow.com/questions/38998987/using-aria-attributes-on-elements-in-react
|
|
2450
|
+
if (name.includes('-')) {
|
|
2451
|
+
return JSON.stringify(name);
|
|
2452
|
+
}
|
|
2366
2453
|
return name;
|
|
2367
2454
|
}
|
|
2455
|
+
/**
|
|
2456
|
+
* @param {import("@babel/types").JSXNamespacedName} node - The Babel AST node.
|
|
2457
|
+
* @returns {string} Stringification of the node.
|
|
2458
|
+
*/
|
|
2459
|
+
JSXNamespacedName(node) {
|
|
2460
|
+
const {
|
|
2461
|
+
namespace,
|
|
2462
|
+
name
|
|
2463
|
+
} = node;
|
|
2464
|
+
const a = this.toSource(namespace);
|
|
2465
|
+
const b = capitalize(this.toSource(name));
|
|
2466
|
+
return a + b;
|
|
2467
|
+
}
|
|
2368
2468
|
/**
|
|
2369
2469
|
* @param {import("@babel/types").JSXExpressionContainer} node - The Babel AST node.
|
|
2370
2470
|
* @returns {string} Stringification of the node.
|
|
@@ -2803,6 +2903,22 @@ class Stringifier {
|
|
|
2803
2903
|
Import(node) {
|
|
2804
2904
|
return 'import';
|
|
2805
2905
|
}
|
|
2906
|
+
/**
|
|
2907
|
+
* @param {import("@babel/types").ImportExpression} node - The Babel AST node.
|
|
2908
|
+
* @returns {string} Stringification of the node.
|
|
2909
|
+
*/
|
|
2910
|
+
ImportExpression(node) {
|
|
2911
|
+
const {
|
|
2912
|
+
source,
|
|
2913
|
+
options
|
|
2914
|
+
} = node;
|
|
2915
|
+
let out = "import(" + this.toSource(source);
|
|
2916
|
+
if (options) {
|
|
2917
|
+
out += ', ' + this.toSource(options);
|
|
2918
|
+
}
|
|
2919
|
+
out += ')';
|
|
2920
|
+
return out;
|
|
2921
|
+
}
|
|
2806
2922
|
/**
|
|
2807
2923
|
* @param {import("@babel/types").ImportDeclaration} node - The Babel AST node.
|
|
2808
2924
|
* @returns {string} Stringification of the node.
|
|
@@ -2815,15 +2931,30 @@ class Stringifier {
|
|
|
2815
2931
|
// ImportNamespaceSpecifier: import * as a from "b";
|
|
2816
2932
|
// ImportDefaultSpecifier : import a from "b";
|
|
2817
2933
|
// ImportSpecifier : import { a } from "b";
|
|
2818
|
-
const
|
|
2819
|
-
const b = this.toSource(source);
|
|
2934
|
+
const from = this.toSource(source);
|
|
2820
2935
|
if (specifiers.length === 0) {
|
|
2821
|
-
return `import ${
|
|
2936
|
+
return `import ${from}`;
|
|
2937
|
+
}
|
|
2938
|
+
let out = 'import ';
|
|
2939
|
+
const n = specifiers.length;
|
|
2940
|
+
let inImportSpecifier = false;
|
|
2941
|
+
let i = 0;
|
|
2942
|
+
for (const specifier of specifiers) {
|
|
2943
|
+
if (specifier.type === 'ImportSpecifier' && !inImportSpecifier) {
|
|
2944
|
+
out += "{";
|
|
2945
|
+
inImportSpecifier = true;
|
|
2946
|
+
}
|
|
2947
|
+
out += this.toSource(specifier);
|
|
2948
|
+
if (i !== n - 1) {
|
|
2949
|
+
out += ', ';
|
|
2950
|
+
}
|
|
2951
|
+
i++;
|
|
2822
2952
|
}
|
|
2823
|
-
if (
|
|
2824
|
-
|
|
2953
|
+
if (inImportSpecifier) {
|
|
2954
|
+
out += '}';
|
|
2825
2955
|
}
|
|
2826
|
-
|
|
2956
|
+
out += ` from ${from};`;
|
|
2957
|
+
return out;
|
|
2827
2958
|
}
|
|
2828
2959
|
/**
|
|
2829
2960
|
* @param {import("@babel/types").ImportSpecifier} node - The Babel AST node.
|
|
@@ -4144,4 +4275,4 @@ function toSourceBabelTS(node) {
|
|
|
4144
4275
|
}
|
|
4145
4276
|
}
|
|
4146
4277
|
|
|
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 };
|
|
4278
|
+
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.1",
|
|
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.1",
|
|
23
23
|
"typescript": "^5.1.6"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|