aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidoc/fakerClass.ts
blob: d7206cf8302a5e1e38b99620ffc4c7ec2bae6628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { DeclarationReflection, ProjectReflection } from 'typedoc';
import { ReflectionKind } from 'typedoc';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
import { writeApiDocsModule } from './apiDocsWriter';
import { analyzeModule, processModuleMethods } from './moduleMethods';
import { analyzeSignature } from './signature';
import { extractModuleFieldName, selectApiSignature } from './typedoc';
import type { ModuleSummary } from './utils';

export async function processFakerClasses(
  project: ProjectReflection
): Promise<ModuleSummary[]> {
  const fakerClasses = project
    .getChildrenByKind(ReflectionKind.Class)
    .filter((clazz) => clazz.name === 'Faker' || clazz.name === 'SimpleFaker');

  if (fakerClasses.length !== 2) {
    throw new Error('Faker classes not found');
  }

  return Promise.all(fakerClasses.map(processClass));
}

async function processClass(
  fakerClass: DeclarationReflection
): Promise<ModuleSummary> {
  const { name } = fakerClass;
  const moduleFieldName = extractModuleFieldName(fakerClass);

  console.log(`Processing ${name} class`);

  const { comment, deprecated, examples } = analyzeModule(fakerClass);
  const methods: Method[] = [];

  console.debug(`- constructor`);
  methods.push(await processConstructor(fakerClass));

  methods.push(
    ...(await processModuleMethods(fakerClass, `${moduleFieldName}.`))
  );

  return writeApiDocsModule(
    name,
    moduleFieldName,
    comment,
    examples,
    deprecated,
    methods
  );
}

async function processConstructor(
  fakerClass: DeclarationReflection
): Promise<Method> {
  const constructor = fakerClass.getChildrenByKind(
    ReflectionKind.Constructor
  )[0];

  const signature = selectApiSignature(constructor);

  const method = await analyzeSignature(
    signature,
    '',
    `new ${fakerClass.name}`
  );

  return {
    ...method,
    name: 'constructor',
  };
}