diff --git a/scripts/apidoc/signature.ts b/scripts/apidoc/signature.ts index d34abf188d4..a4b384a9450 100644 --- a/scripts/apidoc/signature.ts +++ b/scripts/apidoc/signature.ts @@ -277,20 +277,29 @@ function typeToText(type_?: Type, short = false): string { const type = type_ as SomeType; switch (type.type) { - case 'array': - return `${typeToText(type.elementType, short)}[]`; + case 'array': { + const text = typeToText(type.elementType, short); + if (text.includes('|') || text.includes('{')) { + return `Array<${text}>`; + } else { + return `${text}[]`; + } + } + case 'union': return type.types .map((t) => typeToText(t, short)) + .map((t) => (t.includes('=>') ? `(${t})` : t)) .sort() .join(' | '); + 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]), + typeToText(type.typeArguments[0], short), + typeToText(type.typeArguments[1], short), ].join(' | '); } else { return `${type.name}<${type.typeArguments @@ -300,13 +309,25 @@ function typeToText(type_?: Type, short = false): string { case 'reflection': return declarationTypeToText(type.declaration, short); + case 'indexedAccess': return `${typeToText(type.objectType, short)}[${typeToText( type.indexType, short )}]`; + case 'literal': return formatTypescript(type.toString()).replace(/;\n$/, ''); + + case 'typeOperator': { + const text = typeToText(type.target, short); + if (short && type.operator === 'readonly') { + return text; + } else { + return `${type.operator} ${text}`; + } + } + default: return type.toString(); } diff --git a/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap b/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap index 0d34cb7a166..b030225a3b9 100644 --- a/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap +++ b/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap @@ -5,7 +5,10 @@ exports[`signature > analyzeSignature() > complexArrayParameter 1`] = ` "deprecated": false, "description": "

Complex array parameter.

", - "examples": "
ts
faker.complexArrayParameter<T>(array: readonly Object[]): T
+  "examples": "
ts
faker.complexArrayParameter<T>(array: readonly Array<{
+  value: T,
+  weight: number
+}>): T
 
", "name": "complexArrayParameter", @@ -21,7 +24,7 @@ exports[`signature > analyzeSignature() > complexArrayParameter 1`] = ` "description": "

Array to pick the value from.

", "name": "array", - "type": "readonly Object[]", + "type": "Array<{ ... }>", }, { "default": undefined, @@ -123,7 +126,7 @@ exports[`signature > analyzeSignature() > literalUnionParamMethod 1`] = ` "deprecated": false, "description": "

Test with LiteralUnion.

", - "examples": "
ts
faker.literalUnionParamMethod(value: 'a' | 'b' | string): string
+  "examples": "
ts
faker.literalUnionParamMethod(value: 'a' | 'b' | string, namedValue: AB | string, array: readonly Array<'a' | 'b' | string>, namedArray: readonly Array<AB | string>, mixed: 'a' | 'b' | string | readonly Array<'a' | 'b' | string>, namedMixed: AB | string | readonly Array<AB | string>): string
 
", "name": "literalUnionParamMethod", @@ -135,6 +138,41 @@ exports[`signature > analyzeSignature() > literalUnionParamMethod 1`] = ` "name": "value", "type": "'a' | 'b' | string", }, + { + "default": undefined, + "description": "

'a' or 'b'.

+", + "name": "namedValue", + "type": "AB | string", + }, + { + "default": undefined, + "description": "

Array of 'a' or 'b'.

+", + "name": "array", + "type": "Array<'a' | 'b' | string>", + }, + { + "default": undefined, + "description": "

Array of 'a' or 'b'.

+", + "name": "namedArray", + "type": "Array", + }, + { + "default": undefined, + "description": "

Value 'a' or 'b' or an array thereof.

+", + "name": "mixed", + "type": "'a' | 'b' | string | Array<'a' | 'b' | string>", + }, + { + "default": undefined, + "description": "

Value 'a' or 'b' or an array thereof.

+", + "name": "namedMixed", + "type": "AB | string | Array", + }, ], "returns": "string", "seeAlsos": [], diff --git a/test/scripts/apidoc/signature.example.ts b/test/scripts/apidoc/signature.example.ts index 2c93b1afcf2..e99256262f4 100644 --- a/test/scripts/apidoc/signature.example.ts +++ b/test/scripts/apidoc/signature.example.ts @@ -60,6 +60,11 @@ export interface ParameterOptionsInterfaceC { value?: number; } +/** + * A or B. + */ +export type AB = 'a' | 'b'; + export class SignatureTest { /** * Test with no parameters. @@ -128,9 +133,28 @@ export class SignatureTest { * Test with LiteralUnion. * * @param value `'a'` or `'b'`. + * @param namedValue `'a'` or `'b'`. + * @param array Array of `'a'` or `'b'`. + * @param namedArray Array of `'a'` or `'b'`. + * @param mixed Value `'a'` or `'b'` or an array thereof. + * @param namedMixed Value `'a'` or `'b'` or an array thereof. */ - literalUnionParamMethod(value: LiteralUnion<'a' | 'b'>): string { - return value; + literalUnionParamMethod( + value: LiteralUnion<'a' | 'b'>, + namedValue: LiteralUnion, + array: readonly LiteralUnion<'a' | 'b'>[], + namedArray: readonly LiteralUnion[], + mixed: LiteralUnion<'a' | 'b'> | readonly LiteralUnion<'a' | 'b'>[], + namedMixed: readonly LiteralUnion[] | LiteralUnion + ): string { + return ( + value + + namedValue + + array.join('') + + namedArray.join('') + + String(mixed) + + String(namedMixed) + ); } /**