@player-tools/xlr-converters 0.2.1-next.0 → 0.2.1-next.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/dist/index.cjs.js +56 -45
- package/dist/index.d.ts +2 -1
- package/dist/index.esm.js +57 -46
- package/package.json +4 -4
- package/src/ts-to-xlr.ts +91 -71
- package/src/xlr-to-ts.ts +3 -4
package/dist/index.cjs.js
CHANGED
|
@@ -38,6 +38,9 @@ var __spreadValues$1 = (a, b) => {
|
|
|
38
38
|
return a;
|
|
39
39
|
};
|
|
40
40
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
41
|
+
function isMappedTypeNode(x) {
|
|
42
|
+
return ["Pick", "Omit", "Required", "Partial"].includes(x);
|
|
43
|
+
}
|
|
41
44
|
const AnyTypeNode = {
|
|
42
45
|
type: "any"
|
|
43
46
|
};
|
|
@@ -327,34 +330,37 @@ class TsConverter {
|
|
|
327
330
|
clauses.forEach((heritageClause) => {
|
|
328
331
|
heritageClause.types.forEach((parent) => {
|
|
329
332
|
var _a;
|
|
330
|
-
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
333
|
+
let typeToApply;
|
|
334
|
+
const typeName = parent.expression.getText();
|
|
335
|
+
if (isMappedTypeNode(typeName)) {
|
|
336
|
+
typeToApply = this.makeMappedType(typeName, parent);
|
|
337
|
+
} else {
|
|
338
|
+
const parentType = typeChecker.getTypeAtLocation(parent);
|
|
339
|
+
const parentSymbol = parentType.symbol;
|
|
340
|
+
const parentDeclarations = parentSymbol == null ? void 0 : parentSymbol.declarations;
|
|
341
|
+
if (!(parentDeclarations == null ? void 0 : parentDeclarations[0])) {
|
|
342
|
+
this.context.throwError(`Error: Unable to get underlying interface for extending class ${parent.getFullText()}`);
|
|
343
|
+
}
|
|
344
|
+
let parentInterface;
|
|
345
|
+
if (ts__default["default"].isTypeLiteralNode(parentDeclarations == null ? void 0 : parentDeclarations[0]) && ts__default["default"].isTypeAliasDeclaration(parentDeclarations == null ? void 0 : parentDeclarations[0].parent)) {
|
|
346
|
+
parentInterface = parentDeclarations == null ? void 0 : parentDeclarations[0].parent;
|
|
347
|
+
} else {
|
|
348
|
+
parentInterface = parentDeclarations == null ? void 0 : parentDeclarations[0];
|
|
349
|
+
}
|
|
338
350
|
if (this.context.customPrimitives.includes(parentInterface.name.text)) {
|
|
339
351
|
extendsType = this.makeBasicRefNode(parent);
|
|
340
|
-
|
|
341
|
-
const parentInterfaceType = this.convertDeclaration(parentInterface);
|
|
342
|
-
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
343
|
-
const filledInInterface = this.solveGenerics(parentInterfaceType, parentInterface.typeParameters, parent.typeArguments);
|
|
344
|
-
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), filledInInterface.properties);
|
|
345
|
-
if (filledInInterface.additionalProperties) {
|
|
346
|
-
additionalPropertiesCollector.push(filledInInterface.additionalProperties);
|
|
347
|
-
}
|
|
348
|
-
} else {
|
|
349
|
-
if (xlrUtils.isGenericNodeType(baseObject)) {
|
|
350
|
-
baseObject.genericTokens.push(...(_a = parentInterfaceType.genericTokens) != null ? _a : []);
|
|
351
|
-
}
|
|
352
|
-
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), parentInterfaceType.properties);
|
|
353
|
-
if (parentInterfaceType.additionalProperties) {
|
|
354
|
-
additionalPropertiesCollector.push(parentInterfaceType.additionalProperties);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
352
|
+
return;
|
|
357
353
|
}
|
|
354
|
+
typeToApply = this.convertDeclaration(parentInterface);
|
|
355
|
+
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
356
|
+
typeToApply = this.solveGenerics(typeToApply, parentInterface.typeParameters, parent.typeArguments);
|
|
357
|
+
} else if (xlrUtils.isGenericNodeType(baseObject)) {
|
|
358
|
+
baseObject.genericTokens.push(...(_a = typeToApply.genericTokens) != null ? _a : []);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), typeToApply.properties);
|
|
362
|
+
if (typeToApply.additionalProperties) {
|
|
363
|
+
additionalPropertiesCollector.push(typeToApply.additionalProperties);
|
|
358
364
|
}
|
|
359
365
|
});
|
|
360
366
|
});
|
|
@@ -415,7 +421,7 @@ class TsConverter {
|
|
|
415
421
|
return genericArray;
|
|
416
422
|
}
|
|
417
423
|
resolveRefNode(node) {
|
|
418
|
-
var _a, _b, _c, _d, _e
|
|
424
|
+
var _a, _b, _c, _d, _e;
|
|
419
425
|
let refName;
|
|
420
426
|
if (node.typeName.kind === ts__default["default"].SyntaxKind.QualifiedName) {
|
|
421
427
|
refName = `${node.typeName.left.getText()}.${node.typeName.right.getText()}`;
|
|
@@ -455,20 +461,8 @@ class TsConverter {
|
|
|
455
461
|
valueType: (_e = this.convertTsTypeNode(valueType)) != null ? _e : AnyTypeNode
|
|
456
462
|
}, xlrUtils.decorateNode(node));
|
|
457
463
|
}
|
|
458
|
-
if (refName
|
|
459
|
-
|
|
460
|
-
const modifiers = (_g = node.typeArguments) == null ? void 0 : _g[1];
|
|
461
|
-
const baseObj = this.convertTsTypeNode(baseType);
|
|
462
|
-
const modifierNames = xlrUtils.getStringLiteralsFromUnion(modifiers);
|
|
463
|
-
return (_h = xlrUtils.applyPickOrOmitToNodeType(baseObj, refName, modifierNames)) != null ? _h : {
|
|
464
|
-
type: "never"
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
if (refName === "Partial" || refName === "Required") {
|
|
468
|
-
const baseType = (_i = node.typeArguments) == null ? void 0 : _i[0];
|
|
469
|
-
const baseObj = this.convertTsTypeNode(baseType);
|
|
470
|
-
const modifier = refName !== "Partial";
|
|
471
|
-
return xlrUtils.applyPartialOrRequiredToNodeType(baseObj, modifier);
|
|
464
|
+
if (isMappedTypeNode(refName)) {
|
|
465
|
+
return this.makeMappedType(refName, node);
|
|
472
466
|
}
|
|
473
467
|
if (!this.context.customPrimitives.includes(refName)) {
|
|
474
468
|
const typeInfo = xlrUtils.getReferencedType(node, this.context.typeChecker);
|
|
@@ -487,6 +481,23 @@ class TsConverter {
|
|
|
487
481
|
}
|
|
488
482
|
return this.makeBasicRefNode(node);
|
|
489
483
|
}
|
|
484
|
+
makeMappedType(refName, node) {
|
|
485
|
+
var _a, _b, _c;
|
|
486
|
+
if (refName === "Pick" || refName === "Omit") {
|
|
487
|
+
const baseType = (_a = node.typeArguments) == null ? void 0 : _a[0];
|
|
488
|
+
const modifiers = (_b = node.typeArguments) == null ? void 0 : _b[1];
|
|
489
|
+
const baseObj = this.convertTsTypeNode(baseType);
|
|
490
|
+
const modifierNames = xlrUtils.getStringLiteralsFromUnion(modifiers);
|
|
491
|
+
return xlrUtils.applyPickOrOmitToNodeType(baseObj, refName, modifierNames);
|
|
492
|
+
}
|
|
493
|
+
if (refName === "Partial" || refName === "Required") {
|
|
494
|
+
const baseType = (_c = node.typeArguments) == null ? void 0 : _c[0];
|
|
495
|
+
const baseObj = this.convertTsTypeNode(baseType);
|
|
496
|
+
const modifier = refName !== "Partial";
|
|
497
|
+
return xlrUtils.applyPartialOrRequiredToNodeType(baseObj, modifier);
|
|
498
|
+
}
|
|
499
|
+
this.context.throwError(`Can't convert non-MappedType ${refName}`);
|
|
500
|
+
}
|
|
490
501
|
makeBasicRefNode(node) {
|
|
491
502
|
const genericArgs = [];
|
|
492
503
|
if (node.typeArguments) {
|
|
@@ -760,16 +771,16 @@ ${comment.split("\n").map((s) => ` * ${s}`).join("\n")}
|
|
|
760
771
|
}
|
|
761
772
|
return ts__default["default"].addSyntheticLeadingComment(tsNode, ts__default["default"].SyntaxKind.MultiLineCommentTrivia, comment, true);
|
|
762
773
|
}
|
|
763
|
-
createTypeParameters(
|
|
764
|
-
return
|
|
765
|
-
return this.context.factory.createTypeParameterDeclaration(generic.symbol, this.createGenericArgumentNode(generic.constraints), this.createGenericArgumentNode(generic.default));
|
|
774
|
+
createTypeParameters(genericXLRNode) {
|
|
775
|
+
return genericXLRNode.genericTokens.map((generic) => {
|
|
776
|
+
return this.context.factory.createTypeParameterDeclaration(void 0, generic.symbol, this.createGenericArgumentNode(generic.constraints), this.createGenericArgumentNode(generic.default));
|
|
766
777
|
});
|
|
767
778
|
}
|
|
768
779
|
makeInterfaceDeclaration(name, node, generics) {
|
|
769
|
-
return this.context.factory.createInterfaceDeclaration(
|
|
780
|
+
return this.context.factory.createInterfaceDeclaration(this.context.factory.createModifiersFromModifierFlags(ts__default["default"].ModifierFlags.Export), this.context.factory.createIdentifier(name), generics, void 0, node);
|
|
770
781
|
}
|
|
771
782
|
makeTypeDeclaration(name, node, generics) {
|
|
772
|
-
return this.context.factory.createTypeAliasDeclaration(
|
|
783
|
+
return this.context.factory.createTypeAliasDeclaration(this.context.factory.createModifiersFromModifierFlags(ts__default["default"].ModifierFlags.Export), this.context.factory.createIdentifier(name), generics, node);
|
|
773
784
|
}
|
|
774
785
|
}
|
|
775
786
|
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ declare class TsConverter {
|
|
|
25
25
|
convertSourceFile(sourceFile: ts.SourceFile): {
|
|
26
26
|
data: {
|
|
27
27
|
version: number;
|
|
28
|
-
types: NamedType<NodeType
|
|
28
|
+
types: NonNullable<NamedType<NodeType>>[];
|
|
29
29
|
};
|
|
30
30
|
convertedTypes: string[];
|
|
31
31
|
};
|
|
@@ -40,6 +40,7 @@ declare class TsConverter {
|
|
|
40
40
|
private solveGenerics;
|
|
41
41
|
private generateGenerics;
|
|
42
42
|
private resolveRefNode;
|
|
43
|
+
private makeMappedType;
|
|
43
44
|
private makeBasicRefNode;
|
|
44
45
|
}
|
|
45
46
|
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
import { isExportedDeclaration, isGenericTypeDeclaration, decorateNode, isGenericInterfaceDeclaration, isNonNullable, buildTemplateRegex, resolveConditional, isOptionalProperty, tsStripOptionalType,
|
|
2
|
+
import { isExportedDeclaration, isGenericTypeDeclaration, decorateNode, isGenericInterfaceDeclaration, isNonNullable, buildTemplateRegex, resolveConditional, isOptionalProperty, tsStripOptionalType, isGenericNodeType, fillInGenerics, isTypeReferenceGeneric, getReferencedType, getStringLiteralsFromUnion, applyPickOrOmitToNodeType, applyPartialOrRequiredToNodeType, isTopLevelNode, isGenericNamedType, isPrimitiveTypeNode } from '@player-tools/xlr-utils';
|
|
3
3
|
|
|
4
4
|
class ConversionError extends Error {
|
|
5
5
|
constructor(msg) {
|
|
@@ -30,6 +30,9 @@ var __spreadValues$1 = (a, b) => {
|
|
|
30
30
|
return a;
|
|
31
31
|
};
|
|
32
32
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
33
|
+
function isMappedTypeNode(x) {
|
|
34
|
+
return ["Pick", "Omit", "Required", "Partial"].includes(x);
|
|
35
|
+
}
|
|
33
36
|
const AnyTypeNode = {
|
|
34
37
|
type: "any"
|
|
35
38
|
};
|
|
@@ -319,34 +322,37 @@ class TsConverter {
|
|
|
319
322
|
clauses.forEach((heritageClause) => {
|
|
320
323
|
heritageClause.types.forEach((parent) => {
|
|
321
324
|
var _a;
|
|
322
|
-
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
325
|
+
let typeToApply;
|
|
326
|
+
const typeName = parent.expression.getText();
|
|
327
|
+
if (isMappedTypeNode(typeName)) {
|
|
328
|
+
typeToApply = this.makeMappedType(typeName, parent);
|
|
329
|
+
} else {
|
|
330
|
+
const parentType = typeChecker.getTypeAtLocation(parent);
|
|
331
|
+
const parentSymbol = parentType.symbol;
|
|
332
|
+
const parentDeclarations = parentSymbol == null ? void 0 : parentSymbol.declarations;
|
|
333
|
+
if (!(parentDeclarations == null ? void 0 : parentDeclarations[0])) {
|
|
334
|
+
this.context.throwError(`Error: Unable to get underlying interface for extending class ${parent.getFullText()}`);
|
|
335
|
+
}
|
|
336
|
+
let parentInterface;
|
|
337
|
+
if (ts.isTypeLiteralNode(parentDeclarations == null ? void 0 : parentDeclarations[0]) && ts.isTypeAliasDeclaration(parentDeclarations == null ? void 0 : parentDeclarations[0].parent)) {
|
|
338
|
+
parentInterface = parentDeclarations == null ? void 0 : parentDeclarations[0].parent;
|
|
339
|
+
} else {
|
|
340
|
+
parentInterface = parentDeclarations == null ? void 0 : parentDeclarations[0];
|
|
341
|
+
}
|
|
330
342
|
if (this.context.customPrimitives.includes(parentInterface.name.text)) {
|
|
331
343
|
extendsType = this.makeBasicRefNode(parent);
|
|
332
|
-
|
|
333
|
-
const parentInterfaceType = this.convertDeclaration(parentInterface);
|
|
334
|
-
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
335
|
-
const filledInInterface = this.solveGenerics(parentInterfaceType, parentInterface.typeParameters, parent.typeArguments);
|
|
336
|
-
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), filledInInterface.properties);
|
|
337
|
-
if (filledInInterface.additionalProperties) {
|
|
338
|
-
additionalPropertiesCollector.push(filledInInterface.additionalProperties);
|
|
339
|
-
}
|
|
340
|
-
} else {
|
|
341
|
-
if (isGenericNodeType(baseObject)) {
|
|
342
|
-
baseObject.genericTokens.push(...(_a = parentInterfaceType.genericTokens) != null ? _a : []);
|
|
343
|
-
}
|
|
344
|
-
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), parentInterfaceType.properties);
|
|
345
|
-
if (parentInterfaceType.additionalProperties) {
|
|
346
|
-
additionalPropertiesCollector.push(parentInterfaceType.additionalProperties);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
344
|
+
return;
|
|
349
345
|
}
|
|
346
|
+
typeToApply = this.convertDeclaration(parentInterface);
|
|
347
|
+
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
348
|
+
typeToApply = this.solveGenerics(typeToApply, parentInterface.typeParameters, parent.typeArguments);
|
|
349
|
+
} else if (isGenericNodeType(baseObject)) {
|
|
350
|
+
baseObject.genericTokens.push(...(_a = typeToApply.genericTokens) != null ? _a : []);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
newProperties = __spreadValues$1(__spreadValues$1({}, newProperties), typeToApply.properties);
|
|
354
|
+
if (typeToApply.additionalProperties) {
|
|
355
|
+
additionalPropertiesCollector.push(typeToApply.additionalProperties);
|
|
350
356
|
}
|
|
351
357
|
});
|
|
352
358
|
});
|
|
@@ -407,7 +413,7 @@ class TsConverter {
|
|
|
407
413
|
return genericArray;
|
|
408
414
|
}
|
|
409
415
|
resolveRefNode(node) {
|
|
410
|
-
var _a, _b, _c, _d, _e
|
|
416
|
+
var _a, _b, _c, _d, _e;
|
|
411
417
|
let refName;
|
|
412
418
|
if (node.typeName.kind === ts.SyntaxKind.QualifiedName) {
|
|
413
419
|
refName = `${node.typeName.left.getText()}.${node.typeName.right.getText()}`;
|
|
@@ -447,20 +453,8 @@ class TsConverter {
|
|
|
447
453
|
valueType: (_e = this.convertTsTypeNode(valueType)) != null ? _e : AnyTypeNode
|
|
448
454
|
}, decorateNode(node));
|
|
449
455
|
}
|
|
450
|
-
if (refName
|
|
451
|
-
|
|
452
|
-
const modifiers = (_g = node.typeArguments) == null ? void 0 : _g[1];
|
|
453
|
-
const baseObj = this.convertTsTypeNode(baseType);
|
|
454
|
-
const modifierNames = getStringLiteralsFromUnion(modifiers);
|
|
455
|
-
return (_h = applyPickOrOmitToNodeType(baseObj, refName, modifierNames)) != null ? _h : {
|
|
456
|
-
type: "never"
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
if (refName === "Partial" || refName === "Required") {
|
|
460
|
-
const baseType = (_i = node.typeArguments) == null ? void 0 : _i[0];
|
|
461
|
-
const baseObj = this.convertTsTypeNode(baseType);
|
|
462
|
-
const modifier = refName !== "Partial";
|
|
463
|
-
return applyPartialOrRequiredToNodeType(baseObj, modifier);
|
|
456
|
+
if (isMappedTypeNode(refName)) {
|
|
457
|
+
return this.makeMappedType(refName, node);
|
|
464
458
|
}
|
|
465
459
|
if (!this.context.customPrimitives.includes(refName)) {
|
|
466
460
|
const typeInfo = getReferencedType(node, this.context.typeChecker);
|
|
@@ -479,6 +473,23 @@ class TsConverter {
|
|
|
479
473
|
}
|
|
480
474
|
return this.makeBasicRefNode(node);
|
|
481
475
|
}
|
|
476
|
+
makeMappedType(refName, node) {
|
|
477
|
+
var _a, _b, _c;
|
|
478
|
+
if (refName === "Pick" || refName === "Omit") {
|
|
479
|
+
const baseType = (_a = node.typeArguments) == null ? void 0 : _a[0];
|
|
480
|
+
const modifiers = (_b = node.typeArguments) == null ? void 0 : _b[1];
|
|
481
|
+
const baseObj = this.convertTsTypeNode(baseType);
|
|
482
|
+
const modifierNames = getStringLiteralsFromUnion(modifiers);
|
|
483
|
+
return applyPickOrOmitToNodeType(baseObj, refName, modifierNames);
|
|
484
|
+
}
|
|
485
|
+
if (refName === "Partial" || refName === "Required") {
|
|
486
|
+
const baseType = (_c = node.typeArguments) == null ? void 0 : _c[0];
|
|
487
|
+
const baseObj = this.convertTsTypeNode(baseType);
|
|
488
|
+
const modifier = refName !== "Partial";
|
|
489
|
+
return applyPartialOrRequiredToNodeType(baseObj, modifier);
|
|
490
|
+
}
|
|
491
|
+
this.context.throwError(`Can't convert non-MappedType ${refName}`);
|
|
492
|
+
}
|
|
482
493
|
makeBasicRefNode(node) {
|
|
483
494
|
const genericArgs = [];
|
|
484
495
|
if (node.typeArguments) {
|
|
@@ -752,16 +763,16 @@ ${comment.split("\n").map((s) => ` * ${s}`).join("\n")}
|
|
|
752
763
|
}
|
|
753
764
|
return ts.addSyntheticLeadingComment(tsNode, ts.SyntaxKind.MultiLineCommentTrivia, comment, true);
|
|
754
765
|
}
|
|
755
|
-
createTypeParameters(
|
|
756
|
-
return
|
|
757
|
-
return this.context.factory.createTypeParameterDeclaration(generic.symbol, this.createGenericArgumentNode(generic.constraints), this.createGenericArgumentNode(generic.default));
|
|
766
|
+
createTypeParameters(genericXLRNode) {
|
|
767
|
+
return genericXLRNode.genericTokens.map((generic) => {
|
|
768
|
+
return this.context.factory.createTypeParameterDeclaration(void 0, generic.symbol, this.createGenericArgumentNode(generic.constraints), this.createGenericArgumentNode(generic.default));
|
|
758
769
|
});
|
|
759
770
|
}
|
|
760
771
|
makeInterfaceDeclaration(name, node, generics) {
|
|
761
|
-
return this.context.factory.createInterfaceDeclaration(
|
|
772
|
+
return this.context.factory.createInterfaceDeclaration(this.context.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), this.context.factory.createIdentifier(name), generics, void 0, node);
|
|
762
773
|
}
|
|
763
774
|
makeTypeDeclaration(name, node, generics) {
|
|
764
|
-
return this.context.factory.createTypeAliasDeclaration(
|
|
775
|
+
return this.context.factory.createTypeAliasDeclaration(this.context.factory.createModifiersFromModifierFlags(ts.ModifierFlags.Export), this.context.factory.createIdentifier(name), generics, node);
|
|
765
776
|
}
|
|
766
777
|
}
|
|
767
778
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@player-tools/xlr-converters",
|
|
3
|
-
"version": "0.2.1-next.
|
|
3
|
+
"version": "0.2.1-next.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
7
7
|
},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"typescript": "4.
|
|
9
|
+
"typescript": "4.8.4"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@player-tools/xlr": "0.2.1-next.
|
|
13
|
-
"@player-tools/xlr-utils": "0.2.1-next.
|
|
12
|
+
"@player-tools/xlr": "0.2.1-next.2",
|
|
13
|
+
"@player-tools/xlr-utils": "0.2.1-next.2",
|
|
14
14
|
"@babel/runtime": "7.15.4"
|
|
15
15
|
},
|
|
16
16
|
"main": "dist/index.cjs.js",
|
package/src/ts-to-xlr.ts
CHANGED
|
@@ -35,6 +35,15 @@ import {
|
|
|
35
35
|
} from '@player-tools/xlr-utils';
|
|
36
36
|
import { ConversionError } from './types';
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Returns if the string is one of TypeScript's MappedTypes
|
|
40
|
+
*/
|
|
41
|
+
export function isMappedTypeNode(
|
|
42
|
+
x: string
|
|
43
|
+
): x is 'Pick' | 'Omit' | 'Required' | 'Partial' {
|
|
44
|
+
return ['Pick', 'Omit', 'Required', 'Partial'].includes(x);
|
|
45
|
+
}
|
|
46
|
+
|
|
38
47
|
export interface TSConverterContext {
|
|
39
48
|
/** */
|
|
40
49
|
customPrimitives: Array<string>;
|
|
@@ -486,66 +495,66 @@ export class TsConverter {
|
|
|
486
495
|
|
|
487
496
|
clauses.forEach((heritageClause) => {
|
|
488
497
|
heritageClause.types.forEach((parent) => {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
498
|
+
let typeToApply: ObjectType;
|
|
499
|
+
|
|
500
|
+
// Check if its a Mapped Type
|
|
501
|
+
const typeName = parent.expression.getText();
|
|
502
|
+
if (isMappedTypeNode(typeName)) {
|
|
503
|
+
typeToApply = this.makeMappedType(typeName, parent) as ObjectType;
|
|
504
|
+
} else {
|
|
505
|
+
const parentType = typeChecker.getTypeAtLocation(parent);
|
|
506
|
+
const parentSymbol = parentType.symbol;
|
|
507
|
+
const parentDeclarations = parentSymbol?.declarations;
|
|
508
|
+
|
|
509
|
+
if (!parentDeclarations?.[0]) {
|
|
510
|
+
this.context.throwError(
|
|
511
|
+
`Error: Unable to get underlying interface for extending class ${parent.getFullText()}`
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
let parentInterface: TopLevelDeclaration;
|
|
516
|
+
|
|
517
|
+
if (
|
|
518
|
+
ts.isTypeLiteralNode(parentDeclarations?.[0]) &&
|
|
519
|
+
ts.isTypeAliasDeclaration(parentDeclarations?.[0].parent)
|
|
520
|
+
) {
|
|
521
|
+
// check for if the node is a type to get the actual type declaration
|
|
522
|
+
parentInterface = parentDeclarations?.[0].parent;
|
|
523
|
+
} else {
|
|
524
|
+
parentInterface = parentDeclarations?.[0] as TopLevelDeclaration;
|
|
525
|
+
}
|
|
502
526
|
|
|
503
|
-
if (parentInterface && isTopLevelNode(parentInterface)) {
|
|
504
527
|
if (
|
|
505
528
|
this.context.customPrimitives.includes(parentInterface.name.text)
|
|
506
529
|
) {
|
|
507
530
|
extendsType = this.makeBasicRefNode(parent);
|
|
508
|
-
|
|
509
|
-
const parentInterfaceType =
|
|
510
|
-
this.convertDeclaration(parentInterface);
|
|
511
|
-
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
512
|
-
const filledInInterface = this.solveGenerics(
|
|
513
|
-
parentInterfaceType as NodeTypeWithGenerics,
|
|
514
|
-
parentInterface.typeParameters,
|
|
515
|
-
parent.typeArguments
|
|
516
|
-
) as NamedType<ObjectType>;
|
|
517
|
-
newProperties = {
|
|
518
|
-
...newProperties,
|
|
519
|
-
...filledInInterface.properties,
|
|
520
|
-
};
|
|
521
|
-
if (filledInInterface.additionalProperties) {
|
|
522
|
-
additionalPropertiesCollector.push(
|
|
523
|
-
filledInInterface.additionalProperties
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
} else {
|
|
527
|
-
if (isGenericNodeType(baseObject)) {
|
|
528
|
-
baseObject.genericTokens.push(
|
|
529
|
-
...((parentInterfaceType as NodeTypeWithGenerics)
|
|
530
|
-
.genericTokens ?? [])
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
newProperties = {
|
|
535
|
-
...newProperties,
|
|
536
|
-
...parentInterfaceType.properties,
|
|
537
|
-
};
|
|
538
|
-
if (parentInterfaceType.additionalProperties) {
|
|
539
|
-
additionalPropertiesCollector.push(
|
|
540
|
-
parentInterfaceType.additionalProperties
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
531
|
+
return;
|
|
544
532
|
}
|
|
533
|
+
|
|
534
|
+
typeToApply = this.convertDeclaration(parentInterface);
|
|
535
|
+
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
536
|
+
typeToApply = this.solveGenerics(
|
|
537
|
+
typeToApply as NodeTypeWithGenerics,
|
|
538
|
+
parentInterface.typeParameters,
|
|
539
|
+
parent.typeArguments
|
|
540
|
+
) as NamedType<ObjectType>;
|
|
541
|
+
} else if (isGenericNodeType(baseObject)) {
|
|
542
|
+
baseObject.genericTokens.push(
|
|
543
|
+
...((typeToApply as NodeTypeWithGenerics).genericTokens ?? [])
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
newProperties = {
|
|
549
|
+
...newProperties,
|
|
550
|
+
...typeToApply.properties,
|
|
551
|
+
};
|
|
552
|
+
if (typeToApply.additionalProperties) {
|
|
553
|
+
additionalPropertiesCollector.push(typeToApply.additionalProperties);
|
|
545
554
|
}
|
|
546
555
|
});
|
|
547
556
|
});
|
|
548
|
-
|
|
557
|
+
// Resolve Additional Properties
|
|
549
558
|
let additionalProperties: NodeType | false = false;
|
|
550
559
|
if (baseObject.additionalProperties === false) {
|
|
551
560
|
if (additionalPropertiesCollector.length === 1) {
|
|
@@ -682,26 +691,8 @@ export class TsConverter {
|
|
|
682
691
|
};
|
|
683
692
|
}
|
|
684
693
|
|
|
685
|
-
if (refName
|
|
686
|
-
|
|
687
|
-
const modifiers = node.typeArguments?.[1] as ts.TypeNode;
|
|
688
|
-
|
|
689
|
-
const baseObj = this.convertTsTypeNode(baseType) as NamedType<ObjectType>;
|
|
690
|
-
const modifierNames = getStringLiteralsFromUnion(modifiers);
|
|
691
|
-
|
|
692
|
-
return (
|
|
693
|
-
applyPickOrOmitToNodeType(baseObj, refName, modifierNames) ?? {
|
|
694
|
-
type: 'never',
|
|
695
|
-
}
|
|
696
|
-
);
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
if (refName === 'Partial' || refName === 'Required') {
|
|
700
|
-
const baseType = node.typeArguments?.[0] as ts.TypeNode;
|
|
701
|
-
const baseObj = this.convertTsTypeNode(baseType) as NodeType;
|
|
702
|
-
const modifier = refName !== 'Partial';
|
|
703
|
-
|
|
704
|
-
return applyPartialOrRequiredToNodeType(baseObj, modifier);
|
|
694
|
+
if (isMappedTypeNode(refName)) {
|
|
695
|
+
return this.makeMappedType(refName, node);
|
|
705
696
|
}
|
|
706
697
|
|
|
707
698
|
// catch all for all other type references
|
|
@@ -732,6 +723,35 @@ export class TsConverter {
|
|
|
732
723
|
return this.makeBasicRefNode(node);
|
|
733
724
|
}
|
|
734
725
|
|
|
726
|
+
private makeMappedType(
|
|
727
|
+
refName: 'Pick' | 'Omit' | 'Partial' | 'Required',
|
|
728
|
+
node: ts.NodeWithTypeArguments
|
|
729
|
+
): ObjectType {
|
|
730
|
+
if (refName === 'Pick' || refName === 'Omit') {
|
|
731
|
+
const baseType = node.typeArguments?.[0] as ts.TypeNode;
|
|
732
|
+
const modifiers = node.typeArguments?.[1] as ts.TypeNode;
|
|
733
|
+
|
|
734
|
+
const baseObj = this.convertTsTypeNode(baseType) as NamedType<ObjectType>;
|
|
735
|
+
const modifierNames = getStringLiteralsFromUnion(modifiers);
|
|
736
|
+
|
|
737
|
+
return applyPickOrOmitToNodeType(
|
|
738
|
+
baseObj,
|
|
739
|
+
refName,
|
|
740
|
+
modifierNames
|
|
741
|
+
) as ObjectType;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (refName === 'Partial' || refName === 'Required') {
|
|
745
|
+
const baseType = node.typeArguments?.[0] as ts.TypeNode;
|
|
746
|
+
const baseObj = this.convertTsTypeNode(baseType) as NodeType;
|
|
747
|
+
const modifier = refName !== 'Partial';
|
|
748
|
+
|
|
749
|
+
return applyPartialOrRequiredToNodeType(baseObj, modifier) as ObjectType;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
this.context.throwError(`Can't convert non-MappedType ${refName}`);
|
|
753
|
+
}
|
|
754
|
+
|
|
735
755
|
private makeBasicRefNode(node: ts.NodeWithTypeArguments): RefType {
|
|
736
756
|
const genericArgs: Array<NodeType | NamedType<ObjectType>> = [];
|
|
737
757
|
if (node.typeArguments) {
|
package/src/xlr-to-ts.ts
CHANGED
|
@@ -465,10 +465,11 @@ export class TSWriter {
|
|
|
465
465
|
}
|
|
466
466
|
|
|
467
467
|
private createTypeParameters(
|
|
468
|
-
|
|
468
|
+
genericXLRNode: NodeTypeWithGenerics
|
|
469
469
|
): Array<ts.TypeParameterDeclaration> {
|
|
470
|
-
return
|
|
470
|
+
return genericXLRNode.genericTokens.map((generic) => {
|
|
471
471
|
return this.context.factory.createTypeParameterDeclaration(
|
|
472
|
+
undefined,
|
|
472
473
|
generic.symbol,
|
|
473
474
|
this.createGenericArgumentNode(generic.constraints),
|
|
474
475
|
this.createGenericArgumentNode(generic.default)
|
|
@@ -482,7 +483,6 @@ export class TSWriter {
|
|
|
482
483
|
generics: Array<ts.TypeParameterDeclaration> | undefined
|
|
483
484
|
) {
|
|
484
485
|
return this.context.factory.createInterfaceDeclaration(
|
|
485
|
-
undefined,
|
|
486
486
|
this.context.factory.createModifiersFromModifierFlags(
|
|
487
487
|
ts.ModifierFlags.Export
|
|
488
488
|
),
|
|
@@ -499,7 +499,6 @@ export class TSWriter {
|
|
|
499
499
|
generics: Array<ts.TypeParameterDeclaration> | undefined
|
|
500
500
|
) {
|
|
501
501
|
return this.context.factory.createTypeAliasDeclaration(
|
|
502
|
-
undefined, // decorators
|
|
503
502
|
this.context.factory.createModifiersFromModifierFlags(
|
|
504
503
|
ts.ModifierFlags.Export
|
|
505
504
|
),
|