Skip to content

Commit 9ce8563

Browse files
author
Martynas Žilinskas
authored
Feature: Class plugin (#11)
1 parent 65d11a1 commit 9ce8563

12 files changed

Lines changed: 593 additions & 47 deletions

File tree

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
[ModuleDeclaration-0]: index\foonamespace.md#foonamespace
1+
[ClassDeclaration-0]: index/hello.md#hello
22
# index
33

4-
## MyFunction()
5-
6-
```typescript
7-
function MyFunction<T extends { myProperty: string; myPropertyTwo?: number | undefined; } = { myProperty: string; }>(): T
8-
```
9-
10-
### Type parameters
11-
12-
| Name | Constraint type | Default type |
13-
| ---- | -------------------------------------------------------------- | ------------------------- |
14-
| T | \{ myProperty: string; myPropertyTwo?: number \| undefined; \} | \{ myProperty: string; \} |
15-
16-
### Return type
17-
18-
T
19-
20-
21-
## [FooNamespace][ModuleDeclaration-0]
4+
## [Hello][ClassDeclaration-0]
225

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Hello
2+
3+
```typescript
4+
class Hello
5+
```
6+
## Constructor
7+
8+
### constructor(arg)
9+
10+
This is a constructor
11+
12+
```typescript
13+
constructor(arg: string)
14+
```
15+
#### Parameters
16+
17+
| Name | Type | Description |
18+
| ---- | ------ | ---------------------- |
19+
| arg | string | This is an argument ;) |
20+
21+
22+
## Methods
23+
24+
### GetFoo(arg)
25+
26+
```typescript
27+
public GetFoo(arg: number): string
28+
```
29+
#### Parameters
30+
31+
| Name | Type |
32+
| ---- | ------ |
33+
| arg | number |
34+
35+
36+
### GetFoo(arg)
37+
38+
```typescript
39+
public GetFoo(arg: string): string
40+
```
41+
#### Parameters
42+
43+
| Name | Type |
44+
| ---- | ------ |
45+
| arg | string |
46+
47+
48+
### GetFoo(arg)
49+
50+
```typescript
51+
public GetFoo(arg: string | number): string
52+
```
53+
#### Parameters
54+
55+
| Name | Type |
56+
| ---- | ---------------- |
57+
| arg | string \| number |
58+
59+
60+
## Properties
61+
62+
### Foo
63+
64+
```typescript
65+
public Foo: string;
66+
```
67+
68+
### Type
69+
70+
string
71+

packages/ts-docs-gen/examples/simple/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@
7979
// anotherProperty: number;
8080
// }
8181

82-
export function MyFunction<T extends { myProperty: string, myPropertyTwo?: number } = { myProperty: string }>(): T {
83-
return {
84-
myProperty: "sampleString"
85-
} as T;
86-
}
82+
// export function MyFunction<T extends { myProperty: string, myPropertyTwo?: number } = { myProperty: string }>(): T {
83+
// return {
84+
// myProperty: "sampleString"
85+
// } as T;
86+
// }
8787

8888
// export interface MyInterface {
8989
// <TValue>(param1: TValue, param2: TValue): boolean;
@@ -203,10 +203,26 @@ export function MyFunction<T extends { myProperty: string, myPropertyTwo?: numbe
203203
// */
204204
// export type Hello = Uogos;
205205

206-
export namespace FooNamespace {
207-
export namespace BooNamespace {
208-
export namespace BooNamespace2 {
209-
export const Hello = "World!";
210-
}
206+
// export namespace FooNamespace {
207+
// export namespace BooNamespace {
208+
// export namespace BooNamespace2 {
209+
// export const Hello = "World!";
210+
// }
211+
// }
212+
// }
213+
214+
export class Hello {
215+
/**
216+
* This is a constructor
217+
* @param arg This is an argument ;)
218+
*/
219+
constructor(arg: string) { }
220+
221+
GetFoo(arg: number): string
222+
GetFoo(arg: string): string
223+
GetFoo(arg: string | number): string {
224+
throw new Error("Method not implemented.");
211225
}
226+
227+
public Foo: string;
212228
}

packages/ts-docs-gen/src/contracts/plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ export interface PluginOptions<TKind = Contracts.ApiItemDto> {
2929
IsPluginResultExists: IsPluginResultExistsHandler;
3030
}
3131

32-
export interface PluginResult<TKind = Contracts.ApiItemDto> {
33-
Reference: ApiItemReference;
34-
ApiItem: TKind;
32+
export interface PluginResultData {
3533
/**
3634
* All headings used in `Result` with ApiItemIds.
3735
*/
@@ -47,6 +45,11 @@ export interface PluginResult<TKind = Contracts.ApiItemDto> {
4745
Members?: PluginMember[];
4846
}
4947

48+
export interface PluginResult<TKind = Contracts.ApiItemDto> extends PluginResultData {
49+
Reference: ApiItemReference;
50+
ApiItem: TKind;
51+
}
52+
5053
export interface Plugin<TKind = Contracts.ApiItemDto> {
5154
SupportedApiItemKinds(): SupportedApiItemKindType[];
5255
CheckApiItem(item: TKind): boolean;

packages/ts-docs-gen/src/default-plugins.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ import { ApiEnumPlugin } from "./plugins/api-enum-plugin";
44
import { ApiFunctionPlugin } from "./plugins/api-function-plugin";
55
import { ApiTypePlugin } from "./plugins/api-type-plugin";
66
import { ApiNamespacePlugin } from "./plugins/api-namespace-plugin";
7+
import { ApiClassPlugin } from "./plugins/api-class-plugin";
8+
import { ApiClassConstructorPlugin } from "./plugins/api-class-constructor-plugin";
9+
import { ApiClassMethodPlugin } from "./plugins/api-class-method-plugin";
10+
import { ApiClassPropertyPlugin } from "./plugins/api-class-property-plugin";
711

812
export const DefaultPlugins = [
913
new ApiSourceFilePlugin(),
1014
new ApiVariablePlugin(),
1115
new ApiEnumPlugin(),
1216
new ApiFunctionPlugin(),
1317
new ApiTypePlugin(),
14-
new ApiNamespacePlugin()
18+
new ApiNamespacePlugin(),
19+
new ApiClassPlugin(),
20+
new ApiClassConstructorPlugin(),
21+
new ApiClassMethodPlugin(),
22+
new ApiClassPropertyPlugin()
1523
];

packages/ts-docs-gen/src/generator-helpers.ts

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MarkdownGenerator, MarkdownBuilder, Contracts as MarkdownContracts } fr
44
import * as path from "path";
55

66
import { ApiItemReference } from "./contracts/api-item-reference";
7-
import { ApiItemKindsAdditional } from "./contracts/plugin";
7+
import { ApiItemKindsAdditional, PluginResultData } from "./contracts/plugin";
88
import { Logger } from "./utils/logger";
99

1010
export namespace GeneratorHelpers {
@@ -306,6 +306,43 @@ export namespace GeneratorHelpers {
306306
return `${apiItem.Name}${$extends}${defaultType}`;
307307
}
308308

309+
export function ClassToString(
310+
apiItem: Contracts.ApiClassDto,
311+
typeParameters?: Contracts.ApiTypeParameterDto[],
312+
alias?: string
313+
): string {
314+
const name = alias || apiItem.Name;
315+
// Abstract
316+
const abstract = apiItem.IsAbstract ? "abstract " : "";
317+
318+
// TypeParameters
319+
let typeParametersString: string;
320+
if (typeParameters != null && typeParameters.length > 0) {
321+
const params: string[] = typeParameters.map(TypeParameterToString);
322+
typeParametersString = `<${params.join(", ")}>`;
323+
} else {
324+
typeParametersString = "";
325+
}
326+
327+
// Extends
328+
let extendsString: string;
329+
if (apiItem.Extends != null) {
330+
extendsString = ` extends ${apiItem.Extends.Text}`;
331+
} else {
332+
extendsString = "";
333+
}
334+
335+
// Implements
336+
let implementsString: string;
337+
if (apiItem.Implements != null && apiItem.Implements.length > 0) {
338+
implementsString = ` implements ${apiItem.Implements.map(x => x.Text).join(", ")}`;
339+
} else {
340+
implementsString = "";
341+
}
342+
343+
return `${abstract}class ${name}${typeParametersString}${extendsString}${implementsString}`;
344+
}
345+
309346
export function ApiFunctionToSimpleString(
310347
alias: string,
311348
apiItem: Contracts.ApiFunctionDto,
@@ -319,6 +356,66 @@ export namespace GeneratorHelpers {
319356
return `${name}(${parametersString})`;
320357
}
321358

359+
export function ApiClassMethodToString(
360+
apiItem: Contracts.ApiClassMethodDto,
361+
parameters: Contracts.ApiParameterDto[],
362+
alias?: string
363+
): string {
364+
const name = alias || apiItem.Name;
365+
366+
const optional = apiItem.IsOptional ? "?" : "";
367+
const abstract = apiItem.IsAbstract ? " abstract" : "";
368+
const async = apiItem.IsAsync ? " async" : "";
369+
const $static = apiItem.IsStatic ? " static" : "";
370+
const functionHeader = CallableParametersToString(`${name}${optional}`, parameters, apiItem.ReturnType);
371+
372+
return `${apiItem.AccessModifier}${$static}${abstract}${async} ${functionHeader}`.trim();
373+
}
374+
375+
export function ApiClassPropertyToString(apiItem: Contracts.ApiClassPropertyDto, alias?: string): string {
376+
const name = alias || apiItem.Name;
377+
378+
const optional = apiItem.IsOptional ? "?" : "";
379+
const abstract = apiItem.IsAbstract ? " abstract" : "";
380+
const $static = apiItem.IsStatic ? " static" : "";
381+
382+
return `${apiItem.AccessModifier}${$static}${abstract} ${name}${optional}: ${apiItem.Type.Text};`;
383+
}
384+
385+
export function CallableParametersToSimpleString(text: string, parameters: Contracts.ApiParameterDto[]): string {
386+
const parametersString = parameters
387+
.map(x => x.Name)
388+
.join(", ");
389+
390+
return `${text}(${parametersString})`;
391+
}
392+
393+
export function CallableParametersToString(
394+
text: string,
395+
parameters: Contracts.ApiParameterDto[],
396+
returnType?: Contracts.TypeDto
397+
): string {
398+
// Parameters
399+
let parametersString: string;
400+
if (parameters != null && parameters.length > 0) {
401+
parametersString = parameters
402+
.map(x => `${x.Name}: ${x.Type.Text}`)
403+
.join(", ");
404+
} else {
405+
parametersString = "";
406+
}
407+
408+
// ReturnType
409+
let returnTypeString: string;
410+
if (returnType != null) {
411+
returnTypeString = `: ${returnType.Text}`;
412+
} else {
413+
returnTypeString = "";
414+
}
415+
416+
return `${text}(${parametersString})${returnTypeString}`;
417+
}
418+
322419
export function GetApiItemsFromReference<T extends Contracts.ApiItemDto>(
323420
items: Contracts.ApiItemReference[],
324421
extractedData: ExtractDto
@@ -344,4 +441,21 @@ export namespace GeneratorHelpers {
344441
export function StandardisePath(pathString: string): string {
345442
return pathString.split(path.sep).join("/");
346443
}
444+
445+
export function MergePluginResultData<T extends PluginResultData>(a: T, b: Partial<PluginResultData>): T {
446+
a.Headings = a.Headings.concat(b.Headings || []);
447+
a.Members = (a.Members || []).concat(b.Members || []);
448+
a.Result = a.Result.concat(b.Result || []);
449+
a.UsedReferences = a.UsedReferences.concat(b.UsedReferences || []);
450+
451+
return a;
452+
}
453+
454+
export function GetDefaultPluginResultData(): PluginResultData {
455+
return {
456+
Headings: [],
457+
Result: [],
458+
UsedReferences: []
459+
};
460+
}
347461
}

0 commit comments

Comments
 (0)