mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +01:00
build: package api-extractor and -model (#9920)
* fix(ExceptText): don't display import("d..-types/v10"). in return type
* Squashed 'packages/api-extractor-model/' content from commit 39ecb196c
git-subtree-dir: packages/api-extractor-model
git-subtree-split: 39ecb196ca210bdf84ba6c9cadb1bb93571849d7
* Squashed 'packages/api-extractor/' content from commit 341ad6c51
git-subtree-dir: packages/api-extractor
git-subtree-split: 341ad6c51b01656d4f73b74ad4bdb3095f9262c4
* feat(api-extractor): add api-extractor and -model
* fix: package.json docs script
* fix(SourcLink): use <> instead of function syntax
* fix: make packages private
* fix: rest params showing in docs, added labels
* fix: missed two files
* fix: cpy-cli & pnpm-lock
* fix: increase icon size
* fix: icon size again
This commit is contained in:
115
packages/api-extractor-model/src/mixins/ApiAbstractMixin.ts
Normal file
115
packages/api-extractor-model/src/mixins/ApiAbstractMixin.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiAbstractMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiAbstractMixinOptions extends IApiItemOptions {
|
||||
isAbstract: boolean;
|
||||
}
|
||||
|
||||
export interface IApiAbstractMixinJson extends IApiItemJson {
|
||||
isAbstract: boolean;
|
||||
}
|
||||
|
||||
const _isAbstract: unique symbol = Symbol('ApiAbstractMixin._isAbstract');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that have an abstract modifier.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiAbstractMixin extends ApiItem {
|
||||
/**
|
||||
* Indicates that the API item's value has an 'abstract' modifier.
|
||||
*/
|
||||
readonly isAbstract: boolean;
|
||||
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiAbstractMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiAbstractMixin:interface)}
|
||||
* functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiAbstractMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiAbstractMixin) {
|
||||
class MixedClass extends baseClass implements ApiAbstractMixin {
|
||||
public [_isAbstract]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiAbstractMixinOptions = args[0];
|
||||
this[_isAbstract] = options.isAbstract;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiAbstractMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiAbstractMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.isAbstract = jsonObject.isAbstract || false;
|
||||
}
|
||||
|
||||
public get isAbstract(): boolean {
|
||||
return this[_isAbstract];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiAbstractMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.isAbstract = this.isAbstract;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiAbstractMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiAbstractMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiAbstractMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiAbstractMixin {
|
||||
return apiItem.hasOwnProperty(_isAbstract);
|
||||
}
|
||||
}
|
||||
143
packages/api-extractor-model/src/mixins/ApiExportedMixin.ts
Normal file
143
packages/api-extractor-model/src/mixins/ApiExportedMixin.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import { Navigation } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiExportedMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiExportedMixinOptions extends IApiItemOptions {
|
||||
isExported: boolean;
|
||||
}
|
||||
|
||||
export interface IApiExportedMixinJson extends IApiItemJson {
|
||||
isExported: boolean;
|
||||
}
|
||||
|
||||
const _isExported: unique symbol = Symbol('ApiExportedMixin._isExported');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can be exported.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiExportedMixin extends ApiItem {
|
||||
/**
|
||||
* Whether the declaration is exported from its parent item container (i.e. either an `ApiEntryPoint` or an
|
||||
* `ApiNamespace`).
|
||||
*
|
||||
* @remarks
|
||||
* Suppose `index.ts` is your entry point:
|
||||
*
|
||||
* ```ts
|
||||
* // index.ts
|
||||
*
|
||||
* export class A {}
|
||||
* class B {}
|
||||
*
|
||||
* namespace n {
|
||||
* export class C {}
|
||||
* class D {}
|
||||
* }
|
||||
*
|
||||
* // file.ts
|
||||
* export class E {}
|
||||
* ```
|
||||
*
|
||||
* Classes `A` and `C` are both exported, while classes `B`, `D`, and `E` are not. `E` is exported from its
|
||||
* local file, but not from its parent item container (i.e. the entry point).
|
||||
*/
|
||||
readonly isExported: boolean;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiExportedMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiExportedMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiExportedMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiExportedMixin) {
|
||||
class MixedClass extends baseClass implements ApiExportedMixin {
|
||||
public [_isExported]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiExportedMixinOptions = args[0];
|
||||
this[_isExported] = options.isExported;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiExportedMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiExportedMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
const declarationReference: DeclarationReference = DeclarationReference.parse(jsonObject.canonicalReference);
|
||||
options.isExported = declarationReference.navigation === (Navigation.Exports as any); // ambient const enums suck...
|
||||
}
|
||||
|
||||
public get isExported(): boolean {
|
||||
return this[_isExported];
|
||||
}
|
||||
|
||||
/**
|
||||
* The `isExported` property is intentionally not serialized because the information is already present
|
||||
* in the item's `canonicalReference`.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiExportedMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiExportedMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiExportedMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiExportedMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiExportedMixin {
|
||||
return apiItem.hasOwnProperty(_isExported);
|
||||
}
|
||||
}
|
||||
130
packages/api-extractor-model/src/mixins/ApiInitializerMixin.ts
Normal file
130
packages/api-extractor-model/src/mixins/ApiInitializerMixin.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { InternalError } from '@rushstack/node-core-library';
|
||||
import { ApiDeclaredItem } from '../items/ApiDeclaredItem.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
import type { IExcerptTokenRange, Excerpt } from './Excerpt.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiInitializerMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiInitializerMixinOptions extends IApiItemOptions {
|
||||
initializerTokenRange?: IExcerptTokenRange | undefined;
|
||||
}
|
||||
|
||||
export interface IApiInitializerMixinJson extends IApiItemJson {
|
||||
initializerTokenRange?: IExcerptTokenRange;
|
||||
}
|
||||
|
||||
const _initializerExcerpt: unique symbol = Symbol('ApiInitializerMixin._initializerExcerpt');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can have an initializer.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiInitializerMixin extends ApiItem {
|
||||
/**
|
||||
* An {@link Excerpt} that describes the item's initializer.
|
||||
*/
|
||||
readonly initializerExcerpt?: Excerpt | undefined;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiInitializerMixinJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiInitializerMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiInitializerMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiInitializerMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiInitializerMixin) {
|
||||
class MixedClass extends baseClass implements ApiInitializerMixin {
|
||||
public [_initializerExcerpt]?: Excerpt;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiInitializerMixinOptions = args[0];
|
||||
|
||||
if (this instanceof ApiDeclaredItem) {
|
||||
if (options.initializerTokenRange) {
|
||||
this[_initializerExcerpt] = this.buildExcerpt(options.initializerTokenRange);
|
||||
}
|
||||
} else {
|
||||
throw new InternalError('ApiInitializerMixin expects a base class that inherits from ApiDeclaredItem');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiInitializerMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiInitializerMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.initializerTokenRange = jsonObject.initializerTokenRange;
|
||||
}
|
||||
|
||||
public get initializerExcerpt(): Excerpt | undefined {
|
||||
return this[_initializerExcerpt];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiInitializerMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
// Note that JSON does not support the "undefined" value, so we simply omit the field entirely if it is undefined
|
||||
if (this.initializerExcerpt) {
|
||||
jsonObject.initializerTokenRange = this.initializerExcerpt.tokenRange;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiInitializerMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiInitializerMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiInitializerMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiInitializerMixin {
|
||||
return apiItem.hasOwnProperty(_initializerExcerpt);
|
||||
}
|
||||
}
|
||||
562
packages/api-extractor-model/src/mixins/ApiItemContainerMixin.ts
Normal file
562
packages/api-extractor-model/src/mixins/ApiItemContainerMixin.ts
Normal file
@@ -0,0 +1,562 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference.js';
|
||||
import { InternalError } from '@rushstack/node-core-library';
|
||||
import {
|
||||
ApiItem,
|
||||
apiItem_onParentChanged,
|
||||
type IApiItemJson,
|
||||
type IApiItemOptions,
|
||||
type IApiItemConstructor,
|
||||
ApiItemKind,
|
||||
} from '../items/ApiItem.js';
|
||||
import type { ApiClass } from '../model/ApiClass.js';
|
||||
import type { ApiInterface } from '../model/ApiInterface.js';
|
||||
import type { ApiModel } from '../model/ApiModel.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
import type { HeritageType } from '../model/HeritageType.js';
|
||||
import type { IResolveDeclarationReferenceResult } from '../model/ModelReferenceResolver.js';
|
||||
import { ApiNameMixin } from './ApiNameMixin.js';
|
||||
import { type ExcerptToken, ExcerptTokenKind } from './Excerpt.js';
|
||||
import { type IFindApiItemsResult, type IFindApiItemsMessage, FindApiItemsMessageId } from './IFindApiItemsResult.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiItemContainerMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiItemContainerMixinOptions extends IApiItemOptions {
|
||||
members?: ApiItem[] | undefined;
|
||||
preserveMemberOrder?: boolean | undefined;
|
||||
}
|
||||
|
||||
export interface IApiItemContainerJson extends IApiItemJson {
|
||||
members: IApiItemJson[];
|
||||
preserveMemberOrder?: boolean;
|
||||
}
|
||||
|
||||
const _members: unique symbol = Symbol('ApiItemContainerMixin._members');
|
||||
const _membersSorted: unique symbol = Symbol('ApiItemContainerMixin._membersSorted');
|
||||
const _membersByContainerKey: unique symbol = Symbol('ApiItemContainerMixin._membersByContainerKey');
|
||||
const _membersByName: unique symbol = Symbol('ApiItemContainerMixin._membersByName');
|
||||
const _membersByKind: unique symbol = Symbol('ApiItemContainerMixin._membersByKind');
|
||||
const _preserveMemberOrder: unique symbol = Symbol('ApiItemContainerMixin._preserveMemberOrder');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that act as containers for other child items.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
*
|
||||
* Examples of `ApiItemContainerMixin` child classes include `ApiModel`, `ApiPackage`, `ApiEntryPoint`,
|
||||
* and `ApiEnum`. But note that `Parameter` is not considered a "member" of an `ApiMethod`; this relationship
|
||||
* is modeled using {@link (ApiParameterListMixin:interface).parameters} instead
|
||||
* of {@link ApiItem.members}.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiItemContainerMixin extends ApiItem {
|
||||
/**
|
||||
* For a given member of this container, return its `ApiItem.getMergedSiblings()` list.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
_getMergedSiblingsForMember(memberApiItem: ApiItem): readonly ApiItem[];
|
||||
|
||||
/**
|
||||
* Adds a new member to the container.
|
||||
*
|
||||
* @remarks
|
||||
* An ApiItem cannot be added to more than one container.
|
||||
*/
|
||||
addMember(member: ApiItem): void;
|
||||
|
||||
/**
|
||||
* Returns a list of members with the specified name.
|
||||
*/
|
||||
findMembersByName(name: string): readonly ApiItem[];
|
||||
|
||||
/**
|
||||
* Finds all of the ApiItem's immediate and inherited members by walking up the inheritance tree.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* Given the following class heritage:
|
||||
*
|
||||
* ```
|
||||
* export class A {
|
||||
* public a: number|boolean;
|
||||
* }
|
||||
*
|
||||
* export class B extends A {
|
||||
* public a: number;
|
||||
* public b: string;
|
||||
* }
|
||||
*
|
||||
* export class C extends B {
|
||||
* public c: boolean;
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* Calling `findMembersWithInheritance` on `C` will return `B.a`, `B.b`, and `C.c`. Calling the
|
||||
* method on `B` will return `B.a` and `B.b`. And calling the method on `A` will return just
|
||||
* `A.a`.
|
||||
*
|
||||
* The inherited members returned by this method may be incomplete. If so, there will be a flag
|
||||
* on the result object indicating this as well as messages explaining the errors in more detail.
|
||||
* Some scenarios include:
|
||||
*
|
||||
* - Interface extending from a type alias.
|
||||
*
|
||||
* - Class extending from a variable.
|
||||
*
|
||||
* - Extending from a declaration not present in the model (e.g. external package).
|
||||
*
|
||||
* - Extending from an unexported declaration (e.g. ae-forgotten-export). Common in mixin
|
||||
* patterns.
|
||||
*
|
||||
* - Unexpected runtime errors...
|
||||
*
|
||||
* Lastly, be aware that the types of inherited members are returned with respect to their
|
||||
* defining class as opposed to with respect to the inheriting class. For example, consider
|
||||
* the following:
|
||||
*
|
||||
* ```
|
||||
* export class A<T> {
|
||||
* public a: T;
|
||||
* }
|
||||
*
|
||||
* export class B extends A<number> {}
|
||||
* ```
|
||||
*
|
||||
* When called on `B`, this method will return `B.a` with type `T` as opposed to type
|
||||
* `number`, although the latter is more accurate.
|
||||
*/
|
||||
findMembersWithInheritance(): IFindApiItemsResult;
|
||||
|
||||
/**
|
||||
* Disables automatic sorting of {@link ApiItem.members}.
|
||||
*
|
||||
* @remarks
|
||||
* By default `ApiItemContainerMixin` will automatically sort its members according to their
|
||||
* {@link ApiItem.getSortKey} string, which provides a standardized mostly alphabetical ordering
|
||||
* that is appropriate for most API items. When loading older .api.json files the automatic sorting
|
||||
* is reapplied and may update the ordering.
|
||||
*
|
||||
* Set `preserveMemberOrder` to true to disable automatic sorting for this container; instead, the
|
||||
* members will retain whatever ordering appeared in the {@link IApiItemContainerMixinOptions.members} array.
|
||||
* The `preserveMemberOrder` option is saved in the .api.json file.
|
||||
*/
|
||||
readonly preserveMemberOrder: boolean;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
|
||||
/**
|
||||
* Attempts to retrieve a member using its containerKey, or returns `undefined` if no matching member was found.
|
||||
*
|
||||
* @remarks
|
||||
* Use the `getContainerKey()` static member to construct the key. Each subclass has a different implementation
|
||||
* of this function, according to the aspects that are important for identifying it.
|
||||
*
|
||||
* See {@link ApiItem.containerKey} for more information.
|
||||
*/
|
||||
tryGetMemberByKey(containerKey: string): ApiItem | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link ApiDeclaredItem}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiItemContainerMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiItemContainerMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiItemContainerMixin) {
|
||||
class MixedClass extends baseClass implements ApiItemContainerMixin {
|
||||
public readonly [_members]: ApiItem[];
|
||||
|
||||
public [_membersSorted]: boolean;
|
||||
|
||||
public [_membersByContainerKey]: Map<string, ApiItem>;
|
||||
|
||||
public [_preserveMemberOrder]: boolean;
|
||||
|
||||
// For members of this container that extend ApiNameMixin, this stores the list of members with a given name.
|
||||
// Examples include merged declarations, overloaded functions, etc.
|
||||
public [_membersByName]: Map<string, ApiItem[]> | undefined;
|
||||
|
||||
// For members of this container that do NOT extend ApiNameMixin, this stores the list of members
|
||||
// that share a common ApiItemKind. Examples include overloaded constructors or index signatures.
|
||||
public [_membersByKind]: Map<string, ApiItem[]> | undefined; // key is ApiItemKind
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
const options: IApiItemContainerMixinOptions = args[0] as IApiItemContainerMixinOptions;
|
||||
|
||||
this[_members] = [];
|
||||
this[_membersSorted] = false;
|
||||
this[_membersByContainerKey] = new Map<string, ApiItem>();
|
||||
this[_preserveMemberOrder] = options.preserveMemberOrder ?? false;
|
||||
|
||||
if (options.members) {
|
||||
for (const member of options.members) {
|
||||
this.addMember(member);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiItemContainerMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiItemContainerJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
options.preserveMemberOrder = jsonObject.preserveMemberOrder;
|
||||
options.members = [];
|
||||
for (const memberObject of jsonObject.members) {
|
||||
options.members.push(ApiItem.deserialize(memberObject, context));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override get members(): readonly ApiItem[] {
|
||||
if (!this[_membersSorted] && !this[_preserveMemberOrder]) {
|
||||
this[_members].sort((x, y) => x.getSortKey().localeCompare(y.getSortKey()));
|
||||
this[_membersSorted] = true;
|
||||
}
|
||||
|
||||
return this[_members];
|
||||
}
|
||||
|
||||
public get preserveMemberOrder(): boolean {
|
||||
return this[_preserveMemberOrder];
|
||||
}
|
||||
|
||||
public addMember(member: ApiItem): void {
|
||||
if (this[_membersByContainerKey].has(member.containerKey)) {
|
||||
throw new Error(
|
||||
`Another member has already been added with the same name (${member.displayName})` +
|
||||
` and containerKey (${member.containerKey})`,
|
||||
);
|
||||
}
|
||||
|
||||
const existingParent: ApiItem | undefined = member.parent;
|
||||
if (existingParent !== undefined) {
|
||||
throw new Error(`This item has already been added to another container: "${existingParent.displayName}"`);
|
||||
}
|
||||
|
||||
this[_members].push(member);
|
||||
this[_membersByName] = undefined; // invalidate the lookup
|
||||
this[_membersByKind] = undefined; // invalidate the lookup
|
||||
this[_membersSorted] = false;
|
||||
this[_membersByContainerKey].set(member.containerKey, member);
|
||||
|
||||
member[apiItem_onParentChanged](this);
|
||||
}
|
||||
|
||||
public tryGetMemberByKey(containerKey: string): ApiItem | undefined {
|
||||
return this[_membersByContainerKey].get(containerKey);
|
||||
}
|
||||
|
||||
public findMembersByName(name: string): readonly ApiItem[] {
|
||||
this._ensureMemberMaps();
|
||||
return this[_membersByName]!.get(name) ?? [];
|
||||
}
|
||||
|
||||
public findMembersWithInheritance(): IFindApiItemsResult {
|
||||
const messages: IFindApiItemsMessage[] = [];
|
||||
let maybeIncompleteResult = false;
|
||||
|
||||
// For API items that don't support inheritance, this method just returns the item's
|
||||
// immediate members.
|
||||
switch (this.kind) {
|
||||
case ApiItemKind.Class:
|
||||
case ApiItemKind.Interface:
|
||||
break;
|
||||
default: {
|
||||
return {
|
||||
items: this.members.concat(),
|
||||
messages,
|
||||
maybeIncompleteResult,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const membersByName: Map<string, ApiItem[]> = new Map();
|
||||
const membersByKind: Map<ApiItemKind, ApiItem[]> = new Map();
|
||||
|
||||
const toVisit: ApiItem[] = [];
|
||||
let next: ApiItem | undefined = this;
|
||||
|
||||
while (next) {
|
||||
const membersToAdd: ApiItem[] = [];
|
||||
|
||||
// For each member, check to see if we've already seen a member with the same name
|
||||
// previously in the inheritance tree. If so, we know we won't inherit it, and thus
|
||||
// do not add it to our `membersToAdd` array.
|
||||
for (const member of next.members) {
|
||||
// We add the to-be-added members to an intermediate array instead of immediately
|
||||
// to the maps themselves to support method overloads with the same name.
|
||||
if (ApiNameMixin.isBaseClassOf(member)) {
|
||||
if (!membersByName.has(member.name)) {
|
||||
membersToAdd.push(member);
|
||||
}
|
||||
} else if (!membersByKind.has(member.kind)) {
|
||||
membersToAdd.push(member);
|
||||
}
|
||||
}
|
||||
|
||||
for (const member of membersToAdd) {
|
||||
if (ApiNameMixin.isBaseClassOf(member)) {
|
||||
const members: ApiItem[] = membersByName.get(member.name) ?? [];
|
||||
members.push(member);
|
||||
membersByName.set(member.name, members);
|
||||
} else {
|
||||
const members: ApiItem[] = membersByKind.get(member.kind) ?? [];
|
||||
members.push(member);
|
||||
membersByKind.set(member.kind, members);
|
||||
}
|
||||
}
|
||||
|
||||
// Interfaces can extend multiple interfaces, so iterate through all of them.
|
||||
const extendedItems: ApiItem[] = [];
|
||||
let extendsTypes: readonly HeritageType[] | undefined;
|
||||
|
||||
switch (next.kind) {
|
||||
case ApiItemKind.Class: {
|
||||
const apiClass: ApiClass = next as ApiClass;
|
||||
extendsTypes = apiClass.extendsType ? [apiClass.extendsType] : [];
|
||||
break;
|
||||
}
|
||||
|
||||
case ApiItemKind.Interface: {
|
||||
const apiInterface: ApiInterface = next as ApiInterface;
|
||||
extendsTypes = apiInterface.extendsTypes;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (extendsTypes === undefined) {
|
||||
messages.push({
|
||||
messageId: FindApiItemsMessageId.UnsupportedKind,
|
||||
text: `Unable to analyze references of API item ${next.displayName} because it is of unsupported kind ${next.kind}`,
|
||||
});
|
||||
maybeIncompleteResult = true;
|
||||
next = toVisit.shift();
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const extendsType of extendsTypes) {
|
||||
// We want to find the reference token associated with the actual inherited declaration.
|
||||
// In every case we support, this is the first reference token. For example:
|
||||
//
|
||||
// ```
|
||||
// export class A extends B {}
|
||||
// ^
|
||||
// export class A extends B<C> {}
|
||||
// ^
|
||||
// export class A extends B.C {}
|
||||
// ^^^
|
||||
// ```
|
||||
const firstReferenceToken: ExcerptToken | undefined = extendsType.excerpt.spannedTokens.find(
|
||||
(token: ExcerptToken) => {
|
||||
return token.kind === ExcerptTokenKind.Reference && token.canonicalReference;
|
||||
},
|
||||
);
|
||||
|
||||
if (!firstReferenceToken) {
|
||||
messages.push({
|
||||
messageId: FindApiItemsMessageId.ExtendsClauseMissingReference,
|
||||
text: `Unable to analyze extends clause ${extendsType.excerpt.text} of API item ${next.displayName} because no canonical reference was found`,
|
||||
});
|
||||
maybeIncompleteResult = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
const apiModel: ApiModel | undefined = this.getAssociatedModel();
|
||||
if (!apiModel) {
|
||||
messages.push({
|
||||
messageId: FindApiItemsMessageId.NoAssociatedApiModel,
|
||||
text: `Unable to analyze references of API item ${next.displayName} because it is not associated with an ApiModel`,
|
||||
});
|
||||
maybeIncompleteResult = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
const canonicalReference: DeclarationReference = firstReferenceToken.canonicalReference!;
|
||||
const apiItemResult: IResolveDeclarationReferenceResult = apiModel.resolveDeclarationReference(
|
||||
canonicalReference,
|
||||
undefined,
|
||||
);
|
||||
|
||||
const apiItem: ApiItem | undefined = apiItemResult.resolvedApiItem;
|
||||
if (!apiItem) {
|
||||
messages.push({
|
||||
messageId: FindApiItemsMessageId.DeclarationResolutionFailed,
|
||||
text: `Unable to resolve declaration reference within API item ${next.displayName}: ${apiItemResult.errorMessage}`,
|
||||
});
|
||||
maybeIncompleteResult = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
extendedItems.push(apiItem);
|
||||
}
|
||||
|
||||
// For classes, this array will only have one item. For interfaces, there may be multiple items. Sort the array
|
||||
// into alphabetical order before adding to our list of API items to visit. This ensures that in the case
|
||||
// of multiple interface inheritance, a member inherited from multiple interfaces is attributed to the interface
|
||||
// earlier in alphabetical order (as opposed to source order).
|
||||
//
|
||||
// For example, in the code block below, `Bar.x` is reported as the inherited item, not `Foo.x`.
|
||||
//
|
||||
// ```
|
||||
// interface Foo {
|
||||
// public x: string;
|
||||
// }
|
||||
//
|
||||
// interface Bar {
|
||||
// public x: string;
|
||||
// }
|
||||
//
|
||||
// interface FooBar extends Foo, Bar {}
|
||||
// ```
|
||||
extendedItems.sort((x: ApiItem, y: ApiItem) => x.getSortKey().localeCompare(y.getSortKey()));
|
||||
|
||||
toVisit.push(...extendedItems);
|
||||
next = toVisit.shift();
|
||||
}
|
||||
|
||||
const items: ApiItem[] = [];
|
||||
for (const members of membersByName.values()) {
|
||||
items.push(...members);
|
||||
}
|
||||
|
||||
for (const members of membersByKind.values()) {
|
||||
items.push(...members);
|
||||
}
|
||||
|
||||
items.sort((x: ApiItem, y: ApiItem) => x.getSortKey().localeCompare(y.getSortKey()));
|
||||
|
||||
return {
|
||||
items,
|
||||
messages,
|
||||
maybeIncompleteResult,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public _getMergedSiblingsForMember(memberApiItem: ApiItem): readonly ApiItem[] {
|
||||
this._ensureMemberMaps();
|
||||
let result: ApiItem[] | undefined;
|
||||
if (ApiNameMixin.isBaseClassOf(memberApiItem)) {
|
||||
result = this[_membersByName]!.get(memberApiItem.name);
|
||||
} else {
|
||||
result = this[_membersByKind]!.get(memberApiItem.kind);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
throw new InternalError('Item was not found in the _membersByName/_membersByKind lookup');
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public _ensureMemberMaps(): void {
|
||||
// Build the _membersByName and _membersByKind tables if they don't already exist
|
||||
if (this[_membersByName] === undefined) {
|
||||
const membersByName: Map<string, ApiItem[]> = new Map<string, ApiItem[]>();
|
||||
const membersByKind: Map<string, ApiItem[]> = new Map<string, ApiItem[]>();
|
||||
|
||||
for (const member of this[_members]) {
|
||||
let map: Map<ApiItemKind, ApiItem[]> | Map<string, ApiItem[]>;
|
||||
let key: ApiItemKind | string;
|
||||
|
||||
if (ApiNameMixin.isBaseClassOf(member)) {
|
||||
map = membersByName;
|
||||
key = member.name;
|
||||
} else {
|
||||
map = membersByKind;
|
||||
key = member.kind;
|
||||
}
|
||||
|
||||
let list: ApiItem[] | undefined = map.get(key);
|
||||
if (list === undefined) {
|
||||
list = [];
|
||||
map.set(key, list);
|
||||
}
|
||||
|
||||
list.push(member);
|
||||
}
|
||||
|
||||
this[_membersByName] = membersByName;
|
||||
this[_membersByKind] = membersByKind;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiItemContainerJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
const memberObjects: IApiItemJson[] = [];
|
||||
|
||||
for (const member of this.members) {
|
||||
const memberJsonObject: Partial<IApiItemJson> = {};
|
||||
member.serializeInto(memberJsonObject);
|
||||
memberObjects.push(memberJsonObject as IApiItemJson);
|
||||
}
|
||||
|
||||
jsonObject.preserveMemberOrder = this.preserveMemberOrder;
|
||||
jsonObject.members = memberObjects;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiItemContainerMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
|
||||
export namespace ApiItemContainerMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiItemContainerMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiItemContainerMixin {
|
||||
return apiItem.hasOwnProperty(_members);
|
||||
}
|
||||
}
|
||||
128
packages/api-extractor-model/src/mixins/ApiNameMixin.ts
Normal file
128
packages/api-extractor-model/src/mixins/ApiNameMixin.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiNameMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiNameMixinOptions extends IApiItemOptions {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface IApiNameMixinJson extends IApiItemJson {
|
||||
name: string;
|
||||
}
|
||||
|
||||
const _name: unique symbol = Symbol('ApiNameMixin._name');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that have a name. For example, a class has a name, but a class constructor
|
||||
* does not.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiNameMixin extends ApiItem {
|
||||
/**
|
||||
* The exported name of this API item.
|
||||
*
|
||||
* @remarks
|
||||
* Note that due tue type aliasing, the exported name may be different from the locally declared name.
|
||||
*/
|
||||
readonly name: string;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiNameMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiNameMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiNameMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiNameMixin) {
|
||||
class MixedClass extends baseClass implements ApiNameMixin {
|
||||
public readonly [_name]: string;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiNameMixinOptions = args[0];
|
||||
this[_name] = options.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiNameMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiNameMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.name = jsonObject.name;
|
||||
}
|
||||
|
||||
public get name(): string {
|
||||
return this[_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override get displayName(): string {
|
||||
return this[_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiNameMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.name = this.name;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiNameMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiNameMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiNameMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiNameMixin {
|
||||
return apiItem.hasOwnProperty(_name);
|
||||
}
|
||||
}
|
||||
127
packages/api-extractor-model/src/mixins/ApiOptionalMixin.ts
Normal file
127
packages/api-extractor-model/src/mixins/ApiOptionalMixin.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiOptionalMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiOptionalMixinOptions extends IApiItemOptions {
|
||||
isOptional: boolean;
|
||||
}
|
||||
|
||||
export interface IApiOptionalMixinJson extends IApiItemJson {
|
||||
isOptional: boolean;
|
||||
}
|
||||
|
||||
const _isOptional: unique symbol = Symbol('ApiOptionalMixin._isOptional');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can be marked as optional by appending a `?` to them.
|
||||
* For example, a property of an interface can be optional.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiOptionalMixin extends ApiItem {
|
||||
/**
|
||||
* True if this is an optional property.
|
||||
*
|
||||
* @remarks
|
||||
* For example:
|
||||
* ```ts
|
||||
* interface X {
|
||||
* y: string; // not optional
|
||||
* z?: string; // optional
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
readonly isOptional: boolean;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiOptionalMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiOptionalMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiOptionalMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiOptionalMixin) {
|
||||
class MixedClass extends baseClass implements ApiOptionalMixin {
|
||||
public [_isOptional]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiOptionalMixinOptions = args[0];
|
||||
this[_isOptional] = Boolean(options.isOptional);
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiOptionalMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiOptionalMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.isOptional = Boolean(jsonObject.isOptional);
|
||||
}
|
||||
|
||||
public get isOptional(): boolean {
|
||||
return this[_isOptional];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiOptionalMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.isOptional = this.isOptional;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional members for {@link (ApiOptionalMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiOptionalMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiOptionalMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiOptionalMixin {
|
||||
return apiItem.hasOwnProperty(_isOptional);
|
||||
}
|
||||
}
|
||||
202
packages/api-extractor-model/src/mixins/ApiParameterListMixin.ts
Normal file
202
packages/api-extractor-model/src/mixins/ApiParameterListMixin.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { InternalError } from '@rushstack/node-core-library';
|
||||
import { ApiDeclaredItem } from '../items/ApiDeclaredItem.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
import { Parameter } from '../model/Parameter.js';
|
||||
import type { IExcerptTokenRange } from './Excerpt.js';
|
||||
|
||||
/**
|
||||
* Represents parameter information that is part of {@link IApiParameterListMixinOptions}
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiParameterOptions {
|
||||
isOptional: boolean;
|
||||
isRest: boolean;
|
||||
parameterName: string;
|
||||
parameterTypeTokenRange: IExcerptTokenRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiParameterListMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiParameterListMixinOptions extends IApiItemOptions {
|
||||
overloadIndex: number;
|
||||
parameters: IApiParameterOptions[];
|
||||
}
|
||||
|
||||
export interface IApiParameterListJson extends IApiItemJson {
|
||||
overloadIndex: number;
|
||||
parameters: IApiParameterOptions[];
|
||||
}
|
||||
|
||||
const _overloadIndex: unique symbol = Symbol('ApiParameterListMixin._overloadIndex');
|
||||
const _parameters: unique symbol = Symbol('ApiParameterListMixin._parameters');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can have function parameters (but not necessarily a return value).
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiParameterListMixin extends ApiItem {
|
||||
/**
|
||||
* When a function has multiple overloaded declarations, this one-based integer index can be used to uniquely
|
||||
* identify them.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* Consider this overloaded declaration:
|
||||
*
|
||||
* ```ts
|
||||
* export namespace Versioning {
|
||||
* // TSDoc: Versioning.(addVersions:1)
|
||||
* export function addVersions(x: number, y: number): number;
|
||||
*
|
||||
* // TSDoc: Versioning.(addVersions:2)
|
||||
* export function addVersions(x: string, y: string): string;
|
||||
*
|
||||
* // (implementation)
|
||||
* export function addVersions(x: number|string, y: number|string): number|string {
|
||||
* // . . .
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* In the above example, there are two overloaded declarations. The overload using numbers will have
|
||||
* `overloadIndex = 1`. The overload using strings will have `overloadIndex = 2`. The third declaration that
|
||||
* accepts all possible inputs is considered part of the implementation, and is not processed by API Extractor.
|
||||
*/
|
||||
readonly overloadIndex: number;
|
||||
|
||||
/**
|
||||
* The function parameters.
|
||||
*/
|
||||
readonly parameters: readonly Parameter[];
|
||||
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiParameterListMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiParameterListMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiParameterListMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiParameterListMixin) {
|
||||
class MixedClass extends baseClass implements ApiParameterListMixin {
|
||||
public readonly [_overloadIndex]: number;
|
||||
|
||||
public readonly [_parameters]: Parameter[];
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiParameterListMixinOptions = args[0];
|
||||
this[_overloadIndex] = options.overloadIndex;
|
||||
|
||||
this[_parameters] = [];
|
||||
|
||||
if (this instanceof ApiDeclaredItem) {
|
||||
if (options.parameters) {
|
||||
for (const parameterOptions of options.parameters) {
|
||||
const parameter: Parameter = new Parameter({
|
||||
name: parameterOptions.parameterName,
|
||||
parameterTypeExcerpt: this.buildExcerpt(parameterOptions.parameterTypeTokenRange),
|
||||
// Prior to ApiJsonSchemaVersion.V_1005 this input will be undefined
|
||||
isOptional: Boolean(parameterOptions.isOptional),
|
||||
isRest: Boolean(parameterOptions.isRest),
|
||||
parent: this,
|
||||
});
|
||||
|
||||
this[_parameters].push(parameter);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new InternalError('ApiReturnTypeMixin expects a base class that inherits from ApiDeclaredItem');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiParameterListMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiParameterListJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.overloadIndex = jsonObject.overloadIndex;
|
||||
options.parameters = jsonObject.parameters || [];
|
||||
}
|
||||
|
||||
public get overloadIndex(): number {
|
||||
return this[_overloadIndex];
|
||||
}
|
||||
|
||||
public get parameters(): readonly Parameter[] {
|
||||
return this[_parameters];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiParameterListJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.overloadIndex = this.overloadIndex;
|
||||
|
||||
const parameterObjects: IApiParameterOptions[] = [];
|
||||
for (const parameter of this.parameters) {
|
||||
parameterObjects.push({
|
||||
parameterName: parameter.name,
|
||||
parameterTypeTokenRange: parameter.parameterTypeExcerpt.tokenRange,
|
||||
isOptional: parameter.isOptional,
|
||||
isRest: parameter.isRest,
|
||||
});
|
||||
}
|
||||
|
||||
jsonObject.parameters = parameterObjects;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiParameterListMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiParameterListMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiParameterListMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiParameterListMixin {
|
||||
return apiItem.hasOwnProperty(_parameters);
|
||||
}
|
||||
}
|
||||
117
packages/api-extractor-model/src/mixins/ApiProtectedMixin.ts
Normal file
117
packages/api-extractor-model/src/mixins/ApiProtectedMixin.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiProtectedMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiProtectedMixinOptions extends IApiItemOptions {
|
||||
isProtected: boolean;
|
||||
}
|
||||
|
||||
export interface IApiProtectedMixinJson extends IApiItemJson {
|
||||
isProtected: boolean;
|
||||
}
|
||||
|
||||
const _isProtected: unique symbol = Symbol('ApiProtectedMixin._isProtected');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can have the TypeScript `protected` keyword applied to them.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiProtectedMixin extends ApiItem {
|
||||
/**
|
||||
* Whether the declaration has the TypeScript `protected` keyword.
|
||||
*/
|
||||
readonly isProtected: boolean;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiProtectedMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiProtectedMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiProtectedMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiProtectedMixin) {
|
||||
class MixedClass extends baseClass implements ApiProtectedMixin {
|
||||
public [_isProtected]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiProtectedMixinOptions = args[0];
|
||||
this[_isProtected] = options.isProtected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiProtectedMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiProtectedMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.isProtected = jsonObject.isProtected;
|
||||
}
|
||||
|
||||
public get isProtected(): boolean {
|
||||
return this[_isProtected];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiProtectedMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.isProtected = this.isProtected;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiProtectedMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiProtectedMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiProtectedMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiProtectedMixin {
|
||||
return apiItem.hasOwnProperty(_isProtected);
|
||||
}
|
||||
}
|
||||
137
packages/api-extractor-model/src/mixins/ApiReadonlyMixin.ts
Normal file
137
packages/api-extractor-model/src/mixins/ApiReadonlyMixin.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiReadonlyMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiReadonlyMixinOptions extends IApiItemOptions {
|
||||
isReadonly: boolean;
|
||||
}
|
||||
|
||||
export interface IApiReadonlyMixinJson extends IApiItemJson {
|
||||
isReadonly: boolean;
|
||||
}
|
||||
|
||||
const _isReadonly: unique symbol = Symbol('ApiReadonlyMixin._isReadonly');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that cannot be modified after instantiation.
|
||||
* Examples such as the readonly modifier and only having a getter but no setter.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiReadonlyMixin extends ApiItem {
|
||||
/**
|
||||
* Indicates that the API item's value cannot be assigned by an external consumer.
|
||||
*
|
||||
* @remarks
|
||||
* Examples of API items that would be considered "read only" by API Extractor:
|
||||
*
|
||||
* - A class or interface's property that has the `readonly` modifier.
|
||||
*
|
||||
* - A variable that has the `const` modifier.
|
||||
*
|
||||
* - A property or variable whose TSDoc comment includes the `@readonly` tag.
|
||||
*
|
||||
* - A property declaration with a getter but no setter.
|
||||
*
|
||||
* Note that if the `readonly` keyword appears in a type annotation, this does not
|
||||
* guarantee that that the API item will be considered readonly. For example:
|
||||
*
|
||||
* ```ts
|
||||
* declare class C {
|
||||
* // isReadonly=false in this case, because C.x is assignable
|
||||
* public x: readonly string[];
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
readonly isReadonly: boolean;
|
||||
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiReadonlyMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiReadonlyMixin:interface)}
|
||||
* functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiReadonlyMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiReadonlyMixin) {
|
||||
class MixedClass extends baseClass implements ApiReadonlyMixin {
|
||||
public [_isReadonly]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiReadonlyMixinOptions = args[0];
|
||||
this[_isReadonly] = options.isReadonly;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiReadonlyMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiReadonlyMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.isReadonly = jsonObject.isReadonly || false;
|
||||
}
|
||||
|
||||
public get isReadonly(): boolean {
|
||||
return this[_isReadonly];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiReadonlyMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.isReadonly = this.isReadonly;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiReadonlyMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiReadonlyMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiReadonlyMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiReadonlyMixin {
|
||||
return apiItem.hasOwnProperty(_isReadonly);
|
||||
}
|
||||
}
|
||||
132
packages/api-extractor-model/src/mixins/ApiReleaseTagMixin.ts
Normal file
132
packages/api-extractor-model/src/mixins/ApiReleaseTagMixin.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { Enum } from '@rushstack/node-core-library';
|
||||
import { ReleaseTag } from '../aedoc/ReleaseTag.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiReleaseTagMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiReleaseTagMixinOptions extends IApiItemOptions {
|
||||
releaseTag: ReleaseTag;
|
||||
}
|
||||
|
||||
export interface IApiReleaseTagMixinJson extends IApiItemJson {
|
||||
releaseTag: string;
|
||||
}
|
||||
|
||||
const _releaseTag: unique symbol = Symbol('ApiReleaseTagMixin._releaseTag');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can be attributed with a TSDoc tag such as `@internal`,
|
||||
* `@alpha`, `@beta`, or `@public`. These "release tags" indicate the support level for an API.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiReleaseTagMixin extends ApiItem {
|
||||
/**
|
||||
* The effective release tag for this declaration. If it is not explicitly specified, the value may be
|
||||
* inherited from a containing declaration.
|
||||
*
|
||||
* @remarks
|
||||
* For example, an `ApiEnumMember` may inherit its release tag from the containing `ApiEnum`.
|
||||
*/
|
||||
readonly releaseTag: ReleaseTag;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiReleaseTagMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiReleaseTagMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiReleaseTagMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiReleaseTagMixin) {
|
||||
class MixedClass extends baseClass implements ApiReleaseTagMixin {
|
||||
public [_releaseTag]: ReleaseTag;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiReleaseTagMixinOptions = args[0];
|
||||
this[_releaseTag] = options.releaseTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiReleaseTagMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiReleaseTagMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
const deserializedReleaseTag: ReleaseTag | undefined = Enum.tryGetValueByKey<ReleaseTag>(
|
||||
ReleaseTag as any,
|
||||
jsonObject.releaseTag,
|
||||
);
|
||||
if (deserializedReleaseTag === undefined) {
|
||||
throw new Error(`Failed to deserialize release tag ${JSON.stringify(jsonObject.releaseTag)}`);
|
||||
}
|
||||
|
||||
options.releaseTag = deserializedReleaseTag;
|
||||
}
|
||||
|
||||
public get releaseTag(): ReleaseTag {
|
||||
return this[_releaseTag];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiReleaseTagMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.releaseTag = ReleaseTag[this.releaseTag];
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiReleaseTagMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiReleaseTagMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiReleaseTagMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiReleaseTagMixin {
|
||||
return apiItem.hasOwnProperty(_releaseTag);
|
||||
}
|
||||
}
|
||||
125
packages/api-extractor-model/src/mixins/ApiReturnTypeMixin.ts
Normal file
125
packages/api-extractor-model/src/mixins/ApiReturnTypeMixin.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { InternalError } from '@rushstack/node-core-library';
|
||||
import { ApiDeclaredItem } from '../items/ApiDeclaredItem.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
import type { IExcerptTokenRange, Excerpt } from './Excerpt.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiReturnTypeMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiReturnTypeMixinOptions extends IApiItemOptions {
|
||||
returnTypeTokenRange: IExcerptTokenRange;
|
||||
}
|
||||
|
||||
export interface IApiReturnTypeMixinJson extends IApiItemJson {
|
||||
returnTypeTokenRange: IExcerptTokenRange;
|
||||
}
|
||||
|
||||
const _returnTypeExcerpt: unique symbol = Symbol('ApiReturnTypeMixin._returnTypeExcerpt');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that are functions that return a value.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiReturnTypeMixin extends ApiItem {
|
||||
/**
|
||||
* An {@link Excerpt} that describes the type of the function's return value.
|
||||
*/
|
||||
readonly returnTypeExcerpt: Excerpt;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiReturnTypeMixinJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiReturnTypeMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiReturnTypeMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiReturnTypeMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiReturnTypeMixin) {
|
||||
class MixedClass extends baseClass implements ApiReturnTypeMixin {
|
||||
public [_returnTypeExcerpt]: Excerpt;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiReturnTypeMixinOptions = args[0];
|
||||
|
||||
if (this instanceof ApiDeclaredItem) {
|
||||
this[_returnTypeExcerpt] = this.buildExcerpt(options.returnTypeTokenRange);
|
||||
} else {
|
||||
throw new InternalError('ApiReturnTypeMixin expects a base class that inherits from ApiDeclaredItem');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiReturnTypeMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiReturnTypeMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.returnTypeTokenRange = jsonObject.returnTypeTokenRange;
|
||||
}
|
||||
|
||||
public get returnTypeExcerpt(): Excerpt {
|
||||
return this[_returnTypeExcerpt];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiReturnTypeMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.returnTypeTokenRange = this.returnTypeExcerpt.tokenRange;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiReturnTypeMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiReturnTypeMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiReturnTypeMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiReturnTypeMixin {
|
||||
return apiItem.hasOwnProperty(_returnTypeExcerpt);
|
||||
}
|
||||
}
|
||||
117
packages/api-extractor-model/src/mixins/ApiStaticMixin.ts
Normal file
117
packages/api-extractor-model/src/mixins/ApiStaticMixin.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (IApiStaticMixinOptions:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiStaticMixinOptions extends IApiItemOptions {
|
||||
isStatic: boolean;
|
||||
}
|
||||
|
||||
export interface IApiStaticMixinJson extends IApiItemJson {
|
||||
isStatic: boolean;
|
||||
}
|
||||
|
||||
const _isStatic: unique symbol = Symbol('ApiStaticMixin._isStatic');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can have the TypeScript `static` keyword applied to them.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiStaticMixin extends ApiItem {
|
||||
/**
|
||||
* Whether the declaration has the TypeScript `static` keyword.
|
||||
*/
|
||||
readonly isStatic: boolean;
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiStaticMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiStaticMixin:interface)} functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiStaticMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiStaticMixin) {
|
||||
class MixedClass extends baseClass implements ApiStaticMixin {
|
||||
public [_isStatic]: boolean;
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiStaticMixinOptions = args[0];
|
||||
this[_isStatic] = options.isStatic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiStaticMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiStaticMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.isStatic = jsonObject.isStatic;
|
||||
}
|
||||
|
||||
public get isStatic(): boolean {
|
||||
return this[_isStatic];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiStaticMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
jsonObject.isStatic = this.isStatic;
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiStaticMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiStaticMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiStaticMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiStaticMixin {
|
||||
return apiItem.hasOwnProperty(_isStatic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import { InternalError } from '@rushstack/node-core-library';
|
||||
import { ApiDeclaredItem } from '../items/ApiDeclaredItem.js';
|
||||
import type { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem.js';
|
||||
import type { DeserializerContext } from '../model/DeserializerContext.js';
|
||||
import { TypeParameter } from '../model/TypeParameter.js';
|
||||
import type { Excerpt, IExcerptTokenRange } from './Excerpt.js';
|
||||
|
||||
/**
|
||||
* Represents parameter information that is part of {@link IApiTypeParameterListMixinOptions}
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiTypeParameterOptions {
|
||||
constraintTokenRange: IExcerptTokenRange;
|
||||
defaultTypeTokenRange: IExcerptTokenRange;
|
||||
typeParameterName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor options for {@link (ApiTypeParameterListMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IApiTypeParameterListMixinOptions extends IApiItemOptions {
|
||||
typeParameters: IApiTypeParameterOptions[];
|
||||
}
|
||||
|
||||
export interface IApiTypeParameterListMixinJson extends IApiItemJson {
|
||||
typeParameters: IApiTypeParameterOptions[];
|
||||
}
|
||||
|
||||
const _typeParameters: unique symbol = Symbol('ApiTypeParameterListMixin._typeParameters');
|
||||
|
||||
/**
|
||||
* The mixin base class for API items that can have type parameters.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* This is part of the {@link ApiModel} hierarchy of classes, which are serializable representations of
|
||||
* API declarations. The non-abstract classes (e.g. `ApiClass`, `ApiEnum`, `ApiInterface`, etc.) use
|
||||
* TypeScript "mixin" functions (e.g. `ApiDeclaredItem`, `ApiItemContainerMixin`, etc.) to add various
|
||||
* features that cannot be represented as a normal inheritance chain (since TypeScript does not allow a child class
|
||||
* to extend more than one base class). The "mixin" is a TypeScript merged declaration with three components:
|
||||
* the function that generates a subclass, an interface that describes the members of the subclass, and
|
||||
* a namespace containing static members of the class.
|
||||
* @public
|
||||
*/
|
||||
|
||||
export interface ApiTypeParameterListMixin extends ApiItem {
|
||||
serializeInto(jsonObject: Partial<IApiItemJson>): void;
|
||||
|
||||
/**
|
||||
* The type parameters.
|
||||
*/
|
||||
readonly typeParameters: readonly TypeParameter[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin function for {@link (ApiTypeParameterListMixin:interface)}.
|
||||
*
|
||||
* @param baseClass - The base class to be extended
|
||||
* @returns A child class that extends baseClass, adding the {@link (ApiTypeParameterListMixin:interface)}
|
||||
* functionality.
|
||||
* @public
|
||||
*/
|
||||
export function ApiTypeParameterListMixin<TBaseClass extends IApiItemConstructor>(
|
||||
baseClass: TBaseClass,
|
||||
): TBaseClass & (new (...args: any[]) => ApiTypeParameterListMixin) {
|
||||
class MixedClass extends baseClass implements ApiTypeParameterListMixin {
|
||||
public readonly [_typeParameters]: TypeParameter[];
|
||||
|
||||
public constructor(...args: any[]) {
|
||||
super(...args);
|
||||
|
||||
const options: IApiTypeParameterListMixinOptions = args[0];
|
||||
|
||||
this[_typeParameters] = [];
|
||||
|
||||
if (this instanceof ApiDeclaredItem) {
|
||||
if (options.typeParameters) {
|
||||
for (const typeParameterOptions of options.typeParameters) {
|
||||
const defaultTypeExcerpt: Excerpt = this.buildExcerpt(typeParameterOptions.defaultTypeTokenRange);
|
||||
const typeParameter: TypeParameter = new TypeParameter({
|
||||
name: typeParameterOptions.typeParameterName,
|
||||
constraintExcerpt: this.buildExcerpt(typeParameterOptions.constraintTokenRange),
|
||||
defaultTypeExcerpt,
|
||||
isOptional: !defaultTypeExcerpt.isEmpty,
|
||||
parent: this,
|
||||
});
|
||||
|
||||
this[_typeParameters].push(typeParameter);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new InternalError('ApiTypeParameterListMixin expects a base class that inherits from ApiDeclaredItem');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public static override onDeserializeInto(
|
||||
options: Partial<IApiTypeParameterListMixinOptions>,
|
||||
context: DeserializerContext,
|
||||
jsonObject: IApiTypeParameterListMixinJson,
|
||||
): void {
|
||||
baseClass.onDeserializeInto(options, context, jsonObject);
|
||||
|
||||
options.typeParameters = jsonObject.typeParameters || [];
|
||||
}
|
||||
|
||||
public get typeParameters(): readonly TypeParameter[] {
|
||||
return this[_typeParameters];
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
public override serializeInto(jsonObject: Partial<IApiTypeParameterListMixinJson>): void {
|
||||
super.serializeInto(jsonObject);
|
||||
|
||||
const typeParameterObjects: IApiTypeParameterOptions[] = [];
|
||||
for (const typeParameter of this.typeParameters) {
|
||||
typeParameterObjects.push({
|
||||
typeParameterName: typeParameter.name,
|
||||
constraintTokenRange: typeParameter.constraintExcerpt.tokenRange,
|
||||
defaultTypeTokenRange: typeParameter.defaultTypeExcerpt.tokenRange,
|
||||
});
|
||||
}
|
||||
|
||||
if (typeParameterObjects.length > 0) {
|
||||
jsonObject.typeParameters = typeParameterObjects;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MixedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static members for {@link (ApiTypeParameterListMixin:interface)}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export namespace ApiTypeParameterListMixin {
|
||||
/**
|
||||
* A type guard that tests whether the specified `ApiItem` subclass extends the `ApiParameterListMixin` mixin.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* The JavaScript `instanceof` operator cannot be used to test for mixin inheritance, because each invocation of
|
||||
* the mixin function produces a different subclass. (This could be mitigated by `Symbol.hasInstance`, however
|
||||
* the TypeScript type system cannot invoke a runtime test.)
|
||||
*/
|
||||
export function isBaseClassOf(apiItem: ApiItem): apiItem is ApiTypeParameterListMixin {
|
||||
return apiItem.hasOwnProperty(_typeParameters);
|
||||
}
|
||||
}
|
||||
173
packages/api-extractor-model/src/mixins/Excerpt.ts
Normal file
173
packages/api-extractor-model/src/mixins/Excerpt.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
|
||||
import { Text } from '@rushstack/node-core-library';
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export enum ExcerptTokenKind {
|
||||
/**
|
||||
* Generic text without any special properties
|
||||
*/
|
||||
Content = 'Content',
|
||||
|
||||
/**
|
||||
* A reference to an API declaration
|
||||
*/
|
||||
Reference = 'Reference',
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by {@link Excerpt} to indicate a range of indexes within an array of `ExcerptToken` objects.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IExcerptTokenRange {
|
||||
/**
|
||||
* The index of the last member of the span, plus one.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* If `startIndex` and `endIndex` are the same number, then the span is empty.
|
||||
*/
|
||||
endIndex: number;
|
||||
|
||||
/**
|
||||
* The starting index of the span.
|
||||
*/
|
||||
startIndex: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface IExcerptToken {
|
||||
canonicalReference?: string | undefined;
|
||||
readonly kind: ExcerptTokenKind;
|
||||
text: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a fragment of text belonging to an {@link Excerpt} object.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class ExcerptToken {
|
||||
private readonly _kind: ExcerptTokenKind;
|
||||
|
||||
private readonly _text: string;
|
||||
|
||||
private readonly _canonicalReference: DeclarationReference | undefined;
|
||||
|
||||
public constructor(kind: ExcerptTokenKind, text: string, canonicalReference?: DeclarationReference) {
|
||||
this._kind = kind;
|
||||
|
||||
// Standardize the newlines across operating systems. Even though this may deviate from the actual
|
||||
// input source file that was parsed, it's useful because the newline gets serialized inside
|
||||
// a string literal in .api.json, which cannot be automatically normalized by Git.
|
||||
this._text = Text.convertToLf(text);
|
||||
this._canonicalReference = canonicalReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the kind of token.
|
||||
*/
|
||||
public get kind(): ExcerptTokenKind {
|
||||
return this._kind;
|
||||
}
|
||||
|
||||
/**
|
||||
* The text fragment.
|
||||
*/
|
||||
public get text(): string {
|
||||
return this._text;
|
||||
}
|
||||
|
||||
/**
|
||||
* The hyperlink target for a token whose type is `ExcerptTokenKind.Reference`. For other token types,
|
||||
* this property will be `undefined`.
|
||||
*/
|
||||
public get canonicalReference(): DeclarationReference | undefined {
|
||||
return this._canonicalReference;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The `Excerpt` class is used by {@link ApiDeclaredItem} to represent a TypeScript code fragment that may be
|
||||
* annotated with hyperlinks to declared types (and in the future, source code locations).
|
||||
*
|
||||
* @remarks
|
||||
* API Extractor's .api.json file format stores excerpts compactly as a start/end indexes into an array of tokens.
|
||||
* Every `ApiDeclaredItem` has a "main excerpt" corresponding to the full list of tokens. The declaration may
|
||||
* also have have "captured" excerpts that correspond to subranges of tokens.
|
||||
*
|
||||
* For example, if the main excerpt is:
|
||||
*
|
||||
* ```
|
||||
* function parse(s: string): Vector | undefined;
|
||||
* ```
|
||||
*
|
||||
* ...then this entire signature is the "main excerpt", whereas the function's return type `Vector | undefined` is a
|
||||
* captured excerpt. The `Vector` token might be a hyperlink to that API item.
|
||||
*
|
||||
* An excerpt may be empty (i.e. a token range containing zero tokens). For example, if a function's return value
|
||||
* is not explicitly declared, then the returnTypeExcerpt will be empty. By contrast, a class constructor cannot
|
||||
* have a return value, so ApiConstructor has no returnTypeExcerpt property at all.
|
||||
* @public
|
||||
*/
|
||||
export class Excerpt {
|
||||
/**
|
||||
* The complete list of tokens for the source code fragment that this excerpt is based upon.
|
||||
* If this object is the main excerpt, then it will span all of the tokens; otherwise, it will correspond to
|
||||
* a range within the array.
|
||||
*/
|
||||
public readonly tokens: readonly ExcerptToken[];
|
||||
|
||||
/**
|
||||
* Specifies the excerpt's range within the `tokens` array.
|
||||
*/
|
||||
public readonly tokenRange: Readonly<IExcerptTokenRange>;
|
||||
|
||||
/**
|
||||
* The tokens spanned by this excerpt. It is the range of the `tokens` array as specified by the `tokenRange`
|
||||
* property.
|
||||
*/
|
||||
public readonly spannedTokens: readonly ExcerptToken[];
|
||||
|
||||
private _text: string | undefined;
|
||||
|
||||
public constructor(tokens: readonly ExcerptToken[], tokenRange: IExcerptTokenRange) {
|
||||
this.tokens = tokens;
|
||||
this.tokenRange = tokenRange;
|
||||
|
||||
if (
|
||||
this.tokenRange.startIndex < 0 ||
|
||||
this.tokenRange.endIndex > this.tokens.length ||
|
||||
this.tokenRange.startIndex > this.tokenRange.endIndex
|
||||
) {
|
||||
throw new Error('Invalid token range');
|
||||
}
|
||||
|
||||
this.spannedTokens = this.tokens.slice(this.tokenRange.startIndex, this.tokenRange.endIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* The excerpted text, formed by concatenating the text of the `spannedTokens` strings.
|
||||
*/
|
||||
public get text(): string {
|
||||
if (this._text === undefined) {
|
||||
this._text = this.spannedTokens.map((x) => x.text).join('');
|
||||
}
|
||||
|
||||
return this._text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the excerpt is an empty range.
|
||||
*/
|
||||
public get isEmpty(): boolean {
|
||||
return this.tokenRange.startIndex === this.tokenRange.endIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
import type { ApiItem } from '../items/ApiItem.js';
|
||||
|
||||
/**
|
||||
* Generic result object for finding API items used by different kinds of find operations.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IFindApiItemsResult {
|
||||
/**
|
||||
* The API items that were found. Not guaranteed to be complete, see `maybeIncompleteResult`.
|
||||
*/
|
||||
items: ApiItem[];
|
||||
|
||||
/**
|
||||
* Indicates whether the result is potentially incomplete due to errors during the find operation.
|
||||
* If true, the `messages` explain the errors in more detail.
|
||||
*/
|
||||
maybeIncompleteResult: boolean;
|
||||
|
||||
/**
|
||||
* Diagnostic messages regarding the find operation.
|
||||
*/
|
||||
messages: IFindApiItemsMessage[];
|
||||
}
|
||||
|
||||
/**
|
||||
* This object is used for messages returned as part of `IFindApiItemsResult`.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export interface IFindApiItemsMessage {
|
||||
/**
|
||||
* Unique identifier for the message.
|
||||
*
|
||||
* @beta
|
||||
*/
|
||||
messageId: FindApiItemsMessageId;
|
||||
|
||||
/**
|
||||
* Text description of the message.
|
||||
*/
|
||||
text: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique identifiers for messages returned as part of `IFindApiItemsResult`.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export enum FindApiItemsMessageId {
|
||||
/**
|
||||
* "Unable to resolve declaration reference within API item ___: ___"
|
||||
*/
|
||||
DeclarationResolutionFailed = 'declaration-resolution-failed',
|
||||
|
||||
/**
|
||||
* "Unable to analyze extends clause ___ of API item ___ because no canonical reference was found."
|
||||
*/
|
||||
ExtendsClauseMissingReference = 'extends-clause-missing-reference',
|
||||
|
||||
/**
|
||||
* "Unable to analyze references of API item ___ because it is not associated with an ApiModel"
|
||||
*/
|
||||
NoAssociatedApiModel = 'no-associated-api-model',
|
||||
|
||||
/**
|
||||
* "Unable to analyze references of API item ___ because it is of unsupported kind ___"
|
||||
*/
|
||||
UnsupportedKind = 'unsupported-kind',
|
||||
}
|
||||
18
packages/api-extractor-model/src/mixins/Mixin.ts
Normal file
18
packages/api-extractor-model/src/mixins/Mixin.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||||
// See LICENSE in the project root for license information.
|
||||
|
||||
/**
|
||||
* This abstraction is used by the mixin pattern.
|
||||
* It describes a class constructor.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export type Constructor<T = {}> = new (...args: any[]) => T;
|
||||
|
||||
/**
|
||||
* This abstraction is used by the mixin pattern.
|
||||
* It describes the "static side" of a class.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export type PropertiesOf<T> = { [K in keyof T]: T[K] };
|
||||
Reference in New Issue
Block a user