Skip to content

Commit c271811

Browse files
author
Martynas Žilinskas
authored
Feature: Getters and setters (#14)
1 parent 59ae49e commit c271811

9 files changed

Lines changed: 194 additions & 69 deletions

File tree

common/config/rush/npm-shrinkwrap.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[InterfaceDeclaration-9]: index.md#dictionary
66
[InterfaceDeclaration-6]: index.md#objectsinterface
77
[InterfaceDeclaration-12]: index.md#monsterinterface
8+
[ClassDeclaration-0]: index/hello.md#hello
89
# index
910

1011
## interface ExtendedBar
@@ -547,3 +548,5 @@ interface MyInterface {
547548
| MyPropertyTwo | Object |
548549
| MyPropertyThree | number |
549550

551+
## [Hello][ClassDeclaration-0]
552+

packages/ts-docs-gen/examples/simple/docs/api/index/hello.md

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,35 @@ constructor(arg: string)
1919
| arg | string | This is an argument ;) |
2020

2121

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-
22+
## Properties
3523

36-
### GetFoo(arg)
24+
### get Foo
3725

3826
```typescript
39-
public GetFoo(arg: string): string
27+
public get Foo: string;
4028
```
41-
#### Parameters
4229

43-
| Name | Type |
44-
| ---- | ------ |
45-
| arg | string |
30+
#### Type
4631

32+
string
4733

48-
### GetFoo(arg)
34+
### set Foo
4935

5036
```typescript
51-
public GetFoo(arg: string | number): string
37+
public set Foo: string;
5238
```
53-
#### Parameters
5439

55-
| Name | Type |
56-
| ---- | -------------------- |
57-
| arg | string | number |
40+
#### Type
5841

42+
string
5943

60-
## Properties
61-
62-
### Foo
44+
### set Bar
6345

6446
```typescript
65-
public Foo: string;
47+
public static set Bar: string;
6648
```
6749

68-
### Type
50+
#### Type
6951

7052
string
7153

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,18 @@ export interface MyInterface {
300300
// }
301301
// }
302302

303-
// export class Hello {
304-
// /**
305-
// * This is a constructor
306-
// * @param arg This is an argument ;)
307-
// */
308-
// constructor(arg: string) { }
303+
export class Hello {
304+
/**
305+
* This is a constructor
306+
* @param arg This is an argument ;)
307+
*/
308+
constructor(arg: string) { }
309309

310-
// GetFoo(arg: number): string
311-
// GetFoo(arg: string): string
312-
// GetFoo(arg: string | number): string {
313-
// throw new Error("Method not implemented.");
314-
// }
310+
get Foo(): string {
311+
throw new Error("Method not implemented.");
312+
}
315313

316-
// public Foo: string;
317-
// }
314+
set Foo(arg: string) { }
315+
316+
public static set Bar(arg: string) { }
317+
}

packages/ts-docs-gen/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
"fast-glob": "^1.0.1",
2121
"fs-extra": "^5.0.0",
2222
"simplr-logger": "^1.0.1",
23-
"ts-extractor": "^3.0.3",
23+
"ts-extractor": "^3.1.1",
2424
"typescript": "^2.6.2"
2525
},
2626
"devDependencies": {
27-
"@types/jest": "^21.1.9",
27+
"@types/jest": "^22.0.0",
2828
"@types/sinon": "^4.1.2",
29-
"jest": "^22.0.3",
29+
"jest": "^22.0.4",
3030
"simplr-tslint": "0.0.1",
3131
"sinon": "^4.1.3",
3232
"ts-jest": "^22.0.0",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ApiClassPlugin } from "./plugins/api-class-plugin";
1111
import { ApiClassConstructorPlugin } from "./plugins/api-class-constructor-plugin";
1212
import { ApiClassMethodPlugin } from "./plugins/api-class-method-plugin";
1313
import { ApiClassPropertyPlugin } from "./plugins/api-class-property-plugin";
14+
import { ApiClassAccessorPlugin } from "./plugins/api-class-accessor-plugin";
1415

1516
export const DefaultPlugins = [
1617
new ApiSourceFilePlugin(),
@@ -25,5 +26,6 @@ export const DefaultPlugins = [
2526
new ApiClassPlugin(),
2627
new ApiClassConstructorPlugin(),
2728
new ApiClassMethodPlugin(),
28-
new ApiClassPropertyPlugin()
29+
new ApiClassPropertyPlugin(),
30+
new ApiClassAccessorPlugin()
2931
];

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,26 @@ export namespace GeneratorHelpers {
588588
return `${isReadOnlyString}${apiItem.Name}${isOptionalString}: ${returnTypeString}`;
589589
}
590590

591+
export function ApiAccessorToString(
592+
apiItem: Contracts.ApiGetAccessorDto | Contracts.ApiSetAccessorDto,
593+
type: Contracts.TypeDto | undefined,
594+
alias?: string
595+
): string {
596+
const name = alias || apiItem.Name;
597+
const abstract = apiItem.IsAbstract ? " abstract" : "";
598+
const $static = apiItem.IsStatic ? " static" : "";
599+
600+
const typeString = type != null ? type.Text : "???";
601+
let accessorType: string;
602+
if (apiItem.ApiKind === Contracts.ApiItemKinds.SetAccessor) {
603+
accessorType = "set";
604+
} else {
605+
accessorType = "get";
606+
}
607+
608+
return `${apiItem.AccessModifier}${$static}${abstract} ${accessorType} ${name}: ${typeString};`;
609+
}
610+
591611
// TODO: add description from @template jsdoc tag.
592612
export function ApiTypeParametersTableToString(typeParameters: Contracts.ApiTypeParameterDto[]): ReferenceDto<string[]> {
593613
if (typeParameters.length === 0) {
@@ -698,7 +718,11 @@ export namespace GeneratorHelpers {
698718
};
699719
}
700720

701-
export function MergePluginResultData<T extends PluginResultData>(a: T, b: Partial<PluginResultData>): T {
721+
export function MergePluginResultData<T extends PluginResultData>(a: T, b: Partial<PluginResultData> | undefined): T {
722+
if (b == null) {
723+
return a;
724+
}
725+
702726
a.Headings = a.Headings.concat(b.Headings || []);
703727
a.Members = (a.Members || []).concat(b.Members || []);
704728
a.Result = a.Result.concat(b.Result || []);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Contracts, ExtractDto } from "ts-extractor";
2+
import { Plugin, SupportedApiItemKindType, PluginOptions, PluginResult, PluginResultData } from "../contracts/plugin";
3+
import { GeneratorHelpers } from "../generator-helpers";
4+
import { MarkdownBuilder } from "@simplrjs/markdown";
5+
6+
export type Kind = Contracts.ApiSetAccessorDto | Contracts.ApiGetAccessorDto;
7+
8+
export class ApiClassAccessorPlugin implements Plugin<Kind> {
9+
public SupportedApiItemKinds(): SupportedApiItemKindType[] {
10+
return [
11+
GeneratorHelpers.ApiItemKinds.GetAccessor,
12+
GeneratorHelpers.ApiItemKinds.SetAccessor,
13+
];
14+
}
15+
16+
public CheckApiItem(item: Kind): boolean {
17+
return true;
18+
}
19+
20+
private getHeading(data: PluginOptions<Kind>): string {
21+
let accessorType: string;
22+
if (data.ApiItem.ApiKind === Contracts.ApiItemKinds.SetAccessor) {
23+
accessorType = "set";
24+
} else {
25+
accessorType = "get";
26+
}
27+
28+
return `${accessorType} ${data.Reference.Alias}`;
29+
}
30+
31+
private resolveType(apiItem: Kind, extractedData: ExtractDto): Contracts.TypeDto | undefined {
32+
// Resolve type
33+
let type: Contracts.TypeDto | undefined;
34+
if (apiItem.ApiKind === Contracts.ApiItemKinds.GetAccessor) {
35+
type = apiItem.Type;
36+
} else if (apiItem.Parameter != null) {
37+
const apiParameter = extractedData.Registry[apiItem.Parameter.Ids[0]] as Contracts.ApiParameterDto;
38+
if (apiParameter != null) {
39+
type = apiParameter.Type;
40+
}
41+
}
42+
43+
return type;
44+
}
45+
46+
private renderTypeDto(type: Contracts.TypeDto | undefined): Partial<PluginResultData> | undefined {
47+
if (type == null) {
48+
return undefined;
49+
}
50+
51+
const result = GeneratorHelpers.TypeDtoToMarkdownString(type);
52+
53+
const builder = new MarkdownBuilder()
54+
.EmptyLine()
55+
.Header("Type", 4)
56+
.EmptyLine()
57+
.Text(result.Text);
58+
59+
return {
60+
Result: builder.GetOutput(),
61+
UsedReferences: result.References
62+
};
63+
}
64+
65+
public Render(data: PluginOptions<Kind>): PluginResult {
66+
const heading = this.getHeading(data);
67+
const type = this.resolveType(data.ApiItem, data.ExtractedData);
68+
const pluginResult: PluginResult = {
69+
...GeneratorHelpers.GetDefaultPluginResultData(),
70+
ApiItem: data.ApiItem,
71+
Reference: data.Reference,
72+
Headings: [
73+
{
74+
ApiItemId: data.Reference.Id,
75+
Heading: heading
76+
}
77+
]
78+
};
79+
80+
pluginResult.Result = new MarkdownBuilder()
81+
.Header(heading, 3)
82+
.EmptyLine()
83+
.Text(GeneratorHelpers.RenderApiItemMetadata(data.ApiItem))
84+
.Code(GeneratorHelpers.ApiAccessorToString(data.ApiItem, type, data.Reference.Alias), GeneratorHelpers.DEFAULT_CODE_OPTIONS)
85+
.GetOutput();
86+
87+
// Type
88+
const typeResult = this.renderTypeDto(type);
89+
GeneratorHelpers.MergePluginResultData(pluginResult, typeResult);
90+
91+
return pluginResult;
92+
}
93+
}

0 commit comments

Comments
 (0)