Skip to content

Commit

Permalink
docs: nice string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 8, 2022
1 parent 398deda commit 7b0dc44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 3 additions & 1 deletion scripts/apidoc/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
MethodParameter,
} from '../../docs/.vitepress/components/api-docs/method';
import { faker } from '../../src';
import { pathOutputDir } from './utils';
import { formatTypescript, 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 @@ -245,6 +245,8 @@ function typeToText(type_: Type, short = false): string {
type.indexType,
short
)}]`;
case 'literal':
return formatTypescript(type.toString()).replace(/;\n$/, '');
default:
return type.toString();
}
Expand Down
9 changes: 9 additions & 0 deletions test/scripts/apidoc/signature.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ export class SignatureTest {
return fn('a');
}

/**
* Test with string union.
*
* @param value `'a'` or `'b'`.
*/
stringUnionParamMethod(value: 'a' | 'b'): string {
return value;
}

/**
* Test with LiteralUnion.
*
Expand Down
20 changes: 18 additions & 2 deletions test/scripts/apidoc/signature.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
"parameters": [
{
"name": "value",
"type": "\"a\" | \"b\" | string",
"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>",
"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": []
},
Expand Down Expand Up @@ -272,5 +272,21 @@
"examples": "<div class=\"language-ts\"><pre v-pre><code>faker<span class=\"token punctuation\">.</span><span class=\"token function\">requiredNumberParamMethod</span><span class=\"token punctuation\">(</span>a<span class=\"token operator\">:</span> <span class=\"token builtin\">number</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": []
},
"stringUnionParamMethod": {
"name": "stringUnionParamMethod",
"title": "String Union Param Method",
"description": "<p>Test with string union.</p>\n",
"parameters": [
{
"name": "value",
"type": "'a' | 'b'",
"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\">stringUnionParamMethod</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 punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n</code></pre>\n</div>",
"deprecated": false,
"seeAlsos": []
}
}

0 comments on commit 7b0dc44

Please sign in to comment.