@runtime-type-inspector/transpiler 2.2.0 → 2.2.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.
Files changed (3) hide show
  1. package/index.cjs +134 -36
  2. package/index.mjs +129 -31
  3. package/package.json +1 -1
package/index.cjs CHANGED
@@ -345,6 +345,13 @@
345
345
  elements: elements.map(expandTypeDepFree)
346
346
  };
347
347
  }
348
+ if (type.startsWith('typeof ')) {
349
+ var argument = expandTypeDepFree(type.substring(7));
350
+ return {
351
+ type: 'typeof',
352
+ argument: argument
353
+ };
354
+ }
348
355
  if (type === 'object' || type === 'Object') {
349
356
  return {
350
357
  type: 'object',
@@ -541,6 +548,9 @@
541
548
  } else {
542
549
  warn("not an extensible type", lastTypedef);
543
550
  }
551
+ } else if (line.startsWith('@callback')) {
552
+ var _name2 = line.substring(9).trim();
553
+ typedefs[_name2] = 'Function';
544
554
  }
545
555
  }
546
556
  } catch (err) {
@@ -646,6 +656,14 @@
646
656
  if (dedicatedLine) {
647
657
  out += spaces;
648
658
  }
659
+ var multiLine = loc.start.line !== loc.end.line;
660
+ if (multiLine) {
661
+ var _this$parents$at;
662
+ if (((_this$parents$at = this.parents.at(-2)) === null || _this$parents$at === void 0 ? void 0 : _this$parents$at.type) === 'AssignmentExpression') {
663
+ spaces += ' ';
664
+ }
665
+ out += '\n' + spaces;
666
+ }
649
667
  out += '/*';
650
668
  if (value.includes('\n')) {
651
669
  value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(function (_) {
@@ -1021,12 +1039,21 @@
1021
1039
  id = node.id,
1022
1040
  params = node.params;
1023
1041
  var out = '';
1042
+ if (node.leadingComments) {
1043
+ if (this.parentType === 'AssignmentExpression') {
1044
+ this.numSpaces++;
1045
+ }
1046
+ out += '\n' + this.spaces;
1047
+ }
1024
1048
  if (async) {
1025
1049
  out += 'async ';
1026
1050
  }
1027
1051
  var star = generator ? '*' : '';
1028
1052
  out += 'function' + star + ' ' + this.toSource(id) + this.FunctionDeclarationParams(params);
1029
1053
  out += this.toSource(body);
1054
+ if (node.leadingComments && this.parentType === 'AssignmentExpression') {
1055
+ this.numSpaces--;
1056
+ }
1030
1057
  return out;
1031
1058
  }
1032
1059
  }, {
@@ -1426,26 +1453,51 @@
1426
1453
  value: function ExportAllDeclaration(node) {
1427
1454
  return "export * from ".concat(node.source.extra.raw, ";");
1428
1455
  }
1456
+ }, {
1457
+ key: "ExportNamespaceSpecifier",
1458
+ value: function ExportNamespaceSpecifier(node) {
1459
+ var exported = node.exported;
1460
+ return '* as ' + this.toSource(exported);
1461
+ }
1429
1462
  }, {
1430
1463
  key: "ExportNamedDeclaration",
1431
1464
  value: function ExportNamedDeclaration(node) {
1432
1465
  var _this3 = this;
1433
1466
  var declaration = node.declaration,
1434
1467
  source = node.source,
1435
- specifiers = node.specifiers;
1468
+ specifiers = node.specifiers,
1469
+ loc = node.loc;
1436
1470
  var out = this.spaces;
1437
1471
  if (specifiers.length) {
1438
- out += 'export {\n';
1439
- out += this.mapToSource(specifiers).map(function (_) {
1440
- return " ".concat(_this3.spaces).concat(_);
1441
- }).join(',\n');
1442
- out += '\n';
1443
- out += this.spaces;
1444
- out += '}';
1445
- if (source) {
1446
- out += " from " + this.toSource(source);
1472
+ if (!source) {
1473
+ console.warn("ExportNamedDeclaration> no source given");
1474
+ }
1475
+ if (specifiers.length === 1 && specifiers[0].type === 'ExportNamespaceSpecifier') {
1476
+ out += 'export ';
1477
+ out += this.toSource(specifiers[0]);
1478
+ out += ' from ';
1479
+ out += this.toSource(source);
1480
+ out += ";";
1481
+ } else {
1482
+ var sameLine = loc.start.line === loc.end.line;
1483
+ if (sameLine) {
1484
+ out += 'export {';
1485
+ out += this.mapToSource(specifiers).join(', ');
1486
+ out += '}';
1487
+ } else {
1488
+ out += 'export {\n';
1489
+ out += this.mapToSource(specifiers).map(function (_) {
1490
+ return " ".concat(_this3.spaces).concat(_);
1491
+ }).join(',\n');
1492
+ out += '\n';
1493
+ out += this.spaces;
1494
+ out += '}';
1495
+ }
1496
+ if (source) {
1497
+ out += " from " + this.toSource(source);
1498
+ }
1499
+ out += ";\n";
1447
1500
  }
1448
- out += ";\n";
1449
1501
  } else if (declaration) {
1450
1502
  out += 'export ' + this.toSource(declaration);
1451
1503
  } else {
@@ -2000,9 +2052,6 @@
2000
2052
  i--;
2001
2053
  while (i >= 0) {
2002
2054
  parent = parents[i];
2003
- if (parent.type === 'CallExpression') {
2004
- break;
2005
- }
2006
2055
  if (nodeIsFunction(parent)) {
2007
2056
  break;
2008
2057
  }
@@ -2021,37 +2070,49 @@
2021
2070
  }
2022
2071
  (_console = console).warn.apply(_console, arguments);
2023
2072
  }
2073
+ }, {
2074
+ key: "getLeadingCommentsNodeForFunctionExpression",
2075
+ value: function getLeadingCommentsNodeForFunctionExpression(node) {
2076
+ var parents = this.parents;
2077
+ var i = parents.findLastIndex(function (_) {
2078
+ return _ === node;
2079
+ });
2080
+ var parent = parents[i];
2081
+ if (parent.leadingComments) {
2082
+ return parent;
2083
+ }
2084
+ i--;
2085
+ while (i >= 0) {
2086
+ parent = parents[i];
2087
+ if (nodeIsFunction(parent)) {
2088
+ break;
2089
+ }
2090
+ if (parent.leadingComments) {
2091
+ return parent;
2092
+ }
2093
+ i--;
2094
+ }
2095
+ }
2024
2096
  }, {
2025
2097
  key: "getJSDoc",
2026
2098
  value: function getJSDoc(node) {
2027
2099
  if (node.type === 'BlockStatement') {
2028
2100
  node = this.parent;
2029
2101
  }
2030
- if (node.type === 'FunctionExpression') {
2031
- if (node.leadingComments) {
2032
- this.warn("Case of FunctionExpression containing its own leadingComments is not handled");
2033
- return;
2034
- }
2035
- node = this.findParentOfType(node, 'ExpressionStatement');
2036
- if (!node) {
2037
- node = this.parents.findLast(function (_) {
2038
- return _.type === 'VariableDeclaration';
2039
- });
2040
- if (!node) {
2041
- return;
2042
- }
2043
- }
2044
- }
2045
2102
  var _node = node,
2046
2103
  leadingComments = _node.leadingComments;
2104
+ if (node.type === 'FunctionExpression') {
2105
+ var tmp = this.getLeadingCommentsNodeForFunctionExpression(node);
2106
+ leadingComments = tmp === null || tmp === void 0 ? void 0 : tmp.leadingComments;
2107
+ }
2047
2108
  if (!leadingComments) {
2048
2109
  if (node.type === 'FunctionDeclaration') {
2049
2110
  var exportNamedDeclaration = this.findParentOfType(node, 'ExportNamedDeclaration');
2050
2111
  leadingComments = exportNamedDeclaration === null || exportNamedDeclaration === void 0 ? void 0 : exportNamedDeclaration.leadingComments;
2051
2112
  }
2052
2113
  if (node.type === 'ArrowFunctionExpression') {
2053
- var tmp = this.getLeadingCommentsNodeForArrowFunctionExpression(node);
2054
- leadingComments = tmp === null || tmp === void 0 ? void 0 : tmp.leadingComments;
2114
+ var _tmp = this.getLeadingCommentsNodeForArrowFunctionExpression(node);
2115
+ leadingComments = _tmp === null || _tmp === void 0 ? void 0 : _tmp.leadingComments;
2055
2116
  }
2056
2117
  }
2057
2118
  if (leadingComments && leadingComments.length) {
@@ -2279,6 +2340,23 @@
2279
2340
  }
2280
2341
  return out;
2281
2342
  }
2343
+ }, {
2344
+ key: "getNameForFunctionExpression",
2345
+ value: function getNameForFunctionExpression(node) {
2346
+ var objectProperty = this.findParentOfType(node, 'ObjectProperty');
2347
+ if (objectProperty) {
2348
+ return this.toSource(objectProperty.key);
2349
+ }
2350
+ var expressionStatement = this.findParentOfType(node, 'ExpressionStatement');
2351
+ if (expressionStatement) {
2352
+ var left = expressionStatement.expression.left;
2353
+ if (left) {
2354
+ return this.toSource(left);
2355
+ }
2356
+ console.warn("Asserter#getNameForFunctionExpression> expression without left");
2357
+ }
2358
+ return 'unnamed function expression';
2359
+ }
2282
2360
  }, {
2283
2361
  key: "getName",
2284
2362
  value: function getName(node) {
@@ -2308,11 +2386,7 @@
2308
2386
  if (id) {
2309
2387
  return toSource(id);
2310
2388
  }
2311
- var expressionStatement = this.findParentOfType(node, 'ExpressionStatement');
2312
- if (!expressionStatement) {
2313
- return 'unnamed function expression';
2314
- }
2315
- return toSource(expressionStatement.expression.left);
2389
+ return this.getNameForFunctionExpression(node);
2316
2390
  case 'ArrowFunctionExpression':
2317
2391
  var parent = this.findParentOfType(node, 'VariableDeclarator');
2318
2392
  if (parent) {
@@ -2491,6 +2565,9 @@
2491
2565
  ConditionalType = _ts$SyntaxKind.ConditionalType,
2492
2566
  IndexedAccessType = _ts$SyntaxKind.IndexedAccessType,
2493
2567
  RestType = _ts$SyntaxKind.RestType,
2568
+ TypeQuery = _ts$SyntaxKind.TypeQuery,
2569
+ TypeOperator = _ts$SyntaxKind.TypeOperator,
2570
+ KeyOfKeyword = _ts$SyntaxKind.KeyOfKeyword,
2494
2571
  ConstructorType = _ts$SyntaxKind.ConstructorType;
2495
2572
  switch (node.kind) {
2496
2573
  case BigIntKeyword:
@@ -2565,6 +2642,21 @@
2565
2642
  };
2566
2643
  }
2567
2644
  return ret;
2645
+ case TypeQuery:
2646
+ var argument = toSourceTS(node.exprName);
2647
+ return {
2648
+ type: 'typeof',
2649
+ argument: argument
2650
+ };
2651
+ case TypeOperator:
2652
+ if (node.operator === KeyOfKeyword) {
2653
+ var _argument = toSourceTS(node.type);
2654
+ return {
2655
+ type: 'keyof',
2656
+ argument: _argument
2657
+ };
2658
+ }
2659
+ console.warn("unimplemented TypeOperator", node);
2568
2660
  case TypeReference:
2569
2661
  {
2570
2662
  if ((typeName.text === 'Object' || typeName.text === 'Record') && (typeArguments === null || typeArguments === void 0 ? void 0 : typeArguments.length) === 2) {
@@ -2845,6 +2937,12 @@
2845
2937
  return toSourceBabelTS(node.type);
2846
2938
  case 'LastTypeNode':
2847
2939
  return toSourceBabelTS(node.qualifier);
2940
+ case 'TSTypeQuery':
2941
+ var argument = toSourceBabelTS(node.exprName);
2942
+ return {
2943
+ type: 'typeof',
2944
+ argument: argument
2945
+ };
2848
2946
  default:
2849
2947
  console.warn('toSourceBabelTS> unhandled type', node.type, node);
2850
2948
  debugger;
package/index.mjs CHANGED
@@ -81,6 +81,13 @@ function expandTypeDepFree(type) {
81
81
  elements: elements.map(expandTypeDepFree)
82
82
  };
83
83
  }
84
+ if (type.startsWith('typeof ')) {
85
+ const argument = expandTypeDepFree(type.substring(7));
86
+ return {
87
+ type: 'typeof',
88
+ argument
89
+ };
90
+ }
84
91
  if (type === 'object' || type === 'Object') {
85
92
  return {
86
93
  type: 'object',
@@ -270,6 +277,9 @@ function parseJSDocTypedef(typedefs, warn, comment, expandType) {
270
277
  } else {
271
278
  warn("not an extensible type", lastTypedef);
272
279
  }
280
+ } else if (line.startsWith('@callback')) {
281
+ const name = line.substring(9).trim();
282
+ typedefs[name] = 'Function';
273
283
  }
274
284
  }
275
285
  }
@@ -363,7 +373,7 @@ class Stringifier {
363
373
  return '';
364
374
  }
365
375
  this.lastCommentBlockIndex = loc.start.index;
366
- const {
376
+ let {
367
377
  spaces
368
378
  } = this;
369
379
  let out = '';
@@ -371,6 +381,14 @@ class Stringifier {
371
381
  if (dedicatedLine) {
372
382
  out += spaces;
373
383
  }
384
+ const multiLine = loc.start.line !== loc.end.line;
385
+ if (multiLine) {
386
+ var _this$parents$at;
387
+ if (((_this$parents$at = this.parents.at(-2)) == null ? void 0 : _this$parents$at.type) === 'AssignmentExpression') {
388
+ spaces += ' ';
389
+ }
390
+ out += '\n' + spaces;
391
+ }
374
392
  out += '/*';
375
393
  if (value.includes('\n')) {
376
394
  value = trimEndSpaces(value.replace(/^\s*\*/gm, '*')).split('\n').filter(_ => _.length).map((line, i) => (i ? spaces + ' ' : '') + line).join('\n');
@@ -709,12 +727,21 @@ class Stringifier {
709
727
  params
710
728
  } = node;
711
729
  let out = '';
730
+ if (node.leadingComments) {
731
+ if (this.parentType === 'AssignmentExpression') {
732
+ this.numSpaces++;
733
+ }
734
+ out += '\n' + this.spaces;
735
+ }
712
736
  if (async) {
713
737
  out += 'async ';
714
738
  }
715
739
  const star = generator ? '*' : '';
716
740
  out += 'function' + star + ' ' + this.toSource(id) + this.FunctionDeclarationParams(params);
717
741
  out += this.toSource(body);
742
+ if (node.leadingComments && this.parentType === 'AssignmentExpression') {
743
+ this.numSpaces--;
744
+ }
718
745
  return out;
719
746
  }
720
747
  ArrowFunctionExpression(node) {
@@ -1112,23 +1139,48 @@ class Stringifier {
1112
1139
  ExportAllDeclaration(node) {
1113
1140
  return `export * from ${node.source.extra.raw};`;
1114
1141
  }
1142
+ ExportNamespaceSpecifier(node) {
1143
+ const {
1144
+ exported
1145
+ } = node;
1146
+ return '* as ' + this.toSource(exported);
1147
+ }
1115
1148
  ExportNamedDeclaration(node) {
1116
1149
  const {
1117
1150
  declaration,
1118
1151
  source,
1119
- specifiers
1152
+ specifiers,
1153
+ loc
1120
1154
  } = node;
1121
1155
  let out = this.spaces;
1122
1156
  if (specifiers.length) {
1123
- out += 'export {\n';
1124
- out += this.mapToSource(specifiers).map(_ => ` ${this.spaces}${_}`).join(',\n');
1125
- out += '\n';
1126
- out += this.spaces;
1127
- out += '}';
1128
- if (source) {
1129
- out += " from " + this.toSource(source);
1157
+ if (!source) {
1158
+ console.warn("ExportNamedDeclaration> no source given");
1159
+ }
1160
+ if (specifiers.length === 1 && specifiers[0].type === 'ExportNamespaceSpecifier') {
1161
+ out += 'export ';
1162
+ out += this.toSource(specifiers[0]);
1163
+ out += ' from ';
1164
+ out += this.toSource(source);
1165
+ out += ";";
1166
+ } else {
1167
+ const sameLine = loc.start.line === loc.end.line;
1168
+ if (sameLine) {
1169
+ out += 'export {';
1170
+ out += this.mapToSource(specifiers).join(', ');
1171
+ out += '}';
1172
+ } else {
1173
+ out += 'export {\n';
1174
+ out += this.mapToSource(specifiers).map(_ => ` ${this.spaces}${_}`).join(',\n');
1175
+ out += '\n';
1176
+ out += this.spaces;
1177
+ out += '}';
1178
+ }
1179
+ if (source) {
1180
+ out += " from " + this.toSource(source);
1181
+ }
1182
+ out += ";\n";
1130
1183
  }
1131
- out += ";\n";
1132
1184
  } else if (declaration) {
1133
1185
  out += 'export ' + this.toSource(declaration);
1134
1186
  } else {
@@ -1666,9 +1718,6 @@ class Asserter extends Stringifier {
1666
1718
  i--;
1667
1719
  while (i >= 0) {
1668
1720
  parent = parents[i];
1669
- if (parent.type === 'CallExpression') {
1670
- break;
1671
- }
1672
1721
  if (nodeIsFunction(parent)) {
1673
1722
  break;
1674
1723
  }
@@ -1684,26 +1733,38 @@ class Asserter extends Stringifier {
1684
1733
  }
1685
1734
  console.warn(...args);
1686
1735
  }
1687
- getJSDoc(node) {
1688
- if (node.type === 'BlockStatement') {
1689
- node = this.parent;
1736
+ getLeadingCommentsNodeForFunctionExpression(node) {
1737
+ const {
1738
+ parents
1739
+ } = this;
1740
+ let i = parents.findLastIndex(_ => _ === node);
1741
+ let parent = parents[i];
1742
+ if (parent.leadingComments) {
1743
+ return parent;
1690
1744
  }
1691
- if (node.type === 'FunctionExpression') {
1692
- if (node.leadingComments) {
1693
- this.warn("Case of FunctionExpression containing its own leadingComments is not handled");
1694
- return;
1745
+ i--;
1746
+ while (i >= 0) {
1747
+ parent = parents[i];
1748
+ if (nodeIsFunction(parent)) {
1749
+ break;
1695
1750
  }
1696
- node = this.findParentOfType(node, 'ExpressionStatement');
1697
- if (!node) {
1698
- node = this.parents.findLast(_ => _.type === 'VariableDeclaration');
1699
- if (!node) {
1700
- return;
1701
- }
1751
+ if (parent.leadingComments) {
1752
+ return parent;
1702
1753
  }
1754
+ i--;
1755
+ }
1756
+ }
1757
+ getJSDoc(node) {
1758
+ if (node.type === 'BlockStatement') {
1759
+ node = this.parent;
1703
1760
  }
1704
1761
  let {
1705
1762
  leadingComments
1706
1763
  } = node;
1764
+ if (node.type === 'FunctionExpression') {
1765
+ const tmp = this.getLeadingCommentsNodeForFunctionExpression(node);
1766
+ leadingComments = tmp == null ? void 0 : tmp.leadingComments;
1767
+ }
1707
1768
  if (!leadingComments) {
1708
1769
  if (node.type === 'FunctionDeclaration') {
1709
1770
  const exportNamedDeclaration = this.findParentOfType(node, 'ExportNamedDeclaration');
@@ -1918,6 +1979,23 @@ class Asserter extends Stringifier {
1918
1979
  }
1919
1980
  return out;
1920
1981
  }
1982
+ getNameForFunctionExpression(node) {
1983
+ const objectProperty = this.findParentOfType(node, 'ObjectProperty');
1984
+ if (objectProperty) {
1985
+ return this.toSource(objectProperty.key);
1986
+ }
1987
+ const expressionStatement = this.findParentOfType(node, 'ExpressionStatement');
1988
+ if (expressionStatement) {
1989
+ const {
1990
+ left
1991
+ } = expressionStatement.expression;
1992
+ if (left) {
1993
+ return this.toSource(left);
1994
+ }
1995
+ console.warn("Asserter#getNameForFunctionExpression> expression without left");
1996
+ }
1997
+ return 'unnamed function expression';
1998
+ }
1921
1999
  getName(node) {
1922
2000
  const toSource = this.toSource.bind(this);
1923
2001
  if (node.type === 'BlockStatement') {
@@ -1944,11 +2022,7 @@ class Asserter extends Stringifier {
1944
2022
  if (id) {
1945
2023
  return toSource(id);
1946
2024
  }
1947
- const expressionStatement = this.findParentOfType(node, 'ExpressionStatement');
1948
- if (!expressionStatement) {
1949
- return 'unnamed function expression';
1950
- }
1951
- return toSource(expressionStatement.expression.left);
2025
+ return this.getNameForFunctionExpression(node);
1952
2026
  case 'ArrowFunctionExpression':
1953
2027
  const parent = this.findParentOfType(node, 'VariableDeclarator');
1954
2028
  if (parent) {
@@ -2118,6 +2192,9 @@ function toSourceTS(node) {
2118
2192
  ConditionalType,
2119
2193
  IndexedAccessType,
2120
2194
  RestType,
2195
+ TypeQuery,
2196
+ TypeOperator,
2197
+ KeyOfKeyword,
2121
2198
  ConstructorType
2122
2199
  } = ts.SyntaxKind;
2123
2200
  switch (node.kind) {
@@ -2193,6 +2270,21 @@ function toSourceTS(node) {
2193
2270
  };
2194
2271
  }
2195
2272
  return ret;
2273
+ case TypeQuery:
2274
+ const argument = toSourceTS(node.exprName);
2275
+ return {
2276
+ type: 'typeof',
2277
+ argument
2278
+ };
2279
+ case TypeOperator:
2280
+ if (node.operator === KeyOfKeyword) {
2281
+ const _argument = toSourceTS(node.type);
2282
+ return {
2283
+ type: 'keyof',
2284
+ argument: _argument
2285
+ };
2286
+ }
2287
+ console.warn("unimplemented TypeOperator", node);
2196
2288
  case TypeReference:
2197
2289
  {
2198
2290
  if ((typeName.text === 'Object' || typeName.text === 'Record') && (typeArguments == null ? void 0 : typeArguments.length) === 2) {
@@ -2473,6 +2565,12 @@ function toSourceBabelTS(node) {
2473
2565
  return toSourceBabelTS(node.type);
2474
2566
  case 'LastTypeNode':
2475
2567
  return toSourceBabelTS(node.qualifier);
2568
+ case 'TSTypeQuery':
2569
+ const argument = toSourceBabelTS(node.exprName);
2570
+ return {
2571
+ type: 'typeof',
2572
+ argument
2573
+ };
2476
2574
  default:
2477
2575
  console.warn('toSourceBabelTS> unhandled type', node.type, node);
2478
2576
  debugger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtime-type-inspector/transpiler",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Validating JSDoc types at runtime for high-quality types - Trust is good, control is better.",
5
5
  "main": "index.cjs",
6
6
  "module": "index.mjs",