|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information.s |
| 3 | + |
| 4 | +import { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem'; |
| 5 | +import { IExcerptTokenRange } from './Excerpt'; |
| 6 | +import { TypeParameter } from '../model/TypeParameter'; |
| 7 | +import { InternalError } from '@microsoft/node-core-library'; |
| 8 | +import { ApiDeclaredItem } from '../items/ApiDeclaredItem'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents parameter information that is part of {@link IApiTypeParameterListMixinOptions} |
| 12 | + * @public |
| 13 | + */ |
| 14 | +export interface IApiTypeParameterOptions { |
| 15 | + typeParameterName: string; |
| 16 | + constraintTokenRange?: IExcerptTokenRange; |
| 17 | + defaultTypeTokenRange?: IExcerptTokenRange; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Constructor options for {@link (ApiTypeParameterListMixin:interface)}. |
| 22 | + * @public |
| 23 | + */ |
| 24 | +export interface IApiTypeParameterListMixinOptions extends IApiItemOptions { |
| 25 | + typeParameters?: IApiTypeParameterOptions[]; |
| 26 | +} |
| 27 | + |
| 28 | +export interface IApiTypeParameterListMixinJson extends IApiItemJson { |
| 29 | + typeParameters?: IApiTypeParameterOptions[]; |
| 30 | +} |
| 31 | + |
| 32 | +const _typeParameters: unique symbol = Symbol('ApiTypeParameterListMixin._typeParameters'); |
| 33 | + |
| 34 | +/** |
| 35 | + * The mixin base class for API items that can have type parameters. |
| 36 | + * |
| 37 | + * @remarks |
| 38 | + * |
| 39 | + * This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of |
| 40 | + * API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use |
| 41 | + * TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various |
| 42 | + * features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class |
| 43 | + * to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components: |
| 44 | + * the function that generates a subclass, an interface that describes the members of the subclass, and |
| 45 | + * a namespace containing static members of the class. |
| 46 | + * |
| 47 | + * @public |
| 48 | + */ |
| 49 | +// tslint:disable-next-line:interface-name |
| 50 | +export interface ApiTypeParameterListMixin extends ApiItem { |
| 51 | + /** |
| 52 | + * The type parameters. |
| 53 | + */ |
| 54 | + readonly typeParameters: ReadonlyArray<TypeParameter>; |
| 55 | + |
| 56 | + serializeInto(jsonObject: Partial<IApiItemJson>): void; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Mixin function for {@link (ApiTypeParameterListMixin:interface)}. |
| 61 | + * |
| 62 | + * @param baseClass - The base class to be extended |
| 63 | + * @returns A child class that extends baseClass, adding the {@link (ApiTypeParameterListMixin:interface)} |
| 64 | + * functionality. |
| 65 | + * |
| 66 | + * @public |
| 67 | + */ |
| 68 | +export function ApiTypeParameterListMixin<TBaseClass extends IApiItemConstructor>(baseClass: TBaseClass): |
| 69 | + TBaseClass & (new (...args: any[]) => ApiTypeParameterListMixin) { // tslint:disable-line:no-any |
| 70 | + |
| 71 | + abstract class MixedClass extends baseClass implements ApiTypeParameterListMixin { |
| 72 | + public readonly [_typeParameters]: TypeParameter[]; |
| 73 | + |
| 74 | + /** @override */ |
| 75 | + public static onDeserializeInto(options: Partial<IApiTypeParameterListMixinOptions>, |
| 76 | + jsonObject: IApiTypeParameterListMixinJson): void { |
| 77 | + |
| 78 | + baseClass.onDeserializeInto(options, jsonObject); |
| 79 | + |
| 80 | + options.typeParameters = jsonObject.typeParameters || []; |
| 81 | + } |
| 82 | + |
| 83 | + // tslint:disable-next-line:no-any |
| 84 | + constructor(...args: any[]) { |
| 85 | + super(...args); |
| 86 | + |
| 87 | + const options: IApiTypeParameterListMixinOptions = args[0]; |
| 88 | + |
| 89 | + this[_typeParameters] = []; |
| 90 | + |
| 91 | + if (this instanceof ApiDeclaredItem) { |
| 92 | + if (options.typeParameters) { |
| 93 | + for (const typeParameterOptions of options.typeParameters) { |
| 94 | + |
| 95 | + const typeParameter: TypeParameter = new TypeParameter({ |
| 96 | + name: typeParameterOptions.typeParameterName, |
| 97 | + constraintExcerpt: typeParameterOptions.constraintTokenRange && |
| 98 | + this.buildExcerpt(typeParameterOptions.constraintTokenRange), |
| 99 | + defaultTypeExcerpt: typeParameterOptions.defaultTypeTokenRange && |
| 100 | + this.buildExcerpt(typeParameterOptions.defaultTypeTokenRange), |
| 101 | + parent: this |
| 102 | + }); |
| 103 | + |
| 104 | + this[_typeParameters].push(typeParameter); |
| 105 | + } |
| 106 | + } |
| 107 | + } else { |
| 108 | + throw new InternalError('ApiTypeParameterListMixin expects a base class that inherits from ApiDeclaredItem'); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public get typeParameters(): ReadonlyArray<TypeParameter> { |
| 113 | + return this[_typeParameters]; |
| 114 | + } |
| 115 | + |
| 116 | + /** @override */ |
| 117 | + public serializeInto(jsonObject: Partial<IApiTypeParameterListMixinJson>): void { |
| 118 | + super.serializeInto(jsonObject); |
| 119 | + |
| 120 | + const typeParameterObjects: IApiTypeParameterOptions[] = []; |
| 121 | + for (const typeParameter of this.typeParameters) { |
| 122 | + const typeParameterOptions: IApiTypeParameterOptions = { |
| 123 | + typeParameterName: typeParameter.name |
| 124 | + }; |
| 125 | + if (typeParameter.constraintExcerpt) { |
| 126 | + typeParameterOptions.constraintTokenRange = typeParameter.constraintExcerpt.tokenRange; |
| 127 | + } |
| 128 | + if (typeParameter.defaultTypeExcerpt) { |
| 129 | + typeParameterOptions.defaultTypeTokenRange = typeParameter.defaultTypeExcerpt.tokenRange; |
| 130 | + } |
| 131 | + typeParameterObjects.push(typeParameterOptions); |
| 132 | + } |
| 133 | + |
| 134 | + if (typeParameterObjects.length > 0) { |
| 135 | + jsonObject.typeParameters = typeParameterObjects; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return MixedClass; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Static members for {@link (ApiTypeParameterListMixin:interface)}. |
| 145 | + * @public |
| 146 | + */ |
| 147 | +export namespace ApiTypeParameterListMixin { |
| 148 | + /** |
| 149 | + * A type guard that tests whether the specified `ApiItem` subclass extends the `ApiParameterListMixin` mixin. |
| 150 | + * |
| 151 | + * @remarks |
| 152 | + * |
| 153 | + * The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of |
| 154 | + * the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however |
| 155 | + * the TypeScript type system cannot invoke a runtime test.) |
| 156 | + */ |
| 157 | + export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiTypeParameterListMixin { |
| 158 | + return apiItem.hasOwnProperty(_typeParameters); |
| 159 | + } |
| 160 | +} |
0 commit comments