dtsgenerator 3.17.0 → 3.19.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.
@@ -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
- ' pre process plugin:',
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 || content.additionalProperties)
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 === undefined
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) {