Skip to content

Commit d0697f5

Browse files
committed
Update the API, handle type names differently
1 parent 9e7f08f commit d0697f5

File tree

70 files changed

+1337
-1378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1337
-1378
lines changed

src/models/export.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Documentation } from './documentation';
2-
import { TypeNode } from './node';
2+
import { AnyType } from './node';
33

44
export class ExportNode {
55
constructor(
66
public name: string,
7-
public type: TypeNode,
7+
public type: AnyType,
88
public documentation: Documentation | undefined,
99
) {}
1010

src/models/node.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
import { TypeName } from './typeName';
2+
import { ArrayNode } from './types/array';
3+
import { ComponentNode } from './types/component';
4+
import { EnumNode } from './types/enum';
5+
import { ExternalTypeNode } from './types/external';
6+
import { FunctionNode } from './types/function';
7+
import { IntersectionNode } from './types/intersection';
8+
import { IntrinsicNode } from './types/intrinsic';
9+
import { LiteralNode } from './types/literal';
10+
import { ObjectNode } from './types/object';
11+
import { TupleNode } from './types/tuple';
12+
import { TypeParameterNode } from './types/typeParameter';
13+
import { UnionNode } from './types/union';
14+
115
export interface TypeNode {
216
readonly kind: string;
3-
name: string | undefined;
4-
parentNamespaces: string[] | undefined;
17+
typeName: TypeName | undefined;
518
}
19+
20+
export type AnyType =
21+
| ArrayNode
22+
| ComponentNode
23+
| EnumNode
24+
| ExternalTypeNode
25+
| FunctionNode
26+
| IntersectionNode
27+
| IntrinsicNode
28+
| LiteralNode
29+
| ObjectNode
30+
| TupleNode
31+
| TypeParameterNode
32+
| UnionNode;

src/models/property.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Documentation } from './documentation';
2-
import { TypeNode } from './node';
2+
import { AnyType } from './node';
33

44
export class PropertyNode {
55
constructor(
66
public name: string,
7-
public type: TypeNode,
7+
public type: AnyType,
88
public documentation: Documentation | undefined,
99
public optional: boolean,
1010
) {}

src/models/typeName.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class TypeName {
2+
public readonly name: string | undefined;
3+
public readonly namespaces: readonly string[] | undefined;
4+
5+
constructor(name: string | undefined, namespaces: readonly string[] | undefined = undefined) {
6+
this.name = name;
7+
this.namespaces = namespaces;
8+
}
9+
10+
toString(): string {
11+
if (!this.name) {
12+
return '';
13+
}
14+
15+
if (!this.namespaces || this.namespaces.length === 0) {
16+
return this.name;
17+
}
18+
19+
return `${this.namespaces.join('.')}.${this.name}`;
20+
}
21+
}

src/models/types/array.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { TypeNode } from '../node';
1+
import { AnyType, TypeNode } from '../node';
2+
import { TypeName } from '../typeName';
23

34
export class ArrayNode implements TypeNode {
4-
kind = 'array';
5-
constructor(
6-
public name: string | undefined,
7-
public parentNamespaces: string[],
8-
public elementType: TypeNode,
9-
) {}
5+
readonly kind = 'array';
6+
public typeName: TypeName | undefined;
7+
public elementType: AnyType;
8+
9+
constructor(typeName: TypeName | undefined, elementType: AnyType) {
10+
this.typeName = typeName?.name ? typeName : undefined;
11+
this.elementType = elementType;
12+
}
1013
}

src/models/types/component.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { PropertyNode } from '../property';
22
import { TypeNode } from '../node';
3+
import { TypeName } from '../typeName';
34

45
export class ComponentNode implements TypeNode {
5-
kind = 'component';
6+
readonly kind = 'component';
7+
public typeName: TypeName | undefined;
8+
public props: PropertyNode[];
69

7-
constructor(
8-
public name: string | undefined,
9-
public parentNamespaces: string[],
10-
public props: PropertyNode[],
11-
) {}
10+
constructor(typeName: TypeName | undefined, props: PropertyNode[]) {
11+
this.props = props;
12+
this.typeName = typeName?.name ? typeName : undefined;
13+
}
1214
}

src/models/types/compoundTypeUtils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { uniqBy } from 'lodash';
22
import { IntrinsicNode } from './intrinsic';
33
import { LiteralNode } from './literal';
44
import { ExternalTypeNode } from './external';
5-
import { TypeNode } from '../node';
5+
import { AnyType } from '../node';
66
import { IntersectionNode } from './intersection';
77
import { UnionNode } from './union';
88
import { TypeParameterNode } from './typeParameter';
99

1010
export function flattenTypes(
11-
nodes: readonly TypeNode[],
11+
nodes: readonly AnyType[],
1212
nodeToProcess: typeof UnionNode | typeof IntersectionNode,
13-
): TypeNode[] {
14-
let flatTypes: TypeNode[] = [];
13+
): AnyType[] {
14+
let flatTypes: AnyType[] = [];
1515
nodes.forEach((node) => {
16-
if (node instanceof nodeToProcess && !node.name) {
16+
if (node instanceof nodeToProcess && !node.typeName) {
1717
flatTypes = flatTypes.concat(flattenTypes(node.types, nodeToProcess));
1818
} else {
1919
flatTypes.push(node);
@@ -23,25 +23,29 @@ export function flattenTypes(
2323
return flatTypes;
2424
}
2525

26-
export function deduplicateMemberTypes(types: TypeNode[]): TypeNode[] {
26+
export function deduplicateMemberTypes(types: AnyType[]): AnyType[] {
2727
return uniqBy(types, (x) => {
2828
if (x instanceof LiteralNode) {
2929
return x.value;
3030
}
3131

32-
if (x instanceof ExternalTypeNode || x instanceof TypeParameterNode) {
32+
if (x instanceof ExternalTypeNode) {
33+
return x.typeName;
34+
}
35+
36+
if (x instanceof TypeParameterNode) {
3337
return x.name;
3438
}
3539

3640
if (x instanceof IntrinsicNode) {
37-
return x.name ?? x.intrinsic;
41+
return x.typeName ?? x.intrinsic;
3842
}
3943

4044
return x;
4145
});
4246
}
4347

44-
export function sortMemberTypes(members: TypeNode[]) {
48+
export function sortMemberTypes(members: AnyType[]) {
4549
// move undefined and null to the end
4650

4751
const nullIndex = members.findIndex((x) => x instanceof IntrinsicNode && x.intrinsic === 'null');

src/models/types/enum.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Documentation } from '../documentation';
22
import { TypeNode } from '../node';
3+
import { TypeName } from '../typeName';
34

45
export class EnumNode implements TypeNode {
5-
kind = 'enum';
6+
readonly kind = 'enum';
67

78
constructor(
8-
public name: string,
9-
public parentNamespaces: string[],
9+
public typeName: TypeName,
1010
public members: EnumMember[],
1111
public documentation: Documentation | undefined,
1212
) {}

src/models/types/external.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { TypeNode } from '../node';
2+
import { TypeName } from '../typeName';
23

34
export class ExternalTypeNode implements TypeNode {
4-
kind = 'external';
5+
readonly kind = 'external';
56

6-
constructor(
7-
public name: string,
8-
public parentNamespaces: string[],
9-
) {}
7+
constructor(public typeName: TypeName) {}
108
}

src/models/types/function.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { Documentation } from '../documentation';
2-
import { TypeNode } from '../node';
2+
import { AnyType, TypeNode } from '../node';
3+
import { TypeName } from '../typeName';
34

45
export class FunctionNode implements TypeNode {
5-
kind = 'function';
6-
name: string | undefined;
6+
readonly kind = 'function';
7+
typeName: TypeName | undefined;
8+
callSignatures: CallSignature[];
79

8-
constructor(
9-
name: string | undefined,
10-
public parentNamespaces: string[],
11-
public callSignatures: CallSignature[],
12-
) {
13-
this.name = name === '__function' ? undefined : name;
10+
constructor(typeName: TypeName | undefined, callSignatures: CallSignature[]) {
11+
this.typeName =
12+
typeName?.name === '__function' || typeName?.name === undefined ? undefined : typeName;
13+
this.callSignatures = callSignatures;
1414
}
1515
}
1616

1717
export class CallSignature {
1818
constructor(
1919
public parameters: Parameter[],
20-
public returnValueType: TypeNode,
20+
public returnValueType: AnyType,
2121
) {}
2222
}
2323

2424
export class Parameter {
2525
constructor(
26-
public type: TypeNode,
26+
public type: AnyType,
2727
public name: string,
2828
public documentation: Documentation | undefined,
2929
public optional: boolean,

0 commit comments

Comments
 (0)