|
| 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