Skip to content

Commit a624a87

Browse files
committed
Initial prototype of mixins approach
1 parent cb30a4c commit a624a87

14 files changed

Lines changed: 268 additions & 69 deletions

apps/api-extractor/src/api/Extractor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ExtractorContext } from '../analyzer/ExtractorContext';
2222
import { DtsRollupGenerator, DtsRollupKind } from '../generators/DtsRollupGenerator';
2323
import { MonitoredLogger } from './MonitoredLogger';
2424
import { TypeScriptMessageFormatter } from '../analyzer/TypeScriptMessageFormatter';
25+
import { ModelBuilder } from '../generators/ModelBuilder';
2526

2627
/**
2728
* Options for {@link Extractor.processProject}.
@@ -312,6 +313,11 @@ export class Extractor {
312313
validationRules: this.actualConfig.validationRules
313314
});
314315

316+
if (this.actualConfig.apiReviewFile.enabled) {
317+
const modelBuilder: ModelBuilder = new ModelBuilder(context);
318+
modelBuilder.process();
319+
}
320+
315321
this._generateRollupDtsFiles(context);
316322

317323
if (this._localBuild) {

apps/api-extractor/src/api/model/ApiClass.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// See LICENSE in the project root for license information.
33

44
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56

6-
export class ApiClass extends ApiItem {
7-
public readonly kind: ApiItemKind = ApiItemKind.Class;
7+
export class ApiClass extends ApiMembersMixin(ApiItem) {
8+
/** @override */
9+
public get kind(): ApiItemKind {
10+
return ApiItemKind.Class;
11+
}
812

913
/** @override */
10-
protected getSortKey(): string {
14+
public getSortKey(): string {
1115
return this.name;
1216
}
1317
}

apps/api-extractor/src/api/model/ApiEntryPoint.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// See LICENSE in the project root for license information.
33

44
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56

6-
export class ApiEntryPoint extends ApiItem {
7-
public readonly kind: ApiItemKind = ApiItemKind.EntryPoint;
7+
export class ApiEntryPoint extends ApiMembersMixin(ApiItem) {
8+
/** @override */
9+
public get kind(): ApiItemKind {
10+
return ApiItemKind.EntryPoint;
11+
}
812

913
/** @override */
10-
protected getSortKey(): string {
14+
public getSortKey(): string {
1115
return this.name;
1216
}
1317
}

apps/api-extractor/src/api/model/ApiItem.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { Deserializer } from './Deserializer';
5-
64
export const enum ApiItemKind {
75
Class = 'Class',
86
EntryPoint = 'EntryPoint',
97
Method = 'Method',
108
Model = 'Model',
119
Namespace = 'Namespace',
1210
Package = 'Package',
13-
Parameter = 'Parameter'
11+
Parameter = 'Parameter',
12+
None = 'None'
1413
}
1514

1615
export interface IApiItemParameters {
@@ -24,14 +23,11 @@ export interface ISerializedMetadata {
2423

2524
export type SerializedApiItem<T extends IApiItemParameters> = T & ISerializedMetadata;
2625

27-
export abstract class ApiItem {
28-
public abstract readonly kind: ApiItemKind;
29-
30-
private _members: ApiItem[];
31-
private _name: string;
32-
private _membersSorted: boolean;
26+
export class ApiItem {
27+
private readonly _name: string;
3328

3429
public static deserialize(jsonObject: SerializedApiItem<IApiItemParameters>): ApiItem {
30+
// tslint:disable-next-line:no-use-before-declare
3531
return Deserializer.deserialize(jsonObject);
3632
}
3733

@@ -57,19 +53,21 @@ export abstract class ApiItem {
5753
return this._name;
5854
}
5955

60-
public addMember(member: ApiItem): void {
61-
this._members.push(member);
62-
this._membersSorted = false;
63-
}
64-
56+
/** @virtual */
6557
public get members(): ReadonlyArray<ApiItem> {
66-
if (!this._membersSorted) {
67-
this._members.sort((x, y) => x.getSortKey().localeCompare(y.getSortKey()));
68-
this._membersSorted = true;
69-
}
58+
return [];
59+
}
7060

71-
return this._members;
61+
/** @virtual */
62+
public get kind(): ApiItemKind {
63+
throw new Error('ApiItem.kind was not implemented by the child class');
7264
}
7365

74-
protected abstract getSortKey(): string;
66+
/** @virtual */
67+
public getSortKey(): string {
68+
throw new Error('ApiItem.getSortKey was not implemented by the child class');
69+
}
7570
}
71+
72+
// Circular import
73+
import { Deserializer } from './Deserializer';

apps/api-extractor/src/api/model/ApiMethod.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// See LICENSE in the project root for license information.
33

44
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56

6-
export class ApiMethod extends ApiItem {
7-
public readonly kind: ApiItemKind = ApiItemKind.Method;
7+
export class ApiMethod extends ApiMembersMixin(ApiItem) {
8+
/** @override */
9+
public get kind(): ApiItemKind {
10+
return ApiItemKind.Method;
11+
}
812

913
/** @override */
10-
protected getSortKey(): string {
14+
public getSortKey(): string {
1115
return this.name;
1216
}
1317
}

apps/api-extractor/src/api/model/ApiModel.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// See LICENSE in the project root for license information.
33

44
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56
import { ApiPackage } from './ApiPackage';
67

7-
export class ApiModel extends ApiItem {
8-
public readonly kind: ApiItemKind = ApiItemKind.Model;
8+
export class ApiModel extends ApiMembersMixin(ApiItem) {
9+
public constructor() {
10+
super({ name: 'MODEL' });
11+
}
912

1013
public loadPackage(apiJsonFilename: string): ApiPackage {
1114
const apiPackage: ApiPackage = ApiPackage.loadFromJsonFile(apiJsonFilename);
@@ -14,7 +17,12 @@ export class ApiModel extends ApiItem {
1417
}
1518

1619
/** @override */
17-
protected getSortKey(): string {
20+
public get kind(): ApiItemKind {
21+
return ApiItemKind.Method;
22+
}
23+
24+
/** @override */
25+
public getSortKey(): string {
1826
return this.name;
1927
}
2028
}

apps/api-extractor/src/api/model/ApiNamespace.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// See LICENSE in the project root for license information.
33

44
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56

6-
export class ApiNamespace extends ApiItem {
7-
public readonly kind: ApiItemKind = ApiItemKind.Namespace;
7+
export class ApiNamespace extends ApiMembersMixin(ApiItem) {
8+
/** @override */
9+
public get kind(): ApiItemKind {
10+
return ApiItemKind.Namespace;
11+
}
812

913
/** @override */
10-
protected getSortKey(): string {
14+
public getSortKey(): string {
1115
return this.name;
1216
}
1317
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { ApiItem, ApiItemKind, SerializedApiItem, IApiItemParameters } from './ApiItem';
4+
import { ApiItem, ApiItemKind, IApiItemParameters, SerializedApiItem } from './ApiItem';
5+
import { ApiMembersMixin } from './Mixins';
56
import { JsonFile } from '@microsoft/node-core-library';
67

7-
export class ApiPackage extends ApiItem {
8-
public readonly kind: ApiItemKind = ApiItemKind.Package;
9-
8+
export class ApiPackage extends ApiMembersMixin(ApiItem) {
109
public static loadFromJsonFile(apiJsonFilename: string): ApiPackage {
1110
const jsonObject: { } = JsonFile.load(apiJsonFilename);
1211
return ApiItem.deserialize(jsonObject as SerializedApiItem<IApiItemParameters>) as ApiPackage;
1312
}
1413

15-
public saveToJsonFile(apiJsonFilename: string): void {
16-
const jsonObject: { } = { };
17-
this.serializeInto(jsonObject);
18-
JsonFile.save(jsonObject, apiJsonFilename);
14+
/** @override */
15+
public get kind(): ApiItemKind {
16+
return ApiItemKind.Package;
1917
}
2018

2119
/** @override */
22-
protected getSortKey(): string {
20+
public getSortKey(): string {
2321
return this.name;
2422
}
23+
24+
public saveToJsonFile(apiJsonFilename: string): void {
25+
const jsonObject: { } = { };
26+
this.serializeInto(jsonObject);
27+
JsonFile.save(jsonObject, apiJsonFilename);
28+
}
2529
}

apps/api-extractor/src/api/model/ApiParameter.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

apps/api-extractor/src/api/model/Deserializer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ApiMethod } from './ApiMethod';
88
import { ApiModel } from './ApiModel';
99
import { ApiNamespace } from './ApiNamespace';
1010
import { ApiPackage } from './ApiPackage';
11-
import { ApiParameter } from './ApiParameter';
1211

1312
export class Deserializer {
1413
public static deserialize(jsonObject: SerializedApiItem<IApiItemParameters>): ApiItem {
@@ -20,13 +19,13 @@ export class Deserializer {
2019
case ApiItemKind.Method:
2120
return new ApiMethod(jsonObject);
2221
case ApiItemKind.Model:
23-
return new ApiModel(jsonObject);
22+
return new ApiModel();
2423
case ApiItemKind.Namespace:
2524
return new ApiNamespace(jsonObject);
2625
case ApiItemKind.Package:
2726
return new ApiPackage(jsonObject);
2827
case ApiItemKind.Parameter:
29-
return new ApiParameter(jsonObject);
28+
// return new ApiParameter(jsonObject);
3029
default:
3130
throw new Error(`Failed to deserialize unsupported API item type ${JSON.stringify(jsonObject.kind)}`);
3231
}

0 commit comments

Comments
 (0)