diff --git a/scripts/apidoc/moduleMethods.ts b/scripts/apidoc/moduleMethods.ts index 7b090d13e9c..3d81c2db104 100644 --- a/scripts/apidoc/moduleMethods.ts +++ b/scripts/apidoc/moduleMethods.ts @@ -1,6 +1,6 @@ import * as TypeDoc from 'typedoc'; import type { Method } from '../../docs/.vitepress/components/api-docs/method'; -import faker from '../../src'; +import { faker } from '../../src'; import { writeApiDocsData, writeApiDocsModulePage } from './apiDocsWriter'; import { analyzeSignature, toBlock } from './signature'; import type { PageIndex } from './utils'; diff --git a/scripts/apidoc/signature.ts b/scripts/apidoc/signature.ts index 31674c834a0..dafd2c6d112 100644 --- a/scripts/apidoc/signature.ts +++ b/scripts/apidoc/signature.ts @@ -14,7 +14,7 @@ import type { Method, MethodParameter, } from '../../docs/.vitepress/components/api-docs/method'; -import faker from '../../src'; +import { faker } from '../../src'; import { pathOutputDir } from './utils'; // TODO ST-DDT 2022-02-20: Actually import this/fix module import errors // import vitepressConfig from '../../docs/.vitepress/config'; @@ -228,6 +228,11 @@ function typeToText(type_: Type, short = false): string { case 'reference': if (!type.typeArguments || !type.typeArguments.length) { return type.name; + } else if (type.name === 'LiteralUnion') { + return [ + typeToText(type.typeArguments[0]), + typeToText(type.typeArguments[1]), + ].join(' | '); } else { return `${type.name}<${type.typeArguments .map((t) => typeToText(t, short)) diff --git a/test/scripts/apidoc/signature.debug.ts b/test/scripts/apidoc/signature.debug.ts new file mode 100644 index 00000000000..542224ad95e --- /dev/null +++ b/test/scripts/apidoc/signature.debug.ts @@ -0,0 +1,16 @@ +/** + * This file exists, because vitest doesn't allow me to debug code outside of src and test. + * And it's easier to test these features independently from the main project. + */ +import { analyzeSignature } from '../../../scripts/apidoc/signature'; +import { loadExampleMethods } from './utils'; + +/* Run with `pnpm esno test/scripts/apidoc/signature.debug.ts` */ + +const methods = loadExampleMethods(); + +Object.entries(methods).forEach(([name, method]) => { + console.log('Analyzing: ', name); + const result = analyzeSignature(method.signatures[0], null, method.name); + console.log('Result: ', result); +}); diff --git a/test/scripts/apidoc/signature.example.ts b/test/scripts/apidoc/signature.example.ts index 16dae935b22..2219b072659 100644 --- a/test/scripts/apidoc/signature.example.ts +++ b/test/scripts/apidoc/signature.example.ts @@ -1,3 +1,5 @@ +import type { LiteralUnion } from '../../../src/faker'; + /** * Parameter options type with default from signature. */ @@ -113,6 +115,15 @@ export class SignatureTest { return fn('a'); } + /** + * Test with LiteralUnion. + * + * @param value `'a'` or `'b'`. + */ + literalUnionParamMethod(value: LiteralUnion<'a' | 'b'>): string { + return value; + } + /** * Test with a function parameters. * diff --git a/test/scripts/apidoc/signature.expected.json b/test/scripts/apidoc/signature.expected.json index f6e398fe95d..5e4f913afdd 100644 --- a/test/scripts/apidoc/signature.expected.json +++ b/test/scripts/apidoc/signature.expected.json @@ -1,14 +1,4 @@ { - "noParamMethod": { - "name": "noParamMethod", - "title": "No Param Method", - "description": "
Test with no parameters.
\n", - "parameters": [], - "returns": "number", - "examples": "faker.noParamMethod(): number\n
\nTest with LiteralUnion.
\n", + "parameters": [ + { + "name": "value", + "type": "\"a\" | \"b\" | string", + "description": "'a'
or 'b'
.
faker.literalUnionParamMethod(value: \"a\" | \"b\" | string): string\n
\nTest with no parameters.
\n", + "parameters": [], + "returns": "number", + "examples": "faker.noParamMethod(): number\n
\nTest with an optional parameter.
\n", + "parameters": [ + { + "name": "b?", + "type": "string", + "description": "The string parameter.
\n" + } + ], + "returns": "number", + "examples": "faker.optionalStringParamMethod(b?: string): number\n
\nTest with a function parameters with defaults.
\n", - "parameters": [ - { - "name": "a", - "type": "ParameterOptionsTypeA", - "default": "{ value: 1 }", - "description": "Parameter with signature default.
\n" - }, - { - "name": "b", - "type": "ParameterOptionsTypeB", - "default": "{ value: 1 }", - "description": "Parameter with jsdocs default.
\n" - }, - { - "name": "c", - "type": "ParameterOptionsTypeC", - "description": "Parameter with inner jsdocs default.
\n" - } - ], - "returns": "number", - "examples": "faker.optionsTypeParamMethodWithDefaults(a: ParameterOptionsTypeA = { value: 1 }, b: ParameterOptionsTypeB = { value: 1 }, c: ParameterOptionsTypeC): number\n
\nTest with an optional parameter.
\n", - "parameters": [ - { - "name": "b?", - "type": "string", - "description": "The string parameter.
\n" - } - ], - "returns": "number", - "examples": "faker.optionalStringParamMethod(b?: string): number\n
\nTest with a function parameters with defaults.
\n", + "parameters": [ + { + "name": "a", + "type": "ParameterOptionsTypeA", + "default": "{ value: 1 }", + "description": "Parameter with signature default.
\n" + }, + { + "name": "b", + "type": "ParameterOptionsTypeB", + "default": "{ value: 1 }", + "description": "Parameter with jsdocs default.
\n" + }, + { + "name": "c", + "type": "ParameterOptionsTypeC", + "description": "Parameter with inner jsdocs default.
\n" + } + ], + "returns": "number", + "examples": "faker.optionsTypeParamMethodWithDefaults(a: ParameterOptionsTypeA = { value: 1 }, b: ParameterOptionsTypeB = { value: 1 }, c: ParameterOptionsTypeC): number\n
\n