Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: nice literal unions #811

Merged
merged 5 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/apidoc/moduleMethods.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
7 changes: 6 additions & 1 deletion scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions test/scripts/apidoc/signature.debug.ts
Original file line number Diff line number Diff line change
@@ -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);
});
11 changes: 11 additions & 0 deletions test/scripts/apidoc/signature.example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LiteralUnion } from '../../../src/faker';

/**
* Parameter options type with default from signature.
*/
Expand Down Expand Up @@ -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.
*
Expand Down
124 changes: 70 additions & 54 deletions test/scripts/apidoc/signature.expected.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{
"noParamMethod": {
"name": "noParamMethod",
"title": "No Param Method",
"description": "<p>Test with no parameters.</p>\n",
"parameters": [],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">noParamMethod</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"defaultBooleanParamMethod": {
"name": "defaultBooleanParamMethod",
"title": "Default Boolean Param Method",
Expand Down Expand Up @@ -42,6 +32,22 @@
"deprecated": false,
"seeAlsos": []
},
"literalUnionParamMethod": {
"name": "literalUnionParamMethod",
"title": "Literal Union Param Method",
"description": "<p>Test with LiteralUnion.</p>\n",
"parameters": [
{
"name": "value",
"type": "\"a\" | \"b\" | string",
"description": "<p><code>'a'</code> or <code>'b'</code>.</p>\n"
}
],
"returns": "string",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">literalUnionParamMethod</span><span class=\"token punctuation\">(</span>value<span class=\"token operator\">:</span> <span class=\"token string\">\"a\"</span> <span class=\"token operator\">|</span> <span class=\"token string\">\"b\"</span> <span class=\"token operator\">|</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"methodWithDeprecated": {
"name": "methodWithDeprecated",
"title": "Method With Deprecated",
Expand Down Expand Up @@ -89,6 +95,32 @@
"deprecated": false,
"seeAlsos": []
},
"noParamMethod": {
"name": "noParamMethod",
"title": "No Param Method",
"description": "<p>Test with no parameters.</p>\n",
"parameters": [],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">noParamMethod</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"optionalStringParamMethod": {
"name": "optionalStringParamMethod",
"title": "Optional String Param Method",
"description": "<p>Test with an optional parameter.</p>\n",
"parameters": [
{
"name": "b?",
"type": "string",
"description": "<p>The string parameter.</p>\n"
}
],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">optionalStringParamMethod</span><span class=\"token punctuation\">(</span>b<span class=\"token operator\">?</span><span class=\"token operator\">:</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"optionsInlineParamMethodWithDefaults": {
"name": "optionsInlineParamMethodWithDefaults",
"title": "Options Inline Param Method With Defaults",
Expand Down Expand Up @@ -161,50 +193,6 @@
"deprecated": false,
"seeAlsos": []
},
"optionsTypeParamMethodWithDefaults": {
"name": "optionsTypeParamMethodWithDefaults",
"title": "Options Type Param Method With Defaults",
"description": "<p>Test with a function parameters with defaults.</p>\n",
"parameters": [
{
"name": "a",
"type": "ParameterOptionsTypeA",
"default": "{ value: 1 }",
"description": "<p>Parameter with signature default.</p>\n"
},
{
"name": "b",
"type": "ParameterOptionsTypeB",
"default": "{ value: 1 }",
"description": "<p>Parameter with jsdocs default.</p>\n"
},
{
"name": "c",
"type": "ParameterOptionsTypeC",
"description": "<p>Parameter with inner jsdocs default.</p>\n"
}
],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">optionsTypeParamMethodWithDefaults</span><span class=\"token punctuation\">(</span>a<span class=\"token operator\">:</span> ParameterOptionsTypeA <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> value<span class=\"token operator\">:</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> b<span class=\"token operator\">:</span> ParameterOptionsTypeB <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> value<span class=\"token operator\">:</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> c<span class=\"token operator\">:</span> ParameterOptionsTypeC<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"optionalStringParamMethod": {
"name": "optionalStringParamMethod",
"title": "Optional String Param Method",
"description": "<p>Test with an optional parameter.</p>\n",
"parameters": [
{
"name": "b?",
"type": "string",
"description": "<p>The string parameter.</p>\n"
}
],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">optionalStringParamMethod</span><span class=\"token punctuation\">(</span>b<span class=\"token operator\">?</span><span class=\"token operator\">:</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"optionsParamMethod": {
"name": "optionsParamMethod",
"title": "Options Param Method",
Expand Down Expand Up @@ -241,6 +229,34 @@
"deprecated": false,
"seeAlsos": []
},
"optionsTypeParamMethodWithDefaults": {
"name": "optionsTypeParamMethodWithDefaults",
"title": "Options Type Param Method With Defaults",
"description": "<p>Test with a function parameters with defaults.</p>\n",
"parameters": [
{
"name": "a",
"type": "ParameterOptionsTypeA",
"default": "{ value: 1 }",
"description": "<p>Parameter with signature default.</p>\n"
},
{
"name": "b",
"type": "ParameterOptionsTypeB",
"default": "{ value: 1 }",
"description": "<p>Parameter with jsdocs default.</p>\n"
},
{
"name": "c",
"type": "ParameterOptionsTypeC",
"description": "<p>Parameter with inner jsdocs default.</p>\n"
}
],
"returns": "number",
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">optionsTypeParamMethodWithDefaults</span><span class=\"token punctuation\">(</span>a<span class=\"token operator\">:</span> ParameterOptionsTypeA <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> value<span class=\"token operator\">:</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> b<span class=\"token operator\">:</span> ParameterOptionsTypeB <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> value<span class=\"token operator\">:</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> c<span class=\"token operator\">:</span> ParameterOptionsTypeC<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">number</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
},
"requiredNumberParamMethod": {
"name": "requiredNumberParamMethod",
"title": "Required Number Param Method",
Expand Down
24 changes: 5 additions & 19 deletions test/scripts/apidoc/signature.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import * as TypeDoc from 'typedoc';
import { afterAll, describe, expect, it } from 'vitest';
import type { Method } from '../../../docs/.vitepress/components/api-docs/method';
import { analyzeSignature } from '../../../scripts/apidoc/signature';
import { newTypeDocApp, patchProject } from '../../../scripts/apidoc/utils';
import { SignatureTest } from './signature.example';
import expected_ from './signature.expected.json';
import { loadExampleMethods } from './utils';

const expected: Record<string, Method> = expected_;

function prettyJson(object): string {
return JSON.stringify(object, null, 2);
}

describe('signature', () => {
const app = newTypeDocApp();

app.bootstrap({
entryPoints: ['test/scripts/apidoc/signature.example.ts'],
tsconfig: 'test/scripts/apidoc/tsconfig.json',
});

const project = app.convert();

patchProject(project);

const methods: Record<string, TypeDoc.DeclarationReflection> = project
.getChildrenByKind(TypeDoc.ReflectionKind.Class)[0]
.getChildrenByKind(TypeDoc.ReflectionKind.Method)
.reduce((a, v) => ({ ...a, [v.name]: v }), {});

describe('analyzeSignature()', () => {
const actuals = {};
const methods = loadExampleMethods();

it('dummy dependency to rerun the test if the example changes', () => {
expect(new SignatureTest()).toBeTruthy();
Expand All @@ -41,8 +26,9 @@ describe('signature', () => {
expect(Object.keys(methods).sort()).toEqual(Object.keys(expected).sort());
});

it.each(Object.keys(expected))('%s', (name) => {
it.each(Object.keys(expected).sort())('%s', (name) => {
const method = methods[name];
expect(method, `Method ${name} to be defined`).toBeDefined();
const actual = analyzeSignature(method.signatures[0], null, method.name);
actuals[name] = actual;

Expand Down
26 changes: 26 additions & 0 deletions test/scripts/apidoc/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { DeclarationReflection } from 'typedoc';
import { ReflectionKind } from 'typedoc';
import { newTypeDocApp, patchProject } from '../../../scripts/apidoc/utils';

/**
* Loads the example methods using TypeDoc.
*/
export function loadExampleMethods(): Record<string, DeclarationReflection> {
const app = newTypeDocApp();

app.bootstrap({
entryPoints: ['test/scripts/apidoc/signature.example.ts'],
tsconfig: 'test/scripts/apidoc/tsconfig.json',
});

const project = app.convert();

patchProject(project);

const methods: Record<string, DeclarationReflection> = project
.getChildrenByKind(ReflectionKind.Class)[0]
.getChildrenByKind(ReflectionKind.Method)
.reduce((a, v) => ({ ...a, [v.name]: v }), {});

return methods;
}