@sdk-it/core 0.46.10 → 0.47.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/README.md +22 -12
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/derived-type.d.ts +3 -0
- package/dist/lib/derived-type.d.ts.map +1 -0
- package/dist/lib/derived-type.js +7 -0
- package/dist/lib/derived-type.js.map +7 -0
- package/dist/lib/deriver.d.ts +13 -9
- package/dist/lib/deriver.d.ts.map +1 -1
- package/dist/lib/deriver.js +193 -243
- package/dist/lib/deriver.js.map +3 -3
- package/dist/lib/file-system.d.ts +0 -8
- package/dist/lib/file-system.d.ts.map +1 -1
- package/dist/lib/file-system.js +8 -39
- package/dist/lib/file-system.js.map +2 -2
- package/dist/lib/json-pointer.d.ts +13 -0
- package/dist/lib/json-pointer.d.ts.map +1 -0
- package/dist/lib/json-pointer.js +20 -0
- package/dist/lib/json-pointer.js.map +7 -0
- package/dist/lib/paths.d.ts +27 -8
- package/dist/lib/paths.d.ts.map +1 -1
- package/dist/lib/paths.js +165 -96
- package/dist/lib/paths.js.map +3 -3
- package/dist/lib/program.d.ts +19 -6
- package/dist/lib/program.d.ts.map +1 -1
- package/dist/lib/program.js +61 -62
- package/dist/lib/program.js.map +2 -2
- package/dist/lib/ref.d.ts +5 -5
- package/dist/lib/ref.d.ts.map +1 -1
- package/dist/lib/ref.js +28 -8
- package/dist/lib/ref.js.map +2 -2
- package/dist/lib/security.d.ts.map +1 -1
- package/dist/lib/utils.d.ts +9 -6
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +1 -1
- package/dist/lib/utils.js.map +2 -2
- package/dist/lib/zod-jsonschema.d.ts.map +1 -1
- package/dist/lib/zod-jsonschema.js +111 -93
- package/dist/lib/zod-jsonschema.js.map +2 -2
- package/package.json +17 -12
- package/dist/lib/spec-json.d.ts +0 -23
- package/dist/lib/spec-json.d.ts.map +0 -1
- package/dist/lib/spec-json.js +0 -11
- package/dist/lib/spec-json.js.map +0 -7
package/dist/lib/deriver.js
CHANGED
|
@@ -1,7 +1,46 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
NodeFlags,
|
|
3
|
+
SyntaxKind
|
|
4
|
+
} from "@typescript/native/ast";
|
|
5
|
+
import {
|
|
6
|
+
isArrayLiteralExpression,
|
|
7
|
+
isAsExpression,
|
|
8
|
+
isAwaitExpression,
|
|
9
|
+
isBinaryExpression,
|
|
10
|
+
isCallExpression,
|
|
11
|
+
isClassDeclaration,
|
|
12
|
+
isConditionalExpression,
|
|
13
|
+
isExpression,
|
|
14
|
+
isIdentifier,
|
|
15
|
+
isNewExpression,
|
|
16
|
+
isNoSubstitutionTemplateLiteral,
|
|
17
|
+
isNonNullExpression,
|
|
18
|
+
isNumericLiteral,
|
|
19
|
+
isObjectLiteralExpression,
|
|
20
|
+
isParenthesizedExpression,
|
|
21
|
+
isPropertyAccessExpression,
|
|
22
|
+
isPropertyAssignment,
|
|
23
|
+
isPropertyDeclaration,
|
|
24
|
+
isSatisfiesExpression,
|
|
25
|
+
isShorthandPropertyAssignment,
|
|
26
|
+
isSpreadElement,
|
|
27
|
+
isStringLiteral,
|
|
28
|
+
isTaggedTemplateExpression,
|
|
29
|
+
isTemplateExpression,
|
|
30
|
+
isTypeAssertion,
|
|
31
|
+
isTypeReferenceNode,
|
|
32
|
+
isVariableDeclaration,
|
|
33
|
+
isVariableDeclarationList
|
|
34
|
+
} from "@typescript/native/ast/is";
|
|
35
|
+
import {
|
|
36
|
+
IndexKind,
|
|
37
|
+
ObjectFlags,
|
|
38
|
+
SymbolFlags,
|
|
39
|
+
TypeFlags,
|
|
40
|
+
TypeFormatFlags
|
|
41
|
+
} from "@typescript/native/sync";
|
|
42
|
+
import { $types, deriveSymbol } from "./derived-type.js";
|
|
2
43
|
import { sortObjectKeys } from "./utils.js";
|
|
3
|
-
const deriveSymbol = Symbol.for("serialize");
|
|
4
|
-
const $types = Symbol.for("types");
|
|
5
44
|
const defaultTypesMap = {
|
|
6
45
|
Readable: "any",
|
|
7
46
|
ReadableStream: "any",
|
|
@@ -11,6 +50,9 @@ const defaultTypesMap = {
|
|
|
11
50
|
Int8ArrayConstructor: "any",
|
|
12
51
|
Uint8Array: "any"
|
|
13
52
|
};
|
|
53
|
+
function declarationOf(symbol) {
|
|
54
|
+
return (symbol?.valueDeclaration ?? symbol?.declarations[0])?.resolve();
|
|
55
|
+
}
|
|
14
56
|
function nodeLocation(node) {
|
|
15
57
|
try {
|
|
16
58
|
const sourceFile = node.getSourceFile();
|
|
@@ -25,17 +67,19 @@ function nodeLocation(node) {
|
|
|
25
67
|
}
|
|
26
68
|
function skipParentheses(node) {
|
|
27
69
|
let expression = node;
|
|
28
|
-
while (
|
|
70
|
+
while (isParenthesizedExpression(expression)) {
|
|
29
71
|
expression = expression.expression;
|
|
30
72
|
}
|
|
31
73
|
return expression;
|
|
32
74
|
}
|
|
75
|
+
function isConstTypeReference(node) {
|
|
76
|
+
return isTypeReferenceNode(node) && isIdentifier(node.typeName) && node.typeName.text === "const" && !node.typeArguments;
|
|
77
|
+
}
|
|
33
78
|
class TypeDeriver {
|
|
34
79
|
collector = {};
|
|
35
80
|
checker;
|
|
36
81
|
typesMap;
|
|
37
82
|
trace = {};
|
|
38
|
-
currentNode;
|
|
39
83
|
activeAliases = /* @__PURE__ */ new Map();
|
|
40
84
|
constructor(checker, typeMappings = defaultTypesMap) {
|
|
41
85
|
this.checker = checker;
|
|
@@ -44,10 +88,22 @@ class TypeDeriver {
|
|
|
44
88
|
setTrace(ctx) {
|
|
45
89
|
this.trace = { ...ctx };
|
|
46
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* A function or class value: JSON.stringify drops it as a member and writes
|
|
93
|
+
* nothing for it alone. An optional one (`fn?: () => void`) is a union with
|
|
94
|
+
* undefined and is dropped the same way.
|
|
95
|
+
*/
|
|
96
|
+
hasNoJsonForm(type) {
|
|
97
|
+
if (type.isUnionType()) {
|
|
98
|
+
return type.getTypes().every(
|
|
99
|
+
(member) => (member.flags & TypeFlags.Undefined) !== 0 || this.hasNoJsonForm(member)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return type.getCallSignatures().length > 0 || type.getConstructSignatures().length > 0;
|
|
103
|
+
}
|
|
47
104
|
warn(message, node) {
|
|
48
105
|
const parts = [`\x1B[33m\u26A0 ${message}\x1B[0m`];
|
|
49
|
-
const
|
|
50
|
-
const location = resolvedNode ? nodeLocation(resolvedNode) : void 0;
|
|
106
|
+
const location = node ? nodeLocation(node) : void 0;
|
|
51
107
|
if (location) {
|
|
52
108
|
parts.push(` at \x1B[36m${location}\x1B[0m`);
|
|
53
109
|
} else if (this.trace.file) {
|
|
@@ -62,7 +118,7 @@ class TypeDeriver {
|
|
|
62
118
|
return this.serialize(type, "none");
|
|
63
119
|
}
|
|
64
120
|
serialize(type, widening) {
|
|
65
|
-
const alias = type.
|
|
121
|
+
const alias = type.getAliasSymbol()?.name;
|
|
66
122
|
if (!alias) {
|
|
67
123
|
return this.serializeResolved(type, widening);
|
|
68
124
|
}
|
|
@@ -86,16 +142,17 @@ class TypeDeriver {
|
|
|
86
142
|
}
|
|
87
143
|
}
|
|
88
144
|
serializeResolved(type, widening) {
|
|
89
|
-
|
|
145
|
+
const symbol = type.getSymbol();
|
|
146
|
+
const typeName = type.getAliasSymbol()?.name || symbol?.name;
|
|
147
|
+
const mappedType = typeName ? this.typesMap[typeName] : void 0;
|
|
148
|
+
if (mappedType) {
|
|
90
149
|
return {
|
|
91
150
|
[deriveSymbol]: true,
|
|
92
151
|
optional: false,
|
|
93
|
-
[$types]: [
|
|
94
|
-
this.typesMap[type.aliasSymbol?.getName() || type.symbol?.getName()]
|
|
95
|
-
]
|
|
152
|
+
[$types]: [mappedType]
|
|
96
153
|
};
|
|
97
154
|
}
|
|
98
|
-
if (
|
|
155
|
+
if (symbol?.name === "Date") {
|
|
99
156
|
return {
|
|
100
157
|
[deriveSymbol]: true,
|
|
101
158
|
kind: "date",
|
|
@@ -112,14 +169,7 @@ class TypeDeriver {
|
|
|
112
169
|
[$types]: [this.serialize(indexType, widening)]
|
|
113
170
|
};
|
|
114
171
|
}
|
|
115
|
-
if (type.flags & TypeFlags.Any) {
|
|
116
|
-
return {
|
|
117
|
-
[deriveSymbol]: true,
|
|
118
|
-
optional: false,
|
|
119
|
-
[$types]: []
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
if (type.flags & TypeFlags.Unknown) {
|
|
172
|
+
if (type.flags & (TypeFlags.Any | TypeFlags.Unknown)) {
|
|
123
173
|
return {
|
|
124
174
|
[deriveSymbol]: true,
|
|
125
175
|
optional: false,
|
|
@@ -134,7 +184,7 @@ class TypeDeriver {
|
|
|
134
184
|
[$types]: []
|
|
135
185
|
};
|
|
136
186
|
}
|
|
137
|
-
if (type.
|
|
187
|
+
if (type.isStringLiteralType()) {
|
|
138
188
|
return {
|
|
139
189
|
[deriveSymbol]: true,
|
|
140
190
|
optional: false,
|
|
@@ -142,7 +192,7 @@ class TypeDeriver {
|
|
|
142
192
|
[$types]: ["string"]
|
|
143
193
|
};
|
|
144
194
|
}
|
|
145
|
-
if (type.
|
|
195
|
+
if (type.isNumberLiteralType()) {
|
|
146
196
|
return {
|
|
147
197
|
[deriveSymbol]: true,
|
|
148
198
|
optional: false,
|
|
@@ -150,25 +200,15 @@ class TypeDeriver {
|
|
|
150
200
|
[$types]: ["number"]
|
|
151
201
|
};
|
|
152
202
|
}
|
|
153
|
-
if (type.
|
|
203
|
+
if (type.isBooleanLiteralType()) {
|
|
154
204
|
return {
|
|
155
205
|
[deriveSymbol]: true,
|
|
156
206
|
optional: false,
|
|
157
|
-
...widening === "all" ? {} : {
|
|
158
|
-
kind: "literal",
|
|
159
|
-
value: this.checker.typeToString(type) === "true"
|
|
160
|
-
},
|
|
207
|
+
...widening === "all" ? {} : { kind: "literal", value: type.value },
|
|
161
208
|
[$types]: ["boolean"]
|
|
162
209
|
};
|
|
163
210
|
}
|
|
164
|
-
if (type.flags & TypeFlags.TemplateLiteral) {
|
|
165
|
-
return {
|
|
166
|
-
[deriveSymbol]: true,
|
|
167
|
-
optional: false,
|
|
168
|
-
[$types]: ["string"]
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
if (type.flags & TypeFlags.String) {
|
|
211
|
+
if (type.flags & (TypeFlags.TemplateLiteral | TypeFlags.String)) {
|
|
172
212
|
return {
|
|
173
213
|
[deriveSymbol]: true,
|
|
174
214
|
optional: false,
|
|
@@ -182,7 +222,7 @@ class TypeDeriver {
|
|
|
182
222
|
[$types]: ["number"]
|
|
183
223
|
};
|
|
184
224
|
}
|
|
185
|
-
if (type.flags &
|
|
225
|
+
if (type.flags & TypeFlags.Boolean) {
|
|
186
226
|
return {
|
|
187
227
|
[deriveSymbol]: true,
|
|
188
228
|
optional: false,
|
|
@@ -196,12 +236,12 @@ class TypeDeriver {
|
|
|
196
236
|
[$types]: ["null"]
|
|
197
237
|
};
|
|
198
238
|
}
|
|
199
|
-
if (type.
|
|
239
|
+
if (type.isIntersectionType()) {
|
|
200
240
|
let optional;
|
|
201
241
|
const types = [];
|
|
202
|
-
for (const intersectionType of type.
|
|
242
|
+
for (const intersectionType of type.getTypes()) {
|
|
203
243
|
if (optional === void 0) {
|
|
204
|
-
optional = (intersectionType.flags &
|
|
244
|
+
optional = (intersectionType.flags & TypeFlags.Undefined) !== 0;
|
|
205
245
|
if (optional) {
|
|
206
246
|
continue;
|
|
207
247
|
}
|
|
@@ -215,12 +255,12 @@ class TypeDeriver {
|
|
|
215
255
|
[$types]: types
|
|
216
256
|
};
|
|
217
257
|
}
|
|
218
|
-
if (type.
|
|
258
|
+
if (type.isUnionType()) {
|
|
219
259
|
let optional;
|
|
220
260
|
const types = [];
|
|
221
|
-
for (const unionType of type.
|
|
261
|
+
for (const unionType of type.getTypes()) {
|
|
222
262
|
if (optional === void 0) {
|
|
223
|
-
optional = (unionType.flags &
|
|
263
|
+
optional = (unionType.flags & TypeFlags.Undefined) !== 0;
|
|
224
264
|
if (optional) {
|
|
225
265
|
continue;
|
|
226
266
|
}
|
|
@@ -237,11 +277,14 @@ class TypeDeriver {
|
|
|
237
277
|
if (this.checker.isArrayLikeType(type)) {
|
|
238
278
|
const elementType = this.checker.getIndexTypeOfType(
|
|
239
279
|
type,
|
|
240
|
-
|
|
280
|
+
IndexKind.Number
|
|
241
281
|
);
|
|
242
282
|
if (!elementType) {
|
|
243
|
-
const
|
|
244
|
-
this.warn(
|
|
283
|
+
const typeName2 = symbol?.name || "<unknown>";
|
|
284
|
+
this.warn(
|
|
285
|
+
`Could not find element type for array type ${typeName2}`,
|
|
286
|
+
declarationOf(symbol)
|
|
287
|
+
);
|
|
245
288
|
return {
|
|
246
289
|
[deriveSymbol]: true,
|
|
247
290
|
optional: false,
|
|
@@ -249,7 +292,8 @@ class TypeDeriver {
|
|
|
249
292
|
[$types]: ["any"]
|
|
250
293
|
};
|
|
251
294
|
}
|
|
252
|
-
const
|
|
295
|
+
const elementTypeName = elementType.getAliasSymbol()?.name || elementType.getSymbol()?.name;
|
|
296
|
+
const mappedElementType = elementTypeName ? this.typesMap[elementTypeName] : void 0;
|
|
253
297
|
const itemWidening = widening === "all" || this.checker.isTupleType(type) ? "all" : "values";
|
|
254
298
|
return {
|
|
255
299
|
kind: "array",
|
|
@@ -258,26 +302,35 @@ class TypeDeriver {
|
|
|
258
302
|
[$types]: mappedElementType ? [mappedElementType] : [this.serialize(elementType, itemWidening)]
|
|
259
303
|
};
|
|
260
304
|
}
|
|
261
|
-
if (
|
|
262
|
-
|
|
305
|
+
if (this.hasNoJsonForm(type)) {
|
|
306
|
+
return {
|
|
307
|
+
[deriveSymbol]: true,
|
|
308
|
+
kind: "never",
|
|
309
|
+
optional: true,
|
|
310
|
+
[$types]: []
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (type.isObjectType() && type.objectFlags & ObjectFlags.Class) {
|
|
314
|
+
const declaration = symbol?.valueDeclaration?.resolve();
|
|
263
315
|
if (!declaration) {
|
|
264
316
|
return {
|
|
265
317
|
[deriveSymbol]: true,
|
|
266
318
|
optional: false,
|
|
267
|
-
[$types]: [
|
|
319
|
+
[$types]: [symbol?.name]
|
|
268
320
|
};
|
|
269
321
|
}
|
|
270
322
|
return this.serializeNode(declaration);
|
|
271
323
|
}
|
|
272
|
-
if (
|
|
273
|
-
const name =
|
|
324
|
+
if (symbol && symbol.flags & SymbolFlags.Interface) {
|
|
325
|
+
const name = symbol.name;
|
|
274
326
|
if (!this.collector[name]) {
|
|
275
327
|
this.collector[name] = {};
|
|
276
328
|
const properties = {};
|
|
277
329
|
for (const property of this.checker.getPropertiesOfType(type)) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
330
|
+
const propertyType = this.checker.getTypeOfSymbol(property);
|
|
331
|
+
if (!this.hasNoJsonForm(propertyType)) {
|
|
332
|
+
properties[property.name] = this.serializeType(propertyType);
|
|
333
|
+
}
|
|
281
334
|
}
|
|
282
335
|
this.collector[name] = sortObjectKeys(properties);
|
|
283
336
|
}
|
|
@@ -288,24 +341,21 @@ class TypeDeriver {
|
|
|
288
341
|
};
|
|
289
342
|
}
|
|
290
343
|
if (type.flags & TypeFlags.Object) {
|
|
291
|
-
if (this.typesMap[
|
|
344
|
+
if (symbol && this.typesMap[symbol.name]) {
|
|
292
345
|
return {
|
|
293
346
|
[deriveSymbol]: true,
|
|
294
347
|
optional: false,
|
|
295
|
-
[$types]: [this.typesMap[
|
|
348
|
+
[$types]: [this.typesMap[symbol.name]]
|
|
296
349
|
};
|
|
297
350
|
}
|
|
298
|
-
const properties = this.checker.getPropertiesOfType(type)
|
|
351
|
+
const properties = this.checker.getPropertiesOfType(type).filter(
|
|
352
|
+
(prop) => !this.hasNoJsonForm(this.checker.getTypeOfSymbol(prop))
|
|
353
|
+
);
|
|
299
354
|
if (properties.length > 0) {
|
|
300
355
|
const serializedProps = {};
|
|
301
356
|
for (const prop of properties) {
|
|
302
|
-
const
|
|
303
|
-
const
|
|
304
|
-
(it) => ts.isPropertyAssignment(it)
|
|
305
|
-
);
|
|
306
|
-
const shorthand = declarations.find(
|
|
307
|
-
(it) => ts.isShorthandPropertyAssignment(it)
|
|
308
|
-
);
|
|
357
|
+
const propAssingment = prop.declarations.find(isPropertyAssignment.Handle)?.resolve();
|
|
358
|
+
const shorthand = prop.declarations.find(isShorthandPropertyAssignment.Handle)?.resolve();
|
|
309
359
|
const shorthandType = shorthand ? this.typeOfShorthand(shorthand) : void 0;
|
|
310
360
|
if (propAssingment) {
|
|
311
361
|
serializedProps[prop.name] = this.serializeValue(
|
|
@@ -329,17 +379,25 @@ class TypeDeriver {
|
|
|
329
379
|
[$types]: [sortObjectKeys(serializedProps)]
|
|
330
380
|
};
|
|
331
381
|
}
|
|
332
|
-
|
|
333
|
-
if (!declaration) {
|
|
382
|
+
if (type.isMappedType()) {
|
|
334
383
|
return {
|
|
335
384
|
[deriveSymbol]: true,
|
|
385
|
+
kind: "record",
|
|
336
386
|
optional: false,
|
|
337
|
-
[$types]: [type.
|
|
387
|
+
[$types]: [this.serialize(type.getTemplateType(), widening)]
|
|
338
388
|
};
|
|
339
389
|
}
|
|
340
|
-
return
|
|
390
|
+
return {
|
|
391
|
+
[deriveSymbol]: true,
|
|
392
|
+
kind: "object",
|
|
393
|
+
optional: false,
|
|
394
|
+
[$types]: [{}]
|
|
395
|
+
};
|
|
341
396
|
}
|
|
342
|
-
this.warn(
|
|
397
|
+
this.warn(
|
|
398
|
+
`Unhandled type: ${type.flags} ${TypeFlags[type.flags]}`,
|
|
399
|
+
declarationOf(symbol)
|
|
400
|
+
);
|
|
343
401
|
return {
|
|
344
402
|
[deriveSymbol]: true,
|
|
345
403
|
optional: false,
|
|
@@ -347,16 +405,15 @@ class TypeDeriver {
|
|
|
347
405
|
this.checker.typeToString(
|
|
348
406
|
type,
|
|
349
407
|
void 0,
|
|
350
|
-
|
|
408
|
+
TypeFormatFlags.NoTruncation
|
|
351
409
|
)
|
|
352
410
|
]
|
|
353
411
|
};
|
|
354
412
|
}
|
|
355
413
|
serializeValue(type, widening) {
|
|
356
|
-
const isLiteral = type.isLiteral() || (type.flags & TypeFlags.BooleanLiteral) !== 0;
|
|
357
414
|
return this.serialize(
|
|
358
415
|
type,
|
|
359
|
-
widening === "values" &&
|
|
416
|
+
widening === "values" && type.isLiteralType() ? "all" : widening
|
|
360
417
|
);
|
|
361
418
|
}
|
|
362
419
|
/**
|
|
@@ -369,10 +426,10 @@ class TypeDeriver {
|
|
|
369
426
|
return this.checker.getTypeFromTypeNode(satisfies.type);
|
|
370
427
|
}
|
|
371
428
|
const expression = skipParentheses(node);
|
|
372
|
-
if (
|
|
429
|
+
if (isIdentifier(expression)) {
|
|
373
430
|
const symbol = this.checker.getSymbolAtLocation(expression);
|
|
374
|
-
const declaration = symbol?.valueDeclaration ?? symbol?.declarations
|
|
375
|
-
if (symbol && !seen.has(symbol) && declaration &&
|
|
431
|
+
const declaration = (symbol?.valueDeclaration ?? symbol?.declarations[0])?.resolve();
|
|
432
|
+
if (symbol && !seen.has(symbol) && declaration && isVariableDeclaration(declaration) && isVariableDeclarationList(declaration.parent) && declaration.parent.flags & NodeFlags.Const) {
|
|
376
433
|
seen.add(symbol);
|
|
377
434
|
const narrowed = this.checker.getTypeAtLocation(expression);
|
|
378
435
|
if (narrowed !== this.checker.getTypeAtLocation(declaration.name)) {
|
|
@@ -386,22 +443,22 @@ class TypeDeriver {
|
|
|
386
443
|
}
|
|
387
444
|
}
|
|
388
445
|
}
|
|
389
|
-
if (
|
|
446
|
+
if (isArrayLiteralExpression(expression) && expression.elements.length === 1 && isSpreadElement(expression.elements[0])) {
|
|
390
447
|
return this.typeOfExpression(expression.elements[0].expression, seen);
|
|
391
448
|
}
|
|
392
449
|
return this.checker.getTypeAtLocation(expression);
|
|
393
450
|
}
|
|
394
451
|
isConstAsserted(node) {
|
|
395
452
|
const expression = skipParentheses(node);
|
|
396
|
-
return (
|
|
453
|
+
return (isAsExpression(expression) || isTypeAssertion(expression)) && isConstTypeReference(expression.type);
|
|
397
454
|
}
|
|
398
455
|
emptyArraySatisfies(node) {
|
|
399
456
|
const expression = skipParentheses(node);
|
|
400
|
-
if (!
|
|
457
|
+
if (!isSatisfiesExpression(expression)) {
|
|
401
458
|
return void 0;
|
|
402
459
|
}
|
|
403
460
|
const operand = skipParentheses(expression.expression);
|
|
404
|
-
if (!
|
|
461
|
+
if (!isArrayLiteralExpression(operand) || operand.elements.length > 0) {
|
|
405
462
|
return void 0;
|
|
406
463
|
}
|
|
407
464
|
const annotation = this.checker.getTypeFromTypeNode(expression.type);
|
|
@@ -409,38 +466,57 @@ class TypeDeriver {
|
|
|
409
466
|
}
|
|
410
467
|
typeOfShorthand(node) {
|
|
411
468
|
const symbol = this.checker.getShorthandAssignmentValueSymbol(node);
|
|
412
|
-
const declaration = symbol?.valueDeclaration ?? symbol?.declarations
|
|
413
|
-
if (!declaration || !
|
|
469
|
+
const declaration = (symbol?.valueDeclaration ?? symbol?.declarations[0])?.resolve();
|
|
470
|
+
if (!declaration || !isVariableDeclaration(declaration) || !declaration.initializer) {
|
|
414
471
|
return void 0;
|
|
415
472
|
}
|
|
416
473
|
return this.emptyArraySatisfies(declaration.initializer) ? this.typeOfExpression(declaration.initializer) : this.checker.getTypeAtLocation(node.name);
|
|
417
474
|
}
|
|
418
475
|
serializeNode(node) {
|
|
419
|
-
|
|
420
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
476
|
+
if (isObjectLiteralExpression(node)) {
|
|
421
477
|
const symbolType = this.checker.getTypeAtLocation(node);
|
|
422
478
|
const props = {};
|
|
479
|
+
const dropped = /* @__PURE__ */ new Set();
|
|
423
480
|
for (const prop of node.properties) {
|
|
424
|
-
if (
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
481
|
+
if (isPropertyAssignment(prop)) {
|
|
482
|
+
const type = this.typeOfExpression(prop.initializer);
|
|
483
|
+
if (this.hasNoJsonForm(type)) {
|
|
484
|
+
dropped.add(prop.name.getText());
|
|
485
|
+
} else {
|
|
486
|
+
props[prop.name.getText()] = this.serializeType(type);
|
|
487
|
+
}
|
|
488
|
+
} else if (isShorthandPropertyAssignment(prop)) {
|
|
429
489
|
const type = this.typeOfShorthand(prop);
|
|
430
490
|
if (type) {
|
|
431
|
-
|
|
491
|
+
if (this.hasNoJsonForm(type)) {
|
|
492
|
+
dropped.add(prop.name.getText());
|
|
493
|
+
} else {
|
|
494
|
+
props[prop.name.getText()] = this.serializeType(type);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
} else if (prop.kind === SyntaxKind.SpreadAssignment) {
|
|
498
|
+
const source = this.serializeType(
|
|
499
|
+
this.typeOfExpression(prop.expression)
|
|
500
|
+
);
|
|
501
|
+
if (source?.kind === "object") {
|
|
502
|
+
for (const [name, value] of Object.entries(source[$types][0])) {
|
|
503
|
+
dropped.delete(name);
|
|
504
|
+
props[name] = value;
|
|
505
|
+
}
|
|
432
506
|
}
|
|
433
507
|
}
|
|
434
508
|
}
|
|
435
509
|
for (const symbol of symbolType.getProperties()) {
|
|
436
|
-
if (!Object.hasOwn(props, symbol.name)) {
|
|
510
|
+
if (!Object.hasOwn(props, symbol.name) && !dropped.has(symbol.name)) {
|
|
437
511
|
const type = this.checker.getTypeOfSymbol(symbol);
|
|
438
|
-
|
|
512
|
+
if (!this.hasNoJsonForm(type)) {
|
|
513
|
+
props[symbol.name] = this.serializeType(type);
|
|
514
|
+
}
|
|
439
515
|
}
|
|
440
516
|
}
|
|
441
517
|
return sortObjectKeys(props);
|
|
442
518
|
}
|
|
443
|
-
if (
|
|
519
|
+
if (isPropertyAccessExpression(node) || isPropertyDeclaration(node)) {
|
|
444
520
|
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
445
521
|
if (!symbol) {
|
|
446
522
|
this.warn(`No symbol found for ${node.name.getText()}`, node);
|
|
@@ -449,50 +525,7 @@ class TypeDeriver {
|
|
|
449
525
|
const type = this.checker.getTypeOfSymbol(symbol);
|
|
450
526
|
return this.serializeType(type);
|
|
451
527
|
}
|
|
452
|
-
if (
|
|
453
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
454
|
-
if (!symbol) {
|
|
455
|
-
this.warn(`No symbol found for ${node.name.getText()}`, node);
|
|
456
|
-
return null;
|
|
457
|
-
}
|
|
458
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
459
|
-
return this.serializeType(type);
|
|
460
|
-
}
|
|
461
|
-
if (ts.isPropertyDeclaration(node)) {
|
|
462
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
463
|
-
if (!symbol) {
|
|
464
|
-
this.warn(`No symbol found for ${node.name.getText()}`, node);
|
|
465
|
-
return null;
|
|
466
|
-
}
|
|
467
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
468
|
-
return this.serializeType(type);
|
|
469
|
-
}
|
|
470
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
471
|
-
if (!node.name?.text) {
|
|
472
|
-
throw new Error("Interface has no name");
|
|
473
|
-
}
|
|
474
|
-
if (this.typesMap[node.name.text]) {
|
|
475
|
-
return {
|
|
476
|
-
[deriveSymbol]: true,
|
|
477
|
-
optional: false,
|
|
478
|
-
[$types]: [this.typesMap[node.name.text]]
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
if (!this.collector[node.name.text]) {
|
|
482
|
-
this.collector[node.name.text] = {};
|
|
483
|
-
const members = {};
|
|
484
|
-
for (const member of node.members.filter(ts.isPropertySignature)) {
|
|
485
|
-
members[member.name.getText()] = this.serializeNode(member);
|
|
486
|
-
}
|
|
487
|
-
this.collector[node.name.text] = sortObjectKeys(members);
|
|
488
|
-
}
|
|
489
|
-
return {
|
|
490
|
-
[deriveSymbol]: true,
|
|
491
|
-
optional: false,
|
|
492
|
-
[$types]: [`#/components/schemas/${node.name.text}`]
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
if (ts.isClassDeclaration(node)) {
|
|
528
|
+
if (isClassDeclaration(node)) {
|
|
496
529
|
if (!node.name?.text) {
|
|
497
530
|
throw new Error("Class has no name");
|
|
498
531
|
}
|
|
@@ -506,7 +539,11 @@ class TypeDeriver {
|
|
|
506
539
|
if (!this.collector[node.name.text]) {
|
|
507
540
|
this.collector[node.name.text] = {};
|
|
508
541
|
const members = {};
|
|
509
|
-
for (const member of node.members.filter(
|
|
542
|
+
for (const member of node.members.filter(isPropertyDeclaration)) {
|
|
543
|
+
const symbol = this.checker.getSymbolAtLocation(member.name);
|
|
544
|
+
if (symbol && this.hasNoJsonForm(this.checker.getTypeOfSymbol(symbol))) {
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
510
547
|
members[member.name.getText()] = this.serializeNode(member);
|
|
511
548
|
}
|
|
512
549
|
this.collector[node.name.text] = sortObjectKeys(members);
|
|
@@ -514,24 +551,10 @@ class TypeDeriver {
|
|
|
514
551
|
return {
|
|
515
552
|
[deriveSymbol]: true,
|
|
516
553
|
optional: false,
|
|
517
|
-
[$types]: [`#/components/schemas/${node.name.text}`]
|
|
518
|
-
$ref: `#/components/schemas/${node.name.text}`
|
|
554
|
+
[$types]: [`#/components/schemas/${node.name.text}`]
|
|
519
555
|
};
|
|
520
556
|
}
|
|
521
|
-
if (
|
|
522
|
-
const symbol = this.checker.getSymbolAtLocation(node.name);
|
|
523
|
-
if (!symbol) {
|
|
524
|
-
this.warn(`No symbol found for ${node.name.getText()}`, node);
|
|
525
|
-
return null;
|
|
526
|
-
}
|
|
527
|
-
if (!node.type) {
|
|
528
|
-
this.warn(`No type found for ${node.name.getText()}`, node);
|
|
529
|
-
return "any";
|
|
530
|
-
}
|
|
531
|
-
const type = this.checker.getTypeFromTypeNode(node.type);
|
|
532
|
-
return this.serializeType(type);
|
|
533
|
-
}
|
|
534
|
-
if (ts.isIdentifier(node)) {
|
|
557
|
+
if (isIdentifier(node)) {
|
|
535
558
|
const symbol = this.checker.getSymbolAtLocation(node);
|
|
536
559
|
if (!symbol) {
|
|
537
560
|
this.warn(`No symbol found for identifier ${node.getText()}`, node);
|
|
@@ -540,49 +563,20 @@ class TypeDeriver {
|
|
|
540
563
|
const type = this.typeOfExpression(node);
|
|
541
564
|
return this.serializeType(type);
|
|
542
565
|
}
|
|
543
|
-
if (
|
|
544
|
-
|
|
545
|
-
return this.serializeType(type);
|
|
546
|
-
}
|
|
547
|
-
if (ts.isCallOrNewExpression(node)) {
|
|
548
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
549
|
-
return this.serializeType(type);
|
|
566
|
+
if (isAwaitExpression(node) || isCallExpression(node) || isNewExpression(node) || isAsExpression(node)) {
|
|
567
|
+
return this.serializeType(this.checker.getTypeAtLocation(node));
|
|
550
568
|
}
|
|
551
|
-
if (
|
|
552
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
553
|
-
return this.serializeType(type);
|
|
554
|
-
}
|
|
555
|
-
if (ts.isSatisfiesExpression(node)) {
|
|
569
|
+
if (isSatisfiesExpression(node)) {
|
|
556
570
|
return this.serializeType(this.typeOfExpression(node));
|
|
557
571
|
}
|
|
558
|
-
if (
|
|
559
|
-
const symbolType = this.checker.getTypeAtLocation(node);
|
|
560
|
-
const props = {};
|
|
561
|
-
for (const symbol of symbolType.getProperties()) {
|
|
562
|
-
const type = this.checker.getTypeOfSymbol(symbol);
|
|
563
|
-
props[symbol.name] = this.serializeType(type);
|
|
564
|
-
}
|
|
565
|
-
return {
|
|
566
|
-
[deriveSymbol]: true,
|
|
567
|
-
optional: false,
|
|
568
|
-
[$types]: [sortObjectKeys(props)]
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
if (node.kind === ts.SyntaxKind.NullKeyword) {
|
|
572
|
+
if (node.kind === SyntaxKind.NullKeyword) {
|
|
572
573
|
return {
|
|
573
574
|
[deriveSymbol]: true,
|
|
574
575
|
optional: false,
|
|
575
576
|
[$types]: ["null"]
|
|
576
577
|
};
|
|
577
578
|
}
|
|
578
|
-
if (node.kind ===
|
|
579
|
-
return {
|
|
580
|
-
[deriveSymbol]: true,
|
|
581
|
-
optional: false,
|
|
582
|
-
[$types]: ["boolean"]
|
|
583
|
-
};
|
|
584
|
-
}
|
|
585
|
-
if (node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
579
|
+
if (node.kind === SyntaxKind.TrueKeyword) {
|
|
586
580
|
return {
|
|
587
581
|
[deriveSymbol]: true,
|
|
588
582
|
optional: false,
|
|
@@ -591,7 +585,7 @@ class TypeDeriver {
|
|
|
591
585
|
[$types]: ["boolean"]
|
|
592
586
|
};
|
|
593
587
|
}
|
|
594
|
-
if (node.kind ===
|
|
588
|
+
if (node.kind === SyntaxKind.FalseKeyword) {
|
|
595
589
|
return {
|
|
596
590
|
[deriveSymbol]: true,
|
|
597
591
|
optional: false,
|
|
@@ -600,69 +594,27 @@ class TypeDeriver {
|
|
|
600
594
|
[$types]: ["boolean"]
|
|
601
595
|
};
|
|
602
596
|
}
|
|
603
|
-
if (
|
|
604
|
-
if (!node.name) {
|
|
605
|
-
this.warn("Function declaration has no name", node);
|
|
606
|
-
return {
|
|
607
|
-
[deriveSymbol]: true,
|
|
608
|
-
optional: false,
|
|
609
|
-
[$types]: ["any"]
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
const functionName = node.name.text;
|
|
613
|
-
if (this.typesMap[functionName]) {
|
|
614
|
-
return {
|
|
615
|
-
[deriveSymbol]: true,
|
|
616
|
-
optional: false,
|
|
617
|
-
[$types]: [this.typesMap[functionName]]
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
if (node.type) {
|
|
621
|
-
const returnType = this.checker.getTypeFromTypeNode(node.type);
|
|
622
|
-
return this.serializeType(returnType);
|
|
623
|
-
}
|
|
624
|
-
return {
|
|
625
|
-
[deriveSymbol]: true,
|
|
626
|
-
optional: false,
|
|
627
|
-
[$types]: ["any"]
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
597
|
+
if (isArrayLiteralExpression(node)) {
|
|
631
598
|
const type = this.typeOfExpression(node);
|
|
632
599
|
return this.serializeType(type);
|
|
633
600
|
}
|
|
634
|
-
if (
|
|
635
|
-
|
|
636
|
-
return this.serializeType(type);
|
|
601
|
+
if (isStringLiteral(node) || isNumericLiteral(node) || isTemplateExpression(node) || isNoSubstitutionTemplateLiteral(node) || isTaggedTemplateExpression(node) || isBinaryExpression(node) || isConditionalExpression(node)) {
|
|
602
|
+
return this.serializeType(this.checker.getTypeAtLocation(node));
|
|
637
603
|
}
|
|
638
|
-
if (
|
|
639
|
-
|
|
640
|
-
return this.serializeType(type);
|
|
641
|
-
}
|
|
642
|
-
if (ts.isTaggedTemplateExpression(node)) {
|
|
643
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
644
|
-
return this.serializeType(type);
|
|
645
|
-
}
|
|
646
|
-
if (ts.isBinaryExpression(node)) {
|
|
647
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
648
|
-
return this.serializeType(type);
|
|
604
|
+
if (isParenthesizedExpression(node)) {
|
|
605
|
+
return this.serializeType(this.typeOfExpression(node));
|
|
649
606
|
}
|
|
650
|
-
if (
|
|
651
|
-
|
|
652
|
-
return this.serializeType(type);
|
|
607
|
+
if (isNonNullExpression(node)) {
|
|
608
|
+
return this.serializeType(this.checker.getTypeAtLocation(node));
|
|
653
609
|
}
|
|
654
|
-
if (
|
|
610
|
+
if (isExpression(node)) {
|
|
655
611
|
return this.serializeType(this.typeOfExpression(node));
|
|
656
612
|
}
|
|
657
|
-
if (ts.isNonNullExpression(node)) {
|
|
658
|
-
const type = this.checker.getTypeAtLocation(node);
|
|
659
|
-
return this.serializeType(type);
|
|
660
|
-
}
|
|
661
613
|
this.warn(
|
|
662
|
-
`Unhandled node: ${
|
|
614
|
+
`Unhandled node: ${SyntaxKind[node.kind]} "${node.getText()}" (type: ${this.checker.typeToString(
|
|
663
615
|
this.checker.getTypeAtLocation(node),
|
|
664
616
|
void 0,
|
|
665
|
-
|
|
617
|
+
TypeFormatFlags.NoTruncation
|
|
666
618
|
)})`,
|
|
667
619
|
node
|
|
668
620
|
);
|
|
@@ -674,10 +626,8 @@ class TypeDeriver {
|
|
|
674
626
|
}
|
|
675
627
|
}
|
|
676
628
|
export {
|
|
677
|
-
$types,
|
|
678
629
|
TypeDeriver,
|
|
679
630
|
defaultTypesMap,
|
|
680
|
-
deriveSymbol,
|
|
681
631
|
nodeLocation
|
|
682
632
|
};
|
|
683
633
|
//# sourceMappingURL=deriver.js.map
|