dtsgenerator 3.18.0 → 3.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +157 -2
- package/README.md +6 -154
- package/dist/core/dtsGenerator.js +4 -5
- package/dist/core/openApiV3.d.ts +2 -2
- package/dist/core/schemaId.js +1 -1
- package/dist/core/type.js +1 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +15 -20
- package/package.json +19 -18
- package/src/cli.ts +3 -3
- package/src/commandOptions.ts +4 -4
- package/src/core/astBuilder.ts +28 -28
- package/src/core/config.ts +1 -1
- package/src/core/dtsGenerator.ts +63 -63
- package/src/core/jsonSchema.ts +37 -37
- package/src/core/openApiV2.ts +2 -2
- package/src/core/openApiV3.ts +1 -8
- package/src/core/referenceResolver.ts +9 -9
- package/src/core/schemaId.ts +4 -1
- package/src/core/type.ts +9 -9
- package/src/core/utils.ts +3 -3
- package/src/core/validateIdentifier.ts +2 -1
- package/src/jsonPointer.ts +1 -1
- package/src/utils.ts +23 -25
package/src/core/astBuilder.ts
CHANGED
|
@@ -17,7 +17,7 @@ function buildPropertyNameIdentifier(name: string): ts.PropertyName {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export function buildKeyword(
|
|
20
|
-
kind: ts.KeywordTypeSyntaxKind
|
|
20
|
+
kind: ts.KeywordTypeSyntaxKind,
|
|
21
21
|
): ts.KeywordTypeNode {
|
|
22
22
|
return ts.factory.createKeywordTypeNode(kind);
|
|
23
23
|
}
|
|
@@ -41,7 +41,7 @@ export function buildSimpleArrayNode(element: ts.TypeNode): ts.ArrayTypeNode {
|
|
|
41
41
|
}
|
|
42
42
|
export function buildNullKeyword(): ts.LiteralTypeNode {
|
|
43
43
|
return ts.factory.createLiteralTypeNode(
|
|
44
|
-
ts.factory.createToken(ts.SyntaxKind.NullKeyword)
|
|
44
|
+
ts.factory.createToken(ts.SyntaxKind.NullKeyword),
|
|
45
45
|
);
|
|
46
46
|
}
|
|
47
47
|
export function buildStringLiteralTypeNode(s: string): ts.LiteralTypeNode {
|
|
@@ -52,14 +52,14 @@ export function buildNumericLiteralTypeNode(n: string): ts.LiteralTypeNode {
|
|
|
52
52
|
}
|
|
53
53
|
export function buildBooleanLiteralTypeNode(b: boolean): ts.LiteralTypeNode {
|
|
54
54
|
return ts.factory.createLiteralTypeNode(
|
|
55
|
-
b ? ts.factory.createTrue() : ts.factory.createFalse()
|
|
55
|
+
b ? ts.factory.createTrue() : ts.factory.createFalse(),
|
|
56
56
|
);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
export function buildNamespaceNode(
|
|
60
60
|
name: string,
|
|
61
61
|
statements: ts.Statement[],
|
|
62
|
-
root: boolean
|
|
62
|
+
root: boolean,
|
|
63
63
|
): ts.ModuleDeclaration {
|
|
64
64
|
const modifiers = root
|
|
65
65
|
? [ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)]
|
|
@@ -70,14 +70,14 @@ export function buildNamespaceNode(
|
|
|
70
70
|
ts.factory.createModuleBlock(statements),
|
|
71
71
|
ts.NodeFlags.Namespace |
|
|
72
72
|
ts.NodeFlags.ExportContext |
|
|
73
|
-
ts.NodeFlags.ContextFlags
|
|
73
|
+
ts.NodeFlags.ContextFlags,
|
|
74
74
|
);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
export function buildInterfaceNode(
|
|
78
78
|
id: SchemaId,
|
|
79
79
|
members: ts.TypeElement[],
|
|
80
|
-
root: boolean
|
|
80
|
+
root: boolean,
|
|
81
81
|
): ts.InterfaceDeclaration {
|
|
82
82
|
const name = getLastTypeName(id);
|
|
83
83
|
const modifiers = root
|
|
@@ -88,14 +88,14 @@ export function buildInterfaceNode(
|
|
|
88
88
|
buildTypeNameIdentifier(name),
|
|
89
89
|
undefined,
|
|
90
90
|
undefined,
|
|
91
|
-
members
|
|
91
|
+
members,
|
|
92
92
|
);
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
export function buildTypeAliasNode(
|
|
96
96
|
id: SchemaId,
|
|
97
97
|
type: ts.TypeNode,
|
|
98
|
-
root: boolean
|
|
98
|
+
root: boolean,
|
|
99
99
|
): ts.TypeAliasDeclaration {
|
|
100
100
|
const name = getLastTypeName(id);
|
|
101
101
|
const modifiers = root
|
|
@@ -105,7 +105,7 @@ export function buildTypeAliasNode(
|
|
|
105
105
|
modifiers,
|
|
106
106
|
buildTypeNameIdentifier(name),
|
|
107
107
|
undefined,
|
|
108
|
-
type
|
|
108
|
+
type,
|
|
109
109
|
);
|
|
110
110
|
}
|
|
111
111
|
|
|
@@ -114,7 +114,7 @@ export function buildPropertySignature(
|
|
|
114
114
|
propertyName: string,
|
|
115
115
|
valueType: ts.TypeNode,
|
|
116
116
|
required: string[] | undefined,
|
|
117
|
-
isPattern: boolean | undefined
|
|
117
|
+
isPattern: boolean | undefined,
|
|
118
118
|
): ts.PropertySignature | ts.IndexSignatureDeclaration {
|
|
119
119
|
const modifiers = undefined;
|
|
120
120
|
const questionToken =
|
|
@@ -131,24 +131,24 @@ export function buildPropertySignature(
|
|
|
131
131
|
ts.factory.createIdentifier('pattern'),
|
|
132
132
|
undefined,
|
|
133
133
|
ts.factory.createTypeReferenceNode('string', []),
|
|
134
|
-
undefined
|
|
134
|
+
undefined,
|
|
135
135
|
),
|
|
136
136
|
],
|
|
137
|
-
valueType
|
|
137
|
+
valueType,
|
|
138
138
|
);
|
|
139
139
|
}
|
|
140
140
|
return ts.factory.createPropertySignature(
|
|
141
141
|
modifiers,
|
|
142
142
|
buildPropertyNameIdentifier(propertyName),
|
|
143
143
|
questionToken,
|
|
144
|
-
valueType
|
|
144
|
+
valueType,
|
|
145
145
|
);
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
export function buildIndexSignatureNode(
|
|
149
149
|
name: string,
|
|
150
150
|
indexType: ts.TypeNode,
|
|
151
|
-
valueType: ts.TypeNode
|
|
151
|
+
valueType: ts.TypeNode,
|
|
152
152
|
): ts.IndexSignatureDeclaration {
|
|
153
153
|
return ts.factory.createIndexSignature(
|
|
154
154
|
undefined,
|
|
@@ -159,10 +159,10 @@ export function buildIndexSignatureNode(
|
|
|
159
159
|
buildTypeNameIdentifier(name),
|
|
160
160
|
undefined,
|
|
161
161
|
indexType,
|
|
162
|
-
undefined
|
|
162
|
+
undefined,
|
|
163
163
|
),
|
|
164
164
|
],
|
|
165
|
-
valueType
|
|
165
|
+
valueType,
|
|
166
166
|
);
|
|
167
167
|
}
|
|
168
168
|
|
|
@@ -179,7 +179,7 @@ export function buildFreeFormObjectTypeLiteralNode(): ts.TypeNode {
|
|
|
179
179
|
export function buildUnionTypeNode<T>(
|
|
180
180
|
types: T[],
|
|
181
181
|
builder: (t: T, index: number) => ts.TypeNode,
|
|
182
|
-
terminate: boolean
|
|
182
|
+
terminate: boolean,
|
|
183
183
|
): ts.TypeNode {
|
|
184
184
|
const node = ts.factory.createUnionTypeNode(types.map(builder));
|
|
185
185
|
if (terminate) {
|
|
@@ -190,7 +190,7 @@ export function buildUnionTypeNode<T>(
|
|
|
190
190
|
|
|
191
191
|
export function buildIntersectionTypeNode(
|
|
192
192
|
types: readonly ts.TypeNode[],
|
|
193
|
-
terminate: boolean
|
|
193
|
+
terminate: boolean,
|
|
194
194
|
): ts.TypeNode {
|
|
195
195
|
const node = ts.factory.createIntersectionTypeNode(types);
|
|
196
196
|
if (terminate) {
|
|
@@ -203,7 +203,7 @@ export function buildTupleTypeNode(
|
|
|
203
203
|
types: ts.TypeNode | ts.TypeNode[],
|
|
204
204
|
minItems?: number,
|
|
205
205
|
maxItems?: number,
|
|
206
|
-
additionalItems?: ts.TypeNode | false
|
|
206
|
+
additionalItems?: ts.TypeNode | false,
|
|
207
207
|
): ts.TypeNode {
|
|
208
208
|
function typesIsArray<T>(types: T | T[]): types is T[] {
|
|
209
209
|
return Array.isArray(types);
|
|
@@ -247,9 +247,9 @@ export function buildTupleTypeNode(
|
|
|
247
247
|
? additionalItems !== undefined
|
|
248
248
|
? (additionalItems as ts.TypeNode)
|
|
249
249
|
: buildAnyKeyword()
|
|
250
|
-
: types
|
|
251
|
-
)
|
|
252
|
-
)
|
|
250
|
+
: types,
|
|
251
|
+
),
|
|
252
|
+
),
|
|
253
253
|
);
|
|
254
254
|
}
|
|
255
255
|
return ts.factory.createTupleTypeNode(nodes);
|
|
@@ -257,7 +257,7 @@ export function buildTupleTypeNode(
|
|
|
257
257
|
|
|
258
258
|
export function buildTypeReferenceNode(
|
|
259
259
|
schema: NormalizedSchema,
|
|
260
|
-
currentSchema: Schema
|
|
260
|
+
currentSchema: Schema,
|
|
261
261
|
): ts.TypeReferenceNode {
|
|
262
262
|
const typeName = getTypename(schema.id, currentSchema);
|
|
263
263
|
let node: ts.EntityName | undefined;
|
|
@@ -267,7 +267,7 @@ export function buildTypeReferenceNode(
|
|
|
267
267
|
} else {
|
|
268
268
|
node = ts.factory.createQualifiedName(
|
|
269
269
|
node,
|
|
270
|
-
buildTypeNameIdentifier(name)
|
|
270
|
+
buildTypeNameIdentifier(name),
|
|
271
271
|
);
|
|
272
272
|
}
|
|
273
273
|
}
|
|
@@ -296,7 +296,7 @@ function getTypename(id: SchemaId, baseSchema: Schema): string[] {
|
|
|
296
296
|
export function addComment<T extends ts.Node>(
|
|
297
297
|
node: T,
|
|
298
298
|
schema: NormalizedSchema,
|
|
299
|
-
terminate: boolean
|
|
299
|
+
terminate: boolean,
|
|
300
300
|
): T {
|
|
301
301
|
const comments = getComment(schema);
|
|
302
302
|
if (comments.length === 0) {
|
|
@@ -306,7 +306,7 @@ export function addComment<T extends ts.Node>(
|
|
|
306
306
|
node,
|
|
307
307
|
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
308
308
|
` ${comments[0] ?? ''} `,
|
|
309
|
-
false
|
|
309
|
+
false,
|
|
310
310
|
);
|
|
311
311
|
} else {
|
|
312
312
|
let result = '*\n';
|
|
@@ -318,7 +318,7 @@ export function addComment<T extends ts.Node>(
|
|
|
318
318
|
node,
|
|
319
319
|
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
320
320
|
result,
|
|
321
|
-
true
|
|
321
|
+
true,
|
|
322
322
|
);
|
|
323
323
|
}
|
|
324
324
|
}
|
|
@@ -363,7 +363,7 @@ function getComment(schema: NormalizedSchema): string[] {
|
|
|
363
363
|
export function addOptionalInformation<T extends ts.Node>(
|
|
364
364
|
node: T,
|
|
365
365
|
schema: NormalizedSchema,
|
|
366
|
-
terminate: boolean
|
|
366
|
+
terminate: boolean,
|
|
367
367
|
): T {
|
|
368
368
|
const format = schema.content.format;
|
|
369
369
|
const pattern = schema.content.pattern;
|
package/src/core/config.ts
CHANGED
|
@@ -85,7 +85,7 @@ export async function showConfig(version: string, c: Config): Promise<void> {
|
|
|
85
85
|
console.log('Plugins: count=' + plugins.length.toString());
|
|
86
86
|
for (const p of plugins) {
|
|
87
87
|
console.log(
|
|
88
|
-
` ${p.meta.name}@${p.meta.version}: ${p.meta.description ?? ''}
|
|
88
|
+
` ${p.meta.name}@${p.meta.version}: ${p.meta.description ?? ''}`,
|
|
89
89
|
);
|
|
90
90
|
}
|
|
91
91
|
console.log();
|
package/src/core/dtsGenerator.ts
CHANGED
|
@@ -49,7 +49,7 @@ export default class DtsGenerator {
|
|
|
49
49
|
'',
|
|
50
50
|
config.target,
|
|
51
51
|
false,
|
|
52
|
-
ts.ScriptKind.TS
|
|
52
|
+
ts.ScriptKind.TS,
|
|
53
53
|
);
|
|
54
54
|
Object.assign(file, {
|
|
55
55
|
statements: ts.factory.createNodeArray(root),
|
|
@@ -67,7 +67,7 @@ export default class DtsGenerator {
|
|
|
67
67
|
return printer.printNode(
|
|
68
68
|
ts.EmitHint.SourceFile,
|
|
69
69
|
transformedNodes,
|
|
70
|
-
file
|
|
70
|
+
file,
|
|
71
71
|
);
|
|
72
72
|
})();
|
|
73
73
|
|
|
@@ -96,7 +96,7 @@ export default class DtsGenerator {
|
|
|
96
96
|
return { pre, post };
|
|
97
97
|
}
|
|
98
98
|
private async getPreProcess(
|
|
99
|
-
pre: PluginConfig[]
|
|
99
|
+
pre: PluginConfig[],
|
|
100
100
|
): Promise<PreProcessHandler[]> {
|
|
101
101
|
debug('load pre process plugin.');
|
|
102
102
|
const result: PreProcessHandler[] = [];
|
|
@@ -112,14 +112,14 @@ export default class DtsGenerator {
|
|
|
112
112
|
debug(
|
|
113
113
|
' pre process plugin:',
|
|
114
114
|
pc.plugin.meta.name,
|
|
115
|
-
pc.plugin.meta.description
|
|
115
|
+
pc.plugin.meta.description,
|
|
116
116
|
);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
return result;
|
|
120
120
|
}
|
|
121
121
|
private async getPostProcess(
|
|
122
|
-
post: PluginConfig[]
|
|
122
|
+
post: PluginConfig[],
|
|
123
123
|
): Promise<ts.TransformerFactory<ts.SourceFile>[]> {
|
|
124
124
|
debug('load post process plugin.');
|
|
125
125
|
const result: ts.TransformerFactory<ts.SourceFile>[] = [];
|
|
@@ -133,9 +133,9 @@ export default class DtsGenerator {
|
|
|
133
133
|
if (factory != null) {
|
|
134
134
|
result.push(factory);
|
|
135
135
|
debug(
|
|
136
|
-
'
|
|
136
|
+
' post process plugin:',
|
|
137
137
|
pc.plugin.meta.name,
|
|
138
|
-
pc.plugin.meta.description
|
|
138
|
+
pc.plugin.meta.description,
|
|
139
139
|
);
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -153,13 +153,13 @@ export default class DtsGenerator {
|
|
|
153
153
|
if (value.schema !== undefined) {
|
|
154
154
|
const schema = value.schema;
|
|
155
155
|
debug(
|
|
156
|
-
` walk doProcess: key=${key} schemaId=${schema.id.getAbsoluteId()}
|
|
156
|
+
` walk doProcess: key=${key} schemaId=${schema.id.getAbsoluteId()}`,
|
|
157
157
|
);
|
|
158
158
|
result.push(this.walkSchema(schema, root));
|
|
159
159
|
}
|
|
160
160
|
if (value.children.size > 0) {
|
|
161
161
|
result.push(
|
|
162
|
-
ast.buildNamespaceNode(key, this.walk(value, false), root)
|
|
162
|
+
ast.buildNamespaceNode(key, this.walk(value, false), root),
|
|
163
163
|
);
|
|
164
164
|
}
|
|
165
165
|
}
|
|
@@ -174,16 +174,16 @@ export default class DtsGenerator {
|
|
|
174
174
|
ast.addComment(
|
|
175
175
|
this.parseSchema(normalized, root),
|
|
176
176
|
normalized,
|
|
177
|
-
true
|
|
177
|
+
true,
|
|
178
178
|
),
|
|
179
179
|
normalized,
|
|
180
|
-
true
|
|
180
|
+
true,
|
|
181
181
|
);
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
private parseSchema(
|
|
185
185
|
schema: NormalizedSchema,
|
|
186
|
-
root = false
|
|
186
|
+
root = false,
|
|
187
187
|
): ts.DeclarationStatement {
|
|
188
188
|
const type = schema.content.type;
|
|
189
189
|
switch (type) {
|
|
@@ -199,7 +199,7 @@ export default class DtsGenerator {
|
|
|
199
199
|
|
|
200
200
|
private normalizeContent(
|
|
201
201
|
schema: Schema,
|
|
202
|
-
pointer?: string
|
|
202
|
+
pointer?: string,
|
|
203
203
|
): NormalizedSchema {
|
|
204
204
|
if (pointer != null) {
|
|
205
205
|
schema = getSubSchema(schema, pointer);
|
|
@@ -232,7 +232,7 @@ export default class DtsGenerator {
|
|
|
232
232
|
}
|
|
233
233
|
if (
|
|
234
234
|
content.type === undefined &&
|
|
235
|
-
(content.properties
|
|
235
|
+
(content.properties ?? content.additionalProperties)
|
|
236
236
|
) {
|
|
237
237
|
content.type = 'object';
|
|
238
238
|
}
|
|
@@ -259,14 +259,16 @@ export default class DtsGenerator {
|
|
|
259
259
|
}
|
|
260
260
|
private generateDeclareType(
|
|
261
261
|
schema: NormalizedSchema,
|
|
262
|
-
root: boolean
|
|
262
|
+
root: boolean,
|
|
263
263
|
): ts.DeclarationStatement {
|
|
264
264
|
const content = schema.content;
|
|
265
265
|
if (
|
|
266
|
+
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
|
|
266
267
|
content.$ref ||
|
|
267
268
|
content.oneOf ||
|
|
268
269
|
content.anyOf ||
|
|
269
270
|
content.enum ||
|
|
271
|
+
/* eslint-enable @typescript-eslint/prefer-nullish-coalescing */
|
|
270
272
|
'const' in content ||
|
|
271
273
|
content.type !== 'object'
|
|
272
274
|
) {
|
|
@@ -280,19 +282,19 @@ export default class DtsGenerator {
|
|
|
280
282
|
|
|
281
283
|
private generateAnyTypeModel(
|
|
282
284
|
schema: NormalizedSchema,
|
|
283
|
-
root: boolean
|
|
285
|
+
root: boolean,
|
|
284
286
|
): ts.DeclarationStatement {
|
|
285
287
|
const member = ast.buildIndexSignatureNode(
|
|
286
288
|
'name',
|
|
287
289
|
ast.buildStringKeyword(),
|
|
288
|
-
ast.buildAnyKeyword()
|
|
290
|
+
ast.buildAnyKeyword(),
|
|
289
291
|
);
|
|
290
292
|
return ast.buildInterfaceNode(schema.id, [member], root);
|
|
291
293
|
}
|
|
292
294
|
|
|
293
295
|
private generateTypeCollection(
|
|
294
296
|
schema: NormalizedSchema,
|
|
295
|
-
root: boolean
|
|
297
|
+
root: boolean,
|
|
296
298
|
): ts.DeclarationStatement {
|
|
297
299
|
const type = this.generateArrayTypeProperty(schema);
|
|
298
300
|
return ast.buildTypeAliasNode(schema.id, type, root);
|
|
@@ -304,7 +306,7 @@ export default class DtsGenerator {
|
|
|
304
306
|
if (content.additionalProperties) {
|
|
305
307
|
const schema = this.normalizeContent(
|
|
306
308
|
baseSchema,
|
|
307
|
-
'/additionalProperties'
|
|
309
|
+
'/additionalProperties',
|
|
308
310
|
);
|
|
309
311
|
const valueType =
|
|
310
312
|
content.additionalProperties === true
|
|
@@ -313,7 +315,7 @@ export default class DtsGenerator {
|
|
|
313
315
|
const node = ast.buildIndexSignatureNode(
|
|
314
316
|
'name',
|
|
315
317
|
ast.buildStringKeyword(),
|
|
316
|
-
valueType
|
|
318
|
+
valueType,
|
|
317
319
|
);
|
|
318
320
|
result.push(ast.addOptionalInformation(node, schema, true));
|
|
319
321
|
}
|
|
@@ -321,21 +323,21 @@ export default class DtsGenerator {
|
|
|
321
323
|
for (const propertyName of Object.keys(content.properties)) {
|
|
322
324
|
const schema = this.normalizeContent(
|
|
323
325
|
baseSchema,
|
|
324
|
-
'/properties/' + tilde(propertyName)
|
|
326
|
+
'/properties/' + tilde(propertyName),
|
|
325
327
|
);
|
|
326
328
|
const node = ast.buildPropertySignature(
|
|
327
329
|
schema,
|
|
328
330
|
propertyName,
|
|
329
331
|
this.generateTypeProperty(schema),
|
|
330
332
|
baseSchema.content.required,
|
|
331
|
-
false
|
|
333
|
+
false,
|
|
332
334
|
);
|
|
333
335
|
result.push(
|
|
334
336
|
ast.addOptionalInformation(
|
|
335
337
|
ast.addComment(node, schema, true),
|
|
336
338
|
schema,
|
|
337
|
-
true
|
|
338
|
-
)
|
|
339
|
+
true,
|
|
340
|
+
),
|
|
339
341
|
);
|
|
340
342
|
}
|
|
341
343
|
}
|
|
@@ -350,27 +352,27 @@ export default class DtsGenerator {
|
|
|
350
352
|
this.generateTypeProperty(
|
|
351
353
|
this.normalizeContent(
|
|
352
354
|
baseSchema,
|
|
353
|
-
'/patternProperties/' + tilde(propertyName)
|
|
354
|
-
)
|
|
355
|
+
'/patternProperties/' + tilde(propertyName),
|
|
356
|
+
),
|
|
355
357
|
),
|
|
356
|
-
true
|
|
358
|
+
true,
|
|
357
359
|
),
|
|
358
360
|
baseSchema.content.required,
|
|
359
|
-
true
|
|
361
|
+
true,
|
|
360
362
|
);
|
|
361
363
|
result.push(
|
|
362
364
|
ts.addSyntheticTrailingComment(
|
|
363
365
|
node,
|
|
364
366
|
ts.SyntaxKind.MultiLineCommentTrivia,
|
|
365
|
-
` Patterns: ${properties.join(' | ')}
|
|
366
|
-
)
|
|
367
|
+
` Patterns: ${properties.join(' | ')} `,
|
|
368
|
+
),
|
|
367
369
|
);
|
|
368
370
|
}
|
|
369
371
|
return result;
|
|
370
372
|
}
|
|
371
373
|
private generateTypeProperty(
|
|
372
374
|
schema: NormalizedSchema,
|
|
373
|
-
terminate = true
|
|
375
|
+
terminate = true,
|
|
374
376
|
): ts.TypeNode {
|
|
375
377
|
const content = schema.content;
|
|
376
378
|
if (content.$ref) {
|
|
@@ -380,10 +382,10 @@ export default class DtsGenerator {
|
|
|
380
382
|
ast.addComment(
|
|
381
383
|
ast.buildTypeReferenceNode(refSchema, this.currentSchema),
|
|
382
384
|
refSchema,
|
|
383
|
-
false
|
|
385
|
+
false,
|
|
384
386
|
),
|
|
385
387
|
refSchema,
|
|
386
|
-
false
|
|
388
|
+
false,
|
|
387
389
|
);
|
|
388
390
|
return node;
|
|
389
391
|
}
|
|
@@ -395,7 +397,7 @@ export default class DtsGenerator {
|
|
|
395
397
|
content.anyOf,
|
|
396
398
|
mergeContent,
|
|
397
399
|
'/anyOf/',
|
|
398
|
-
terminate
|
|
400
|
+
terminate,
|
|
399
401
|
);
|
|
400
402
|
}
|
|
401
403
|
if (content.oneOf) {
|
|
@@ -406,14 +408,14 @@ export default class DtsGenerator {
|
|
|
406
408
|
content.oneOf,
|
|
407
409
|
mergeContent,
|
|
408
410
|
'/oneOf/',
|
|
409
|
-
terminate
|
|
411
|
+
terminate,
|
|
410
412
|
);
|
|
411
413
|
}
|
|
412
414
|
return this.generateLiteralTypeProperty(schema, terminate);
|
|
413
415
|
}
|
|
414
416
|
private generateLiteralTypeProperty(
|
|
415
417
|
schema: NormalizedSchema,
|
|
416
|
-
terminate = true
|
|
418
|
+
terminate = true,
|
|
417
419
|
): ts.TypeNode {
|
|
418
420
|
const content = schema.content;
|
|
419
421
|
if (content.enum) {
|
|
@@ -422,7 +424,7 @@ export default class DtsGenerator {
|
|
|
422
424
|
(value) => {
|
|
423
425
|
return this.generateLiteralTypeNode(content, value);
|
|
424
426
|
},
|
|
425
|
-
terminate
|
|
427
|
+
terminate,
|
|
426
428
|
);
|
|
427
429
|
} else if (content.not) {
|
|
428
430
|
return ast.buildVoidKeyword();
|
|
@@ -434,7 +436,7 @@ export default class DtsGenerator {
|
|
|
434
436
|
}
|
|
435
437
|
private checkExistOtherType(
|
|
436
438
|
content: JsonSchemaObject,
|
|
437
|
-
base: NormalizedSchema
|
|
439
|
+
base: NormalizedSchema,
|
|
438
440
|
): ts.TypeNode | undefined {
|
|
439
441
|
const schema = Object.assign({}, base, { content });
|
|
440
442
|
const result = this.generateLiteralTypeProperty(schema, false);
|
|
@@ -453,7 +455,7 @@ export default class DtsGenerator {
|
|
|
453
455
|
}
|
|
454
456
|
private generateLiteralTypeNode(
|
|
455
457
|
content: JsonSchemaObject,
|
|
456
|
-
value: any
|
|
458
|
+
value: any,
|
|
457
459
|
): ts.LiteralTypeNode {
|
|
458
460
|
switch (content.type) {
|
|
459
461
|
case 'integer':
|
|
@@ -485,13 +487,13 @@ export default class DtsGenerator {
|
|
|
485
487
|
contents: JsonSchema[],
|
|
486
488
|
mergeContent: JsonSchemaObject,
|
|
487
489
|
path: string,
|
|
488
|
-
terminate: boolean
|
|
490
|
+
terminate: boolean,
|
|
489
491
|
): ts.TypeNode {
|
|
490
492
|
const merged: boolean[] = [];
|
|
491
493
|
const children = contents.map((_, index) => {
|
|
492
494
|
const schema = this.normalizeContent(
|
|
493
495
|
baseSchema,
|
|
494
|
-
path + index.toString()
|
|
496
|
+
path + index.toString(),
|
|
495
497
|
);
|
|
496
498
|
merged.push(utils.mergeSchema(schema.content, mergeContent));
|
|
497
499
|
return schema;
|
|
@@ -508,31 +510,31 @@ export default class DtsGenerator {
|
|
|
508
510
|
? ast.addOptionalInformation(
|
|
509
511
|
this.generateTypeProperty(schema, false),
|
|
510
512
|
schema,
|
|
511
|
-
false
|
|
513
|
+
false,
|
|
512
514
|
)
|
|
513
515
|
: ast.addOptionalInformation(
|
|
514
516
|
ast.buildTypeReferenceNode(
|
|
515
517
|
schema,
|
|
516
|
-
this.currentSchema
|
|
518
|
+
this.currentSchema,
|
|
517
519
|
),
|
|
518
520
|
schema,
|
|
519
|
-
false
|
|
521
|
+
false,
|
|
520
522
|
);
|
|
521
523
|
if (baseType != null && !allRef && !merged[index]) {
|
|
522
524
|
return ast.buildIntersectionTypeNode(
|
|
523
525
|
[baseType, node],
|
|
524
|
-
false
|
|
526
|
+
false,
|
|
525
527
|
);
|
|
526
528
|
}
|
|
527
529
|
return node;
|
|
528
530
|
},
|
|
529
|
-
terminate
|
|
531
|
+
terminate,
|
|
530
532
|
),
|
|
531
533
|
baseSchema,
|
|
532
|
-
false
|
|
534
|
+
false,
|
|
533
535
|
),
|
|
534
536
|
baseSchema,
|
|
535
|
-
terminate
|
|
537
|
+
terminate,
|
|
536
538
|
);
|
|
537
539
|
if (baseType != null && allRef) {
|
|
538
540
|
return ast.buildIntersectionTypeNode([baseType, result], terminate);
|
|
@@ -542,7 +544,7 @@ export default class DtsGenerator {
|
|
|
542
544
|
|
|
543
545
|
private generateArrayTypeProperty(
|
|
544
546
|
schema: NormalizedSchema,
|
|
545
|
-
terminate = true
|
|
547
|
+
terminate = true,
|
|
546
548
|
): ts.TypeNode {
|
|
547
549
|
const items = schema.content.items;
|
|
548
550
|
const minItems = schema.content.minItems;
|
|
@@ -573,13 +575,13 @@ export default class DtsGenerator {
|
|
|
573
575
|
maxItems === undefined
|
|
574
576
|
) {
|
|
575
577
|
return ast.buildSimpleArrayNode(
|
|
576
|
-
ast.addOptionalInformation(node, subSchema, false)
|
|
578
|
+
ast.addOptionalInformation(node, subSchema, false),
|
|
577
579
|
);
|
|
578
580
|
} else {
|
|
579
581
|
return ast.addOptionalInformation(
|
|
580
582
|
ast.buildTupleTypeNode(node, minItems, maxItems),
|
|
581
583
|
schema,
|
|
582
|
-
terminate
|
|
584
|
+
terminate,
|
|
583
585
|
);
|
|
584
586
|
}
|
|
585
587
|
} else if (
|
|
@@ -591,9 +593,7 @@ export default class DtsGenerator {
|
|
|
591
593
|
return additionalItemsNode === false
|
|
592
594
|
? ast.buildTupleTypeNode([], 0, 0, additionalItemsNode)
|
|
593
595
|
: ast.buildSimpleArrayNode(
|
|
594
|
-
additionalItemsNode
|
|
595
|
-
? ast.buildAnyKeyword()
|
|
596
|
-
: additionalItemsNode
|
|
596
|
+
additionalItemsNode ?? ast.buildAnyKeyword(),
|
|
597
597
|
);
|
|
598
598
|
} else if (
|
|
599
599
|
minItems != null &&
|
|
@@ -606,7 +606,7 @@ export default class DtsGenerator {
|
|
|
606
606
|
for (let i = 0; i < items.length; i++) {
|
|
607
607
|
const type = this.normalizeContent(
|
|
608
608
|
schema,
|
|
609
|
-
'/items/' + i.toString()
|
|
609
|
+
'/items/' + i.toString(),
|
|
610
610
|
);
|
|
611
611
|
if (type.id.isEmpty()) {
|
|
612
612
|
types.push(this.generateTypeProperty(type, false));
|
|
@@ -615,11 +615,11 @@ export default class DtsGenerator {
|
|
|
615
615
|
ast.addOptionalInformation(
|
|
616
616
|
ast.buildTypeReferenceNode(
|
|
617
617
|
type,
|
|
618
|
-
this.currentSchema
|
|
618
|
+
this.currentSchema,
|
|
619
619
|
),
|
|
620
620
|
type,
|
|
621
|
-
false
|
|
622
|
-
)
|
|
621
|
+
false,
|
|
622
|
+
),
|
|
623
623
|
);
|
|
624
624
|
}
|
|
625
625
|
}
|
|
@@ -630,17 +630,17 @@ export default class DtsGenerator {
|
|
|
630
630
|
types,
|
|
631
631
|
minItems,
|
|
632
632
|
maxItems,
|
|
633
|
-
additionalItemsNode
|
|
633
|
+
additionalItemsNode,
|
|
634
634
|
),
|
|
635
635
|
schema,
|
|
636
|
-
terminate
|
|
636
|
+
terminate,
|
|
637
637
|
);
|
|
638
638
|
}
|
|
639
639
|
}
|
|
640
640
|
|
|
641
641
|
private generateType(
|
|
642
642
|
schema: NormalizedSchema,
|
|
643
|
-
terminate: boolean
|
|
643
|
+
terminate: boolean,
|
|
644
644
|
): ts.TypeNode {
|
|
645
645
|
const type = schema.content.type;
|
|
646
646
|
if (type == null) {
|
|
@@ -658,7 +658,7 @@ export default class DtsGenerator {
|
|
|
658
658
|
(t) => {
|
|
659
659
|
return this.generateTypeName(schema, t, false);
|
|
660
660
|
},
|
|
661
|
-
terminate
|
|
661
|
+
terminate,
|
|
662
662
|
);
|
|
663
663
|
}
|
|
664
664
|
}
|
|
@@ -666,7 +666,7 @@ export default class DtsGenerator {
|
|
|
666
666
|
private generateTypeName(
|
|
667
667
|
schema: NormalizedSchema,
|
|
668
668
|
type: string,
|
|
669
|
-
terminate: boolean
|
|
669
|
+
terminate: boolean,
|
|
670
670
|
): ts.TypeNode {
|
|
671
671
|
const tsType = utils.toTSType(type, schema.content);
|
|
672
672
|
if (tsType) {
|