-
Notifications
You must be signed in to change notification settings - Fork 626
[api-extractor] Add support for type alias types and type parameters #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
156015b
Add support for type alias types and type parameters
rbuckton 90bce40
Add changelog entries
rbuckton a2a2cf1
Add support for emitting type parameters to yaml
rbuckton 0e024ce
Add changelog entries
rbuckton d8002f7
Provide a more detailed example in the TypeParameter doc comment
octogonz 5121152
PR feedback
rbuckton c20db74
Change bump type to "minor"
octogonz 914cc7e
Add doc comment example for ApiTypeAlias.aliasTypeExcerpt
octogonz addc973
Rename aliasType... to type...
rbuckton 32c9b73
Merge branch 'master' into updateApiModel
octogonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
apps/api-extractor-model/src/mixins/ApiTypeParameterListMixin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information.s | ||
|
||
import { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem'; | ||
import { IExcerptTokenRange } from './Excerpt'; | ||
import { TypeParameter } from '../model/TypeParameter'; | ||
import { InternalError } from '@microsoft/node-core-library'; | ||
import { ApiDeclaredItem } from '../items/ApiDeclaredItem'; | ||
|
||
/** | ||
* Represents parameter information that is part of {@link IApiTypeParameterListMixinOptions} | ||
* @public | ||
*/ | ||
export interface IApiTypeParameterOptions { | ||
typeParameterName: string; | ||
constraintTokenRange: IExcerptTokenRange; | ||
defaultTypeTokenRange: IExcerptTokenRange; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
// tslint:disable-next-line:interface-name | ||
export interface ApiTypeParameterListMixin extends ApiItem { | ||
/** | ||
* The type parameters. | ||
*/ | ||
readonly typeParameters: ReadonlyArray<TypeParameter>; | ||
|
||
serializeInto(jsonObject: Partial<IApiItemJson>): void; | ||
} | ||
|
||
/** | ||
* 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) { // tslint:disable-line:no-any | ||
|
||
abstract class MixedClass extends baseClass implements ApiTypeParameterListMixin { | ||
public readonly [_typeParameters]: TypeParameter[]; | ||
|
||
/** @override */ | ||
public static onDeserializeInto(options: Partial<IApiTypeParameterListMixinOptions>, | ||
jsonObject: IApiTypeParameterListMixinJson): void { | ||
|
||
baseClass.onDeserializeInto(options, jsonObject); | ||
|
||
options.typeParameters = jsonObject.typeParameters || []; | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
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 typeParameter: TypeParameter = new TypeParameter({ | ||
name: typeParameterOptions.typeParameterName, | ||
constraintExcerpt: this.buildExcerpt(typeParameterOptions.constraintTokenRange), | ||
defaultTypeExcerpt: this.buildExcerpt(typeParameterOptions.defaultTypeTokenRange), | ||
parent: this | ||
}); | ||
|
||
this[_typeParameters].push(typeParameter); | ||
} | ||
} | ||
} else { | ||
throw new InternalError('ApiTypeParameterListMixin expects a base class that inherits from ApiDeclaredItem'); | ||
} | ||
} | ||
|
||
public get typeParameters(): ReadonlyArray<TypeParameter> { | ||
return this[_typeParameters]; | ||
} | ||
|
||
/** @override */ | ||
public 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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, somehow we forgot to export this