Skip to content

Commit cb30a4c

Browse files
committed
Initial skeleton of AstModel concept
1 parent 1534b3b commit cb30a4c

10 files changed

Lines changed: 220 additions & 1 deletion

File tree

apps/api-extractor/src/analyzer/AstSymbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class AstSymbol {
4343

4444
/**
4545
* If this symbol was imported from another package, that information is tracked here.
46-
* Otherwies, the value is undefined.
46+
* Otherwise, the value is undefined.
4747
*/
4848
public readonly astImport: AstImport | undefined;
4949

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
6+
export class ApiClass extends ApiItem {
7+
public readonly kind: ApiItemKind = ApiItemKind.Class;
8+
9+
/** @override */
10+
protected getSortKey(): string {
11+
return this.name;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
6+
export class ApiEntryPoint extends ApiItem {
7+
public readonly kind: ApiItemKind = ApiItemKind.EntryPoint;
8+
9+
/** @override */
10+
protected getSortKey(): string {
11+
return this.name;
12+
}
13+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { Deserializer } from './Deserializer';
5+
6+
export const enum ApiItemKind {
7+
Class = 'Class',
8+
EntryPoint = 'EntryPoint',
9+
Method = 'Method',
10+
Model = 'Model',
11+
Namespace = 'Namespace',
12+
Package = 'Package',
13+
Parameter = 'Parameter'
14+
}
15+
16+
export interface IApiItemParameters {
17+
name: string;
18+
}
19+
20+
export interface ISerializedMetadata {
21+
kind: ApiItemKind;
22+
members: ReadonlyArray<SerializedApiItem<IApiItemParameters>>;
23+
}
24+
25+
export type SerializedApiItem<T extends IApiItemParameters> = T & ISerializedMetadata;
26+
27+
export abstract class ApiItem {
28+
public abstract readonly kind: ApiItemKind;
29+
30+
private _members: ApiItem[];
31+
private _name: string;
32+
private _membersSorted: boolean;
33+
34+
public static deserialize(jsonObject: SerializedApiItem<IApiItemParameters>): ApiItem {
35+
return Deserializer.deserialize(jsonObject);
36+
}
37+
38+
public constructor(parameters: IApiItemParameters) {
39+
this._name = parameters.name;
40+
}
41+
42+
public serializeInto(jsonObject: Partial<SerializedApiItem<IApiItemParameters>>): void {
43+
jsonObject.kind = this.kind;
44+
jsonObject.name = this.name;
45+
const memberObjects: Partial<SerializedApiItem<IApiItemParameters>>[] = [];
46+
47+
for (const member of this.members) {
48+
const memberJsonObject: Partial<SerializedApiItem<IApiItemParameters>> = {};
49+
member.serializeInto(memberJsonObject);
50+
memberObjects.push(memberJsonObject);
51+
}
52+
53+
jsonObject.members = memberObjects as SerializedApiItem<IApiItemParameters>[];
54+
}
55+
56+
public get name(): string {
57+
return this._name;
58+
}
59+
60+
public addMember(member: ApiItem): void {
61+
this._members.push(member);
62+
this._membersSorted = false;
63+
}
64+
65+
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+
}
70+
71+
return this._members;
72+
}
73+
74+
protected abstract getSortKey(): string;
75+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
6+
export class ApiMethod extends ApiItem {
7+
public readonly kind: ApiItemKind = ApiItemKind.Method;
8+
9+
/** @override */
10+
protected getSortKey(): string {
11+
return this.name;
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiPackage } from './ApiPackage';
6+
7+
export class ApiModel extends ApiItem {
8+
public readonly kind: ApiItemKind = ApiItemKind.Model;
9+
10+
public loadPackage(apiJsonFilename: string): ApiPackage {
11+
const apiPackage: ApiPackage = ApiPackage.loadFromJsonFile(apiJsonFilename);
12+
this.addMember(apiPackage);
13+
return apiPackage;
14+
}
15+
16+
/** @override */
17+
protected getSortKey(): string {
18+
return this.name;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
6+
export class ApiNamespace extends ApiItem {
7+
public readonly kind: ApiItemKind = ApiItemKind.Namespace;
8+
9+
/** @override */
10+
protected getSortKey(): string {
11+
return this.name;
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind, SerializedApiItem, IApiItemParameters } from './ApiItem';
5+
import { JsonFile } from '@microsoft/node-core-library';
6+
7+
export class ApiPackage extends ApiItem {
8+
public readonly kind: ApiItemKind = ApiItemKind.Package;
9+
10+
public static loadFromJsonFile(apiJsonFilename: string): ApiPackage {
11+
const jsonObject: { } = JsonFile.load(apiJsonFilename);
12+
return ApiItem.deserialize(jsonObject as SerializedApiItem<IApiItemParameters>) as ApiPackage;
13+
}
14+
15+
public saveToJsonFile(apiJsonFilename: string): void {
16+
const jsonObject: { } = { };
17+
this.serializeInto(jsonObject);
18+
JsonFile.save(jsonObject, apiJsonFilename);
19+
}
20+
21+
/** @override */
22+
protected getSortKey(): string {
23+
return this.name;
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ApiItem, ApiItemKind } from './ApiItem';
5+
6+
export class ApiParameter extends ApiItem {
7+
public readonly kind: ApiItemKind = ApiItemKind.Parameter;
8+
9+
/** @override */
10+
protected getSortKey(): string {
11+
return this.name;
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { SerializedApiItem, IApiItemParameters, ApiItem, ApiItemKind } from './ApiItem';
5+
import { ApiClass } from './ApiClass';
6+
import { ApiEntryPoint } from './ApiEntryPoint';
7+
import { ApiMethod } from './ApiMethod';
8+
import { ApiModel } from './ApiModel';
9+
import { ApiNamespace } from './ApiNamespace';
10+
import { ApiPackage } from './ApiPackage';
11+
import { ApiParameter } from './ApiParameter';
12+
13+
export class Deserializer {
14+
public static deserialize(jsonObject: SerializedApiItem<IApiItemParameters>): ApiItem {
15+
switch (jsonObject.kind) {
16+
case ApiItemKind.Class:
17+
return new ApiClass(jsonObject);
18+
case ApiItemKind.EntryPoint:
19+
return new ApiEntryPoint(jsonObject);
20+
case ApiItemKind.Method:
21+
return new ApiMethod(jsonObject);
22+
case ApiItemKind.Model:
23+
return new ApiModel(jsonObject);
24+
case ApiItemKind.Namespace:
25+
return new ApiNamespace(jsonObject);
26+
case ApiItemKind.Package:
27+
return new ApiPackage(jsonObject);
28+
case ApiItemKind.Parameter:
29+
return new ApiParameter(jsonObject);
30+
default:
31+
throw new Error(`Failed to deserialize unsupported API item type ${JSON.stringify(jsonObject.kind)}`);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)