@player-tools/xlr-converters 0.2.2--canary.17.363 → 0.3.0-next.0
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 +246 -88
- package/dist/index.d.ts +13 -7
- package/dist/index.esm.js +247 -89
- package/package.json +3 -3
- package/src/ts-to-xlr.ts +351 -116
- package/src/xlr-to-ts.ts +39 -7
package/dist/index.cjs.js
CHANGED
|
@@ -59,25 +59,34 @@ class TsConverter {
|
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
convertSourceFile(sourceFile) {
|
|
62
|
-
const declarations = sourceFile.statements.filter(
|
|
63
|
-
const types = declarations.filter((declaration) => xlrUtils.isExportedDeclaration(declaration)).map((statement) => this.
|
|
62
|
+
const declarations = sourceFile.statements.filter(xlrUtils.isTopLevelNode);
|
|
63
|
+
const types = declarations.filter((declaration) => xlrUtils.isExportedDeclaration(declaration)).map((statement) => this.convertTopLevelNode(statement)).filter((v) => !!v);
|
|
64
64
|
return {
|
|
65
65
|
data: { version: 1, types },
|
|
66
66
|
convertedTypes: types.map(({ name }) => name)
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
var _a;
|
|
69
|
+
convertTopLevelNode(node) {
|
|
71
70
|
const sourceFile = node.parent;
|
|
72
71
|
const { fileName } = sourceFile;
|
|
72
|
+
if (ts__default["default"].isVariableStatement(node)) {
|
|
73
|
+
return __spreadValues$1({
|
|
74
|
+
source: fileName
|
|
75
|
+
}, this.convertVariable(node));
|
|
76
|
+
}
|
|
77
|
+
return __spreadValues$1({
|
|
78
|
+
source: fileName
|
|
79
|
+
}, this.convertDeclaration(node));
|
|
80
|
+
}
|
|
81
|
+
convertDeclaration(node) {
|
|
82
|
+
var _a;
|
|
73
83
|
if (ts__default["default"].isTypeAliasDeclaration(node)) {
|
|
74
84
|
let genericTokens;
|
|
75
85
|
if (xlrUtils.isGenericTypeDeclaration(node)) {
|
|
76
86
|
genericTokens = this.generateGenerics(node.typeParameters);
|
|
77
87
|
}
|
|
78
88
|
return __spreadProps(__spreadValues$1(__spreadValues$1({
|
|
79
|
-
name: node.name.getText()
|
|
80
|
-
source: fileName
|
|
89
|
+
name: node.name.getText()
|
|
81
90
|
}, (_a = this.convertTsTypeNode(node.type)) != null ? _a : AnyTypeNode), xlrUtils.decorateNode(node)), {
|
|
82
91
|
genericTokens
|
|
83
92
|
});
|
|
@@ -89,9 +98,8 @@ class TsConverter {
|
|
|
89
98
|
}
|
|
90
99
|
const baseObject = __spreadProps(__spreadValues$1(__spreadValues$1({
|
|
91
100
|
name: node.name.getText(),
|
|
92
|
-
type: "object"
|
|
93
|
-
|
|
94
|
-
}, this.fromTsObjectMembers(node)), xlrUtils.decorateNode(node)), {
|
|
101
|
+
type: "object"
|
|
102
|
+
}, this.tsObjectMembersToProperties(node)), xlrUtils.decorateNode(node)), {
|
|
95
103
|
genericTokens
|
|
96
104
|
});
|
|
97
105
|
if (node.heritageClauses) {
|
|
@@ -101,6 +109,24 @@ class TsConverter {
|
|
|
101
109
|
}
|
|
102
110
|
this.context.throwError(`Error: type node is not an Interface or a Type, can't convert as Declaration`);
|
|
103
111
|
}
|
|
112
|
+
convertVariable(node) {
|
|
113
|
+
const variableDeclarations = node.declarationList.declarations;
|
|
114
|
+
if (variableDeclarations.length === 1) {
|
|
115
|
+
const variable = variableDeclarations[0];
|
|
116
|
+
if (variable.initializer) {
|
|
117
|
+
let resultingNode;
|
|
118
|
+
if (ts__default["default"].isCallExpression(variable.initializer) || ts__default["default"].isArrowFunction(variable.initializer)) {
|
|
119
|
+
resultingNode = this.resolveFunctionCall(variable.initializer, node.parent);
|
|
120
|
+
} else {
|
|
121
|
+
resultingNode = this.tsLiteralToType(variable.initializer);
|
|
122
|
+
}
|
|
123
|
+
return __spreadValues$1({
|
|
124
|
+
name: variable.name.getText()
|
|
125
|
+
}, resultingNode);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.context.throwError(`Error: Multi-variable declaration on line ${node.pos} is not supported for conversion`);
|
|
129
|
+
}
|
|
104
130
|
convertTsTypeNode(node) {
|
|
105
131
|
if (this.context.cache.convertedNodes.has(node)) {
|
|
106
132
|
const cachedType = this.context.cache.convertedNodes.get(node);
|
|
@@ -125,7 +151,10 @@ class TsConverter {
|
|
|
125
151
|
}, xlrUtils.decorateNode(node));
|
|
126
152
|
}
|
|
127
153
|
if (ts__default["default"].isParenthesizedTypeNode(node)) {
|
|
128
|
-
const children = [
|
|
154
|
+
const children = [];
|
|
155
|
+
node.forEachChild((child) => {
|
|
156
|
+
children.push(child);
|
|
157
|
+
});
|
|
129
158
|
if (((_a = children[0]) == null ? void 0 : _a.kind) === ts__default["default"].SyntaxKind.OpenParenToken) {
|
|
130
159
|
children.shift();
|
|
131
160
|
}
|
|
@@ -166,6 +195,11 @@ class TsConverter {
|
|
|
166
195
|
additionalProperties: AnyTypeNode
|
|
167
196
|
}, xlrUtils.decorateNode(node));
|
|
168
197
|
}
|
|
198
|
+
if (node.kind === ts__default["default"].SyntaxKind.VoidKeyword) {
|
|
199
|
+
return __spreadValues$1({
|
|
200
|
+
type: "void"
|
|
201
|
+
}, xlrUtils.decorateNode(node));
|
|
202
|
+
}
|
|
169
203
|
if (ts__default["default"].isTemplateLiteralTypeNode(node)) {
|
|
170
204
|
let format;
|
|
171
205
|
if (this.context.cache.convertedTemplates.has(node)) {
|
|
@@ -205,45 +239,17 @@ class TsConverter {
|
|
|
205
239
|
if (ts__default["default"].isTupleTypeNode(node)) {
|
|
206
240
|
return __spreadValues$1(__spreadValues$1({
|
|
207
241
|
type: "tuple"
|
|
208
|
-
}, this.
|
|
242
|
+
}, this.tsTupleToType(node)), xlrUtils.decorateNode(node));
|
|
209
243
|
}
|
|
210
244
|
if (ts__default["default"].isLiteralTypeNode(node)) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const: Number(node.literal.text)
|
|
215
|
-
}, xlrUtils.decorateNode(node));
|
|
216
|
-
}
|
|
217
|
-
if (ts__default["default"].isStringLiteral(node.literal)) {
|
|
218
|
-
return __spreadValues$1({
|
|
219
|
-
type: "string",
|
|
220
|
-
const: node.literal.text
|
|
221
|
-
}, xlrUtils.decorateNode(node));
|
|
222
|
-
}
|
|
223
|
-
if (node.literal.kind === ts__default["default"].SyntaxKind.TrueKeyword) {
|
|
224
|
-
return __spreadValues$1({
|
|
225
|
-
type: "boolean",
|
|
226
|
-
const: true
|
|
227
|
-
}, xlrUtils.decorateNode(node));
|
|
228
|
-
}
|
|
229
|
-
if (node.literal.kind === ts__default["default"].SyntaxKind.FalseKeyword) {
|
|
230
|
-
return __spreadValues$1({
|
|
231
|
-
type: "boolean",
|
|
232
|
-
const: false
|
|
233
|
-
}, xlrUtils.decorateNode(node));
|
|
234
|
-
}
|
|
235
|
-
if (node.literal.kind === ts__default["default"].SyntaxKind.NullKeyword) {
|
|
236
|
-
return __spreadValues$1({ type: "null" }, xlrUtils.decorateNode(node));
|
|
237
|
-
}
|
|
238
|
-
if (node.literal.kind === ts__default["default"].SyntaxKind.PrefixUnaryExpression) {
|
|
239
|
-
this.context.throwError("Prefix unary expressions not supported");
|
|
240
|
-
}
|
|
241
|
-
this.context.throwError("Literal type not understood");
|
|
242
|
-
} else if (ts__default["default"].isTypeLiteralNode(node)) {
|
|
245
|
+
return this.tsLiteralToType(node.literal);
|
|
246
|
+
}
|
|
247
|
+
if (ts__default["default"].isTypeLiteralNode(node)) {
|
|
243
248
|
return __spreadValues$1(__spreadValues$1({
|
|
244
249
|
type: "object"
|
|
245
|
-
}, this.
|
|
246
|
-
}
|
|
250
|
+
}, this.tsObjectMembersToProperties(node)), xlrUtils.decorateNode(node));
|
|
251
|
+
}
|
|
252
|
+
if (ts__default["default"].isFunctionTypeNode(node) || ts__default["default"].isFunctionDeclaration(node) || ts__default["default"].isArrowFunction(node)) {
|
|
247
253
|
const parameters = node.parameters.map((param) => {
|
|
248
254
|
let typeNode;
|
|
249
255
|
if (param.type) {
|
|
@@ -256,13 +262,17 @@ class TsConverter {
|
|
|
256
262
|
default: param.initializer ? this.convertTsTypeNode(param.initializer) : void 0
|
|
257
263
|
};
|
|
258
264
|
});
|
|
259
|
-
|
|
265
|
+
let returnType;
|
|
266
|
+
if (node.type !== void 0) {
|
|
267
|
+
returnType = this.convertTsTypeNode(node.type);
|
|
268
|
+
}
|
|
260
269
|
return __spreadValues$1({
|
|
261
270
|
type: "function",
|
|
262
271
|
parameters,
|
|
263
272
|
returnType
|
|
264
273
|
}, xlrUtils.decorateNode(node));
|
|
265
|
-
}
|
|
274
|
+
}
|
|
275
|
+
if (ts__default["default"].isIndexedAccessTypeNode(node)) {
|
|
266
276
|
if (ts__default["default"].isTypeReferenceNode(node.objectType) && ts__default["default"].isLiteralTypeNode(node.indexType)) {
|
|
267
277
|
const baseObject = this.convertTsTypeNode(node.objectType);
|
|
268
278
|
const accessor = node.indexType.literal.getText().replace(/["']/g, "");
|
|
@@ -282,36 +292,147 @@ class TsConverter {
|
|
|
282
292
|
}
|
|
283
293
|
}
|
|
284
294
|
if (ts__default["default"].isTypeQueryNode(node.objectType)) {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
295
|
+
const elements = this.tsNodeToType(node.objectType);
|
|
296
|
+
return {
|
|
297
|
+
type: "or",
|
|
298
|
+
or: [...elements.elementTypes.map((element) => element.type)]
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
this.context.throwError(`Error: could not solve IndexedAccessType ${node.getFullText()}`);
|
|
302
|
+
}
|
|
303
|
+
if (ts__default["default"].isTypeQueryNode(node)) {
|
|
304
|
+
const effectiveType = this.context.typeChecker.getTypeAtLocation(node);
|
|
305
|
+
const syntheticType = this.context.typeChecker.typeToTypeNode(effectiveType, node, void 0);
|
|
306
|
+
if (syntheticType) {
|
|
307
|
+
return this.tsNodeToType(syntheticType);
|
|
308
|
+
}
|
|
309
|
+
this.context.throwError(`Error: could not synthesize type for ${node.getText()}`);
|
|
310
|
+
}
|
|
311
|
+
if (ts__default["default"].isTypeOperatorNode(node)) {
|
|
312
|
+
return this.tsNodeToType(node.type);
|
|
313
|
+
}
|
|
314
|
+
this.context.throwError(`Unimplemented type ${ts__default["default"].SyntaxKind[node.kind]} at ${node.getText()}`);
|
|
315
|
+
}
|
|
316
|
+
tsLiteralToType(node) {
|
|
317
|
+
if (ts__default["default"].isNumericLiteral(node)) {
|
|
318
|
+
return __spreadValues$1({
|
|
319
|
+
type: "number",
|
|
320
|
+
const: Number(node.text)
|
|
321
|
+
}, xlrUtils.decorateNode(node));
|
|
322
|
+
}
|
|
323
|
+
if (ts__default["default"].isStringLiteral(node)) {
|
|
324
|
+
return __spreadValues$1({
|
|
325
|
+
type: "string",
|
|
326
|
+
const: node.text
|
|
327
|
+
}, xlrUtils.decorateNode(node));
|
|
328
|
+
}
|
|
329
|
+
if (node.kind === ts__default["default"].SyntaxKind.TrueKeyword) {
|
|
330
|
+
return __spreadValues$1({
|
|
331
|
+
type: "boolean",
|
|
332
|
+
const: true
|
|
333
|
+
}, xlrUtils.decorateNode(node));
|
|
334
|
+
}
|
|
335
|
+
if (node.kind === ts__default["default"].SyntaxKind.FalseKeyword) {
|
|
336
|
+
return __spreadValues$1({
|
|
337
|
+
type: "boolean",
|
|
338
|
+
const: false
|
|
339
|
+
}, xlrUtils.decorateNode(node));
|
|
340
|
+
}
|
|
341
|
+
if (node.kind === ts__default["default"].SyntaxKind.NullKeyword) {
|
|
342
|
+
return __spreadValues$1({ type: "null" }, xlrUtils.decorateNode(node));
|
|
343
|
+
}
|
|
344
|
+
if (ts__default["default"].isPrefixUnaryExpression(node)) {
|
|
345
|
+
this.context.throwError("Prefix unary expressions not supported");
|
|
346
|
+
}
|
|
347
|
+
if (ts__default["default"].isArrayLiteralExpression(node)) {
|
|
348
|
+
const arrayElements = [];
|
|
349
|
+
node.elements.forEach((element) => {
|
|
350
|
+
var _a;
|
|
351
|
+
if (ts__default["default"].isSpreadElement(element)) {
|
|
352
|
+
const arrayReference = this.resolveLiteralReference(element.expression);
|
|
353
|
+
arrayElements.push(...(_a = arrayReference.const) != null ? _a : []);
|
|
354
|
+
} else {
|
|
355
|
+
arrayElements.push(this.tsLiteralToType(element));
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
return {
|
|
359
|
+
type: "array",
|
|
360
|
+
elementType: { type: "any" },
|
|
361
|
+
const: arrayElements
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
if (ts__default["default"].isObjectLiteralExpression(node)) {
|
|
365
|
+
const ret = {
|
|
366
|
+
type: "object",
|
|
367
|
+
properties: {},
|
|
368
|
+
additionalProperties: false
|
|
369
|
+
};
|
|
370
|
+
node.properties.forEach((property) => {
|
|
371
|
+
var _a;
|
|
372
|
+
if (ts__default["default"].isPropertyAssignment(property)) {
|
|
373
|
+
const propertyName = (_a = property.name) == null ? void 0 : _a.getText();
|
|
374
|
+
ret.properties[propertyName] = {
|
|
375
|
+
required: true,
|
|
376
|
+
node: this.tsLiteralToType(property.initializer)
|
|
306
377
|
};
|
|
378
|
+
} else if (ts__default["default"].isSpreadAssignment(property)) {
|
|
379
|
+
const spreadValue = this.resolveLiteralReference(property.expression);
|
|
380
|
+
ret.properties = __spreadValues$1(__spreadValues$1({}, ret.properties), spreadValue.properties);
|
|
307
381
|
}
|
|
382
|
+
});
|
|
383
|
+
return ret;
|
|
384
|
+
}
|
|
385
|
+
if (ts__default["default"].isIdentifier(node)) {
|
|
386
|
+
return this.resolveLiteralReference(node);
|
|
387
|
+
}
|
|
388
|
+
this.context.throwError(`Literal type not understood ${ts__default["default"].SyntaxKind[node.kind]} at ${node.getText()}`);
|
|
389
|
+
}
|
|
390
|
+
resolveLiteralReference(expression) {
|
|
391
|
+
var _a, _b;
|
|
392
|
+
if (ts__default["default"].isIdentifier(expression)) {
|
|
393
|
+
const symbol = this.context.typeChecker.getSymbolAtLocation(expression);
|
|
394
|
+
let expressionReference = (_a = symbol == null ? void 0 : symbol.declarations) == null ? void 0 : _a[0];
|
|
395
|
+
if (symbol && expressionReference && ts__default["default"].isImportSpecifier(expressionReference)) {
|
|
396
|
+
const referencedDeclaration = this.context.typeChecker.getAliasedSymbol(symbol);
|
|
397
|
+
expressionReference = (_b = referencedDeclaration.declarations) == null ? void 0 : _b[0];
|
|
308
398
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
399
|
+
if (expressionReference && ts__default["default"].isVariableDeclaration(expressionReference) && expressionReference.initializer) {
|
|
400
|
+
return this.convertVariable(expressionReference.parent.parent);
|
|
401
|
+
}
|
|
402
|
+
this.context.throwError(`Error: Can't resolve non-variable declaration ${expressionReference == null ? void 0 : expressionReference.getText()}`);
|
|
403
|
+
}
|
|
404
|
+
this.context.throwError(`Error: Can't resolve non-identifier reference in literal ${expression.getText()}`);
|
|
405
|
+
}
|
|
406
|
+
resolveFunctionCall(functionCall, document) {
|
|
407
|
+
var _a;
|
|
408
|
+
if (ts__default["default"].isArrowFunction(functionCall)) {
|
|
409
|
+
const declaredReturnType = functionCall.parent.type;
|
|
410
|
+
if (declaredReturnType) {
|
|
411
|
+
return this.tsNodeToType(declaredReturnType);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const functionReturnType = this.context.typeChecker.getTypeAtLocation(functionCall);
|
|
415
|
+
const syntheticNode = this.context.typeChecker.typeToTypeNode(functionReturnType, document, void 0);
|
|
416
|
+
if (syntheticNode) {
|
|
417
|
+
if (ts__default["default"].isTypeReferenceNode(syntheticNode) && ts__default["default"].isIdentifier(syntheticNode.typeName)) {
|
|
418
|
+
const { typeName } = syntheticNode;
|
|
419
|
+
if (this.context.customPrimitives.includes(typeName.text)) {
|
|
420
|
+
return this.makeBasicRefNode(syntheticNode);
|
|
421
|
+
}
|
|
422
|
+
const declarationSymbol = typeName.symbol;
|
|
423
|
+
if (declarationSymbol && ((_a = declarationSymbol.declarations) == null ? void 0 : _a[0])) {
|
|
424
|
+
const declaration = declarationSymbol.declarations[0];
|
|
425
|
+
if (ts__default["default"].isTypeAliasDeclaration(declaration) || ts__default["default"].isInterfaceDeclaration(declaration)) {
|
|
426
|
+
return this.convertDeclaration(declaration);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
this.context.throwError(`Error: could not get referenced type ${syntheticNode.getText()}`);
|
|
430
|
+
}
|
|
431
|
+
return this.tsNodeToType(syntheticNode);
|
|
312
432
|
}
|
|
433
|
+
this.context.throwError(`Error: could not determine effective return type of ${functionCall.getText()}`);
|
|
313
434
|
}
|
|
314
|
-
|
|
435
|
+
tsObjectMembersToProperties(node) {
|
|
315
436
|
const ret = {
|
|
316
437
|
properties: {},
|
|
317
438
|
additionalProperties: false
|
|
@@ -334,7 +455,7 @@ class TsConverter {
|
|
|
334
455
|
});
|
|
335
456
|
return ret;
|
|
336
457
|
}
|
|
337
|
-
|
|
458
|
+
tsTupleToType(node) {
|
|
338
459
|
var _a;
|
|
339
460
|
if (node.elements.length === 0) {
|
|
340
461
|
return { elementTypes: [], additionalItems: false, minItems: 0 };
|
|
@@ -345,11 +466,24 @@ class TsConverter {
|
|
|
345
466
|
node.elements[node.elements.length - 1]
|
|
346
467
|
] : [[...node.elements], void 0];
|
|
347
468
|
const elementTypes = elements.map((element) => {
|
|
348
|
-
|
|
349
|
-
|
|
469
|
+
if (ts__default["default"].isNamedTupleMember(element)) {
|
|
470
|
+
let typeNode;
|
|
471
|
+
if (element.type) {
|
|
472
|
+
typeNode = this.convertTsTypeNode(element.type);
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
name: element.name.text,
|
|
476
|
+
type: typeNode != null ? typeNode : AnyTypeNode,
|
|
477
|
+
optional: element.questionToken ? true : void 0
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
type: this.convertTsTypeNode(xlrUtils.tsStripOptionalType(element)),
|
|
482
|
+
optional: ts__default["default"].isOptionalTypeNode(element)
|
|
483
|
+
};
|
|
350
484
|
});
|
|
351
485
|
const additionalItems = rest ? (_a = this.convertTsTypeNode(rest.type.elementType)) != null ? _a : AnyTypeNode : false;
|
|
352
|
-
const firstOptional =
|
|
486
|
+
const firstOptional = elementTypes.findIndex((element) => element.optional === true);
|
|
353
487
|
const minItems = firstOptional === -1 ? elements.length : firstOptional;
|
|
354
488
|
return __spreadProps(__spreadValues$1({
|
|
355
489
|
elementTypes
|
|
@@ -390,7 +524,7 @@ class TsConverter {
|
|
|
390
524
|
extendsType = typeToApply.extends;
|
|
391
525
|
}
|
|
392
526
|
if (parentInterface.typeParameters && parent.typeArguments) {
|
|
393
|
-
typeToApply = this.
|
|
527
|
+
typeToApply = this.resolveGenerics(typeToApply, parentInterface.typeParameters, parent.typeArguments);
|
|
394
528
|
} else if (xlrUtils.isGenericNodeType(baseObject)) {
|
|
395
529
|
baseObject.genericTokens.push(...(_a = typeToApply.genericTokens) != null ? _a : []);
|
|
396
530
|
}
|
|
@@ -417,7 +551,7 @@ class TsConverter {
|
|
|
417
551
|
additionalProperties
|
|
418
552
|
});
|
|
419
553
|
}
|
|
420
|
-
|
|
554
|
+
resolveGenerics(baseInterface, typeParameters, typeArguments) {
|
|
421
555
|
if (typeArguments && typeArguments.length === 0)
|
|
422
556
|
return baseInterface;
|
|
423
557
|
const genericMap = new Map();
|
|
@@ -504,11 +638,11 @@ class TsConverter {
|
|
|
504
638
|
if (!this.context.customPrimitives.includes(refName)) {
|
|
505
639
|
const typeInfo = xlrUtils.getReferencedType(node, this.context.typeChecker);
|
|
506
640
|
if (typeInfo) {
|
|
507
|
-
const genericType = this.
|
|
641
|
+
const genericType = this.convertTopLevelNode(typeInfo.declaration);
|
|
508
642
|
const genericParams = typeInfo.declaration.typeParameters;
|
|
509
643
|
const genericArgs = node.typeArguments;
|
|
510
644
|
if (genericType && genericParams && genericArgs) {
|
|
511
|
-
return this.
|
|
645
|
+
return this.resolveGenerics(genericType, genericParams, genericArgs);
|
|
512
646
|
}
|
|
513
647
|
if (genericType) {
|
|
514
648
|
return genericType;
|
|
@@ -540,7 +674,7 @@ class TsConverter {
|
|
|
540
674
|
if (node.typeArguments) {
|
|
541
675
|
node.typeArguments.forEach((typeArg) => {
|
|
542
676
|
let convertedNode;
|
|
543
|
-
if (xlrUtils.
|
|
677
|
+
if (xlrUtils.isTopLevelDeclaration(typeArg)) {
|
|
544
678
|
convertedNode = this.convertDeclaration(typeArg);
|
|
545
679
|
} else {
|
|
546
680
|
convertedNode = this.convertTsTypeNode(typeArg);
|
|
@@ -552,9 +686,15 @@ class TsConverter {
|
|
|
552
686
|
}
|
|
553
687
|
});
|
|
554
688
|
}
|
|
689
|
+
let ref;
|
|
690
|
+
if (node.pos === -1 && ts__default["default"].isTypeReferenceNode(node) && ts__default["default"].isIdentifier(node.typeName)) {
|
|
691
|
+
ref = node.typeName.text;
|
|
692
|
+
} else {
|
|
693
|
+
ref = node.getText();
|
|
694
|
+
}
|
|
555
695
|
return __spreadProps(__spreadValues$1({
|
|
556
696
|
type: "ref",
|
|
557
|
-
ref
|
|
697
|
+
ref
|
|
558
698
|
}, xlrUtils.decorateNode(node)), {
|
|
559
699
|
genericArguments: genericArgs.length > 0 ? genericArgs : void 0
|
|
560
700
|
});
|
|
@@ -605,10 +745,6 @@ class TSWriter {
|
|
|
605
745
|
convertNamedTypeNode(type) {
|
|
606
746
|
const typeName = type.name;
|
|
607
747
|
const tsNode = this.convertTypeNode(type);
|
|
608
|
-
let generics;
|
|
609
|
-
if (xlrUtils.isGenericNamedType(type)) {
|
|
610
|
-
generics = this.createTypeParameters(type);
|
|
611
|
-
}
|
|
612
748
|
let customPrimitiveHeritageClass;
|
|
613
749
|
if (type.type === "object" && type.extends) {
|
|
614
750
|
const refName = type.extends.ref.split("<")[0];
|
|
@@ -618,6 +754,15 @@ class TSWriter {
|
|
|
618
754
|
])
|
|
619
755
|
];
|
|
620
756
|
}
|
|
757
|
+
let generics;
|
|
758
|
+
if (xlrUtils.isGenericNamedType(type)) {
|
|
759
|
+
generics = this.createTypeParameters(type);
|
|
760
|
+
type.genericTokens.forEach((token) => {
|
|
761
|
+
if (this.importSet.has(token.symbol)) {
|
|
762
|
+
this.importSet.delete(token.symbol);
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
}
|
|
621
766
|
let finalNode;
|
|
622
767
|
if (ts__default["default"].isTypeLiteralNode(tsNode)) {
|
|
623
768
|
finalNode = this.makeInterfaceDeclaration(typeName, tsNode.members, generics, customPrimitiveHeritageClass);
|
|
@@ -641,6 +786,9 @@ class TSWriter {
|
|
|
641
786
|
}));
|
|
642
787
|
}
|
|
643
788
|
if (type.type === "array") {
|
|
789
|
+
if (type.const) {
|
|
790
|
+
return this.context.factory.createTupleTypeNode(type.const.map((element) => this.convertTypeNode(element)));
|
|
791
|
+
}
|
|
644
792
|
return this.context.factory.createTypeReferenceNode(this.context.factory.createIdentifier("Array"), [this.convertTypeNode(type.elementType)]);
|
|
645
793
|
}
|
|
646
794
|
if (xlrUtils.isPrimitiveTypeNode(type)) {
|
|
@@ -667,6 +815,7 @@ class TSWriter {
|
|
|
667
815
|
this.context.throwError(`Unable to convert node type: ${type.type}`);
|
|
668
816
|
}
|
|
669
817
|
createRefNode(xlrNode) {
|
|
818
|
+
const typeArgs = [];
|
|
670
819
|
if (xlrNode.genericArguments) {
|
|
671
820
|
xlrNode.genericArguments.forEach((genericArg) => {
|
|
672
821
|
if (genericArg.name) {
|
|
@@ -686,12 +835,14 @@ class TSWriter {
|
|
|
686
835
|
this.additionalTypes.set(type.name, additionalType);
|
|
687
836
|
}
|
|
688
837
|
});
|
|
838
|
+
} else {
|
|
839
|
+
typeArgs.push(this.convertTypeNode(genericArg));
|
|
689
840
|
}
|
|
690
841
|
});
|
|
691
842
|
}
|
|
692
843
|
const importName = xlrNode.ref.split("<")[0];
|
|
693
844
|
this.importSet.add(importName);
|
|
694
|
-
return this.context.factory.createTypeReferenceNode(
|
|
845
|
+
return this.context.factory.createTypeReferenceNode(importName, typeArgs);
|
|
695
846
|
}
|
|
696
847
|
createPrimitiveNode(xlrNode) {
|
|
697
848
|
if ((xlrNode.type === "string" || xlrNode.type === "boolean" || xlrNode.type === "number") && xlrNode.const || xlrNode.type === "null") {
|
|
@@ -712,6 +863,8 @@ class TSWriter {
|
|
|
712
863
|
return this.context.factory.createKeywordTypeNode(ts__default["default"].SyntaxKind.NeverKeyword);
|
|
713
864
|
case "undefined":
|
|
714
865
|
return this.context.factory.createKeywordTypeNode(ts__default["default"].SyntaxKind.UndefinedKeyword);
|
|
866
|
+
case "void":
|
|
867
|
+
return this.context.factory.createKeywordTypeNode(ts__default["default"].SyntaxKind.VoidKeyword);
|
|
715
868
|
default:
|
|
716
869
|
this.context.throwError(`Unknown primitive type ${xlrNode.type}`);
|
|
717
870
|
}
|
|
@@ -732,7 +885,12 @@ class TSWriter {
|
|
|
732
885
|
this.context.throwError(`Can't make literal out of type ${xlrNode.type}`);
|
|
733
886
|
}
|
|
734
887
|
createTupleNode(xlrNode) {
|
|
735
|
-
return this.context.factory.createTupleTypeNode(xlrNode.elementTypes.map((e) =>
|
|
888
|
+
return this.context.factory.createTupleTypeNode(xlrNode.elementTypes.map((e) => {
|
|
889
|
+
if (e.name) {
|
|
890
|
+
return this.context.factory.createNamedTupleMember(void 0, this.context.factory.createIdentifier(e.name), e.optional ? this.context.factory.createToken(ts__default["default"].SyntaxKind.QuestionToken) : void 0, this.convertTypeNode(e.type));
|
|
891
|
+
}
|
|
892
|
+
return this.convertTypeNode(e.type);
|
|
893
|
+
}));
|
|
736
894
|
}
|
|
737
895
|
createFunctionDeclarationNode(xlrNode) {
|
|
738
896
|
return this.context.factory.createFunctionTypeNode(void 0, xlrNode.parameters.map((e) => {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
-
import { NodeType, NamedType, ObjectType,
|
|
3
|
-
import { TopLevelDeclaration } from '@player-tools/xlr-utils';
|
|
2
|
+
import { NodeType, NamedType, NamedTypeWithGenerics, ObjectType, NodeTypeWithGenerics } from '@player-tools/xlr';
|
|
3
|
+
import { TopLevelNode, TopLevelDeclaration } from '@player-tools/xlr-utils';
|
|
4
4
|
|
|
5
5
|
interface TSConverterContext {
|
|
6
6
|
/** */
|
|
@@ -29,15 +29,21 @@ declare class TsConverter {
|
|
|
29
29
|
};
|
|
30
30
|
convertedTypes: string[];
|
|
31
31
|
};
|
|
32
|
+
convertTopLevelNode(node: TopLevelNode): NamedType | NamedTypeWithGenerics;
|
|
32
33
|
/** Converts a single type/interface declaration to XLRs */
|
|
33
|
-
convertDeclaration(node: TopLevelDeclaration):
|
|
34
|
+
convertDeclaration(node: TopLevelDeclaration): ObjectType | NodeTypeWithGenerics<ObjectType>;
|
|
35
|
+
convertVariable(node: ts.VariableStatement): NodeType;
|
|
34
36
|
/** Converts an arbitrary ts.TypeNode to XLRs */
|
|
35
|
-
convertTsTypeNode(node: ts.TypeNode): NodeType
|
|
37
|
+
convertTsTypeNode(node: ts.TypeNode): NodeType;
|
|
38
|
+
/** Should not be called directly unless you want to bypass the cache, use `convertTsTypeNode` */
|
|
36
39
|
private tsNodeToType;
|
|
37
|
-
private
|
|
38
|
-
private
|
|
40
|
+
private tsLiteralToType;
|
|
41
|
+
private resolveLiteralReference;
|
|
42
|
+
private resolveFunctionCall;
|
|
43
|
+
private tsObjectMembersToProperties;
|
|
44
|
+
private tsTupleToType;
|
|
39
45
|
private handleHeritageClauses;
|
|
40
|
-
private
|
|
46
|
+
private resolveGenerics;
|
|
41
47
|
private generateGenerics;
|
|
42
48
|
private resolveRefNode;
|
|
43
49
|
private makeMappedType;
|