[@kbn/docs-utils] Support ConstructSignatures (#131287)

This commit is contained in:
Alejandro Fernández Haro 2022-05-02 22:30:12 +02:00 committed by GitHub
parent 90e8795ed7
commit 060cdf6df2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 119 additions and 15 deletions

View file

@ -53,6 +53,24 @@ it('Test number primitive doc def', () => {
expect(def.type).toBe(TypeKind.NumberKind);
});
it('Test a constructor type declaration inside an interface', () => {
const node = nodes.find((n) => getNodeName(n) === 'ClassConstructorWithStaticProperties');
expect(node).toBeDefined();
const def = buildApiDeclarationTopNode(node!, {
plugins,
log,
currentPluginId: plugins[0].manifest.id,
scope: ApiScope.CLIENT,
captureReferences: false,
});
expect(def.type).toBe(TypeKind.InterfaceKind);
expect(def.children).toHaveLength(2);
expect(def.children![1].type).toBe(TypeKind.FunctionKind);
expect(def.children![1].label).toBe('new');
expect(def.children![1].id).toBe('def-public.ClassConstructorWithStaticProperties.new');
});
it('Function type is exported as type with signature', () => {
const node = nodes.find((n) => getNodeName(n) === 'FnWithGeneric');
expect(node).toBeDefined();

View file

@ -65,12 +65,17 @@ export function buildApiDeclaration(node: Node, opts: BuildApiDecOpts): ApiDecla
Node.isMethodSignature(node) ||
Node.isFunctionDeclaration(node) ||
Node.isMethodDeclaration(node) ||
Node.isConstructSignatureDeclaration(node) ||
Node.isConstructorDeclaration(node)
) {
return buildFunctionDec(node, {
...opts,
// Use "Constructor" if applicable, instead of the default "Unnamed"
name: Node.isConstructorDeclaration(node) ? 'Constructor' : node.getName() || 'Unnamed',
name: Node.isConstructSignatureDeclaration(node)
? 'new'
: Node.isConstructorDeclaration(node)
? 'Constructor'
: node.getName() || 'Unnamed',
});
} else if (
Node.isPropertySignature(node) ||

View file

@ -11,6 +11,7 @@ import {
MethodDeclaration,
ConstructorDeclaration,
MethodSignature,
ConstructSignatureDeclaration,
} from 'ts-morph';
import { buildApiDecsForParameters } from './build_parameter_decs';
@ -23,7 +24,12 @@ import { BuildApiDecOpts } from './types';
* Takes the various function-like node declaration types and converts them into an ApiDeclaration.
*/
export function buildFunctionDec(
node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | MethodSignature,
node:
| ConstructSignatureDeclaration
| FunctionDeclaration
| MethodDeclaration
| ConstructorDeclaration
| MethodSignature,
opts: BuildApiDecOpts
): ApiDeclaration {
const fn = {

View file

@ -46,7 +46,11 @@ export function buildParentApiId(parentName: string, parentsParentApiId?: string
}
export function getOptsForChild(node: Node, parentOpts: BuildApiDecOpts): BuildApiDecOpts {
const name = isNamedNode(node) ? node.getName() : 'Unnamed';
const name = Node.isConstructSignatureDeclaration(node)
? 'new'
: isNamedNode(node)
? node.getName()
: 'Unnamed';
return getOptsForChildWithName(name, parentOpts);
}

View file

@ -65,11 +65,16 @@ export function runBuildApiDocsCli() {
// Delete all files except the README that warns about the auto-generated nature of
// the folder.
const files = Fs.readdirSync(outputFolder);
files.forEach((file) => {
if (file.indexOf('README.md') < 0) {
Fs.rmSync(Path.resolve(outputFolder, file));
}
});
await Promise.all(
files
.filter((file) => file.indexOf('README.md') < 0)
.map(
(file) =>
new Promise<void>((resolve, reject) =>
Fs.rm(Path.resolve(outputFolder, file), (err) => (err ? reject(err) : resolve()))
)
)
);
}
const collectReferences = flags.references as boolean;

View file

@ -38,7 +38,7 @@ beforeAll(() => {
});
test('foo service has all exports', () => {
expect(doc?.client.length).toBe(37);
expect(doc?.client.length).toBe(38);
const split = splitApisByFolder(doc);
expect(split.length).toBe(2);
@ -47,5 +47,5 @@ test('foo service has all exports', () => {
expect(fooDoc?.common.length).toBe(1);
expect(fooDoc?.client.length).toBe(2);
expect(mainDoc?.client.length).toBe(35);
expect(mainDoc?.client.length).toBe(36);
});

View file

@ -24,6 +24,11 @@ export interface InterfaceWithIndexSignature {
[key: string]: { foo: string };
}
export interface ClassConstructorWithStaticProperties {
staticProperty1: string;
new (config: { foo: string }): InterfaceWithIndexSignature;
}
export function plugin() {
return new PluginA();
}

View file

@ -712,6 +712,67 @@
],
"initialIsOpen": false
},
{
"parentPluginId": "pluginA",
"id": "def-public.ClassConstructorWithStaticProperties",
"type": "Interface",
"tags": [],
"label": "ClassConstructorWithStaticProperties",
"description": [],
"path": "packages/kbn-docs-utils/src/api_docs/tests/__fixtures__/src/plugin_a/public/index.ts",
"deprecated": false,
"children": [
{
"parentPluginId": "pluginA",
"id": "def-public.ClassConstructorWithStaticProperties.staticProperty1",
"type": "string",
"tags": [],
"label": "staticProperty1",
"description": [],
"path": "packages/kbn-docs-utils/src/api_docs/tests/__fixtures__/src/plugin_a/public/index.ts",
"deprecated": false
},
{
"parentPluginId": "pluginA",
"id": "def-public.ClassConstructorWithStaticProperties.new",
"type": "Function",
"tags": [],
"label": "new",
"description": [],
"signature": [
"any"
],
"path": "packages/kbn-docs-utils/src/api_docs/tests/__fixtures__/src/plugin_a/public/index.ts",
"deprecated": false,
"children": [
{
"parentPluginId": "pluginA",
"id": "def-public.ClassConstructorWithStaticProperties.new.$1",
"type": "Object",
"tags": [],
"label": "config",
"description": [],
"path": "packages/kbn-docs-utils/src/api_docs/tests/__fixtures__/src/plugin_a/public/index.ts",
"deprecated": false,
"children": [
{
"parentPluginId": "pluginA",
"id": "def-public.ClassConstructorWithStaticProperties.new.$1.foo",
"type": "string",
"tags": [],
"label": "foo",
"description": [],
"path": "packages/kbn-docs-utils/src/api_docs/tests/__fixtures__/src/plugin_a/public/index.ts",
"deprecated": false
}
]
}
],
"returnComment": []
}
],
"initialIsOpen": false
},
{
"parentPluginId": "pluginA",
"id": "def-public.ExampleInterface",

View file

@ -4,7 +4,7 @@ slug: /kibana-dev-docs/api/pluginA
title: "pluginA"
image: https://source.unsplash.com/400x175/?github
summary: API docs for the pluginA plugin
date: 2022-02-14
date: 2022-04-30
tags: ['contributor', 'dev', 'apidocs', 'kibana', 'pluginA']
warning: This document is auto-generated and is meant to be viewed inside our experimental, new docs system. Reach out in #docs-engineering for more info.
---
@ -18,7 +18,7 @@ Contact Kibana Core for questions regarding this plugin.
| Public API count | Any count | Items lacking comments | Missing exports |
|-------------------|-----------|------------------------|-----------------|
| 131 | 1 | 71 | 2 |
| 136 | 1 | 76 | 2 |
## Client

View file

@ -4,7 +4,7 @@ slug: /kibana-dev-docs/api/pluginA-foo
title: "pluginA.foo"
image: https://source.unsplash.com/400x175/?github
summary: API docs for the pluginA.foo plugin
date: 2022-02-14
date: 2022-04-30
tags: ['contributor', 'dev', 'apidocs', 'kibana', 'pluginA.foo']
warning: This document is auto-generated and is meant to be viewed inside our experimental, new docs system. Reach out in #docs-engineering for more info.
---
@ -18,7 +18,7 @@ Contact Kibana Core for questions regarding this plugin.
| Public API count | Any count | Items lacking comments | Missing exports |
|-------------------|-----------|------------------------|-----------------|
| 131 | 1 | 71 | 2 |
| 136 | 1 | 76 | 2 |
## Client

View file

@ -4,7 +4,7 @@ slug: /kibana-dev-docs/api/pluginB
title: "pluginB"
image: https://source.unsplash.com/400x175/?github
summary: API docs for the pluginB plugin
date: 2022-02-14
date: 2022-04-30
tags: ['contributor', 'dev', 'apidocs', 'kibana', 'pluginB']
warning: This document is auto-generated and is meant to be viewed inside our experimental, new docs system. Reach out in #docs-engineering for more info.
---