Skip to content
This repository was archived by the owner on Sep 3, 2019. It is now read-only.

Commit 2c0589e

Browse files
author
nathan
committed
Added converter for intersection types
This is how the output json output has changed from typecript line type IIntersectionFooBar = IFoo & IBar; output json diff "id": 6, "name": "IIntersectionFooBar", "kind": 4194304, "kindString": "Type alias", "flags": {}, "sources": [ { "fileName": "test.ts", "line": 9, "character": 24 } ], "type": { - "type": "unknown", - "name": "IIntersectionFooBar" + "type": "intersection", + "types": [ + { + "type": "reference", + "name": "IFoo" + }, + { + "type": "reference", + "name": "IBar" + } + ] }
1 parent b53f9d3 commit 2c0589e

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

src/lib/converter/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {ArrayConverter} from './array';
33
export {BindingArrayConverter} from './binding-array';
44
export {BindingObjectConverter} from './binding-object';
55
export {EnumConverter} from './enum';
6+
export {IntersectionConverter} from './intersection';
67
export {IntrinsicConverter} from './intrinsic'
78
export {StringLiteralConverter} from './string-literal';
89
export {ReferenceConverter} from './reference';
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as ts from 'typescript';
2+
3+
import {Type, IntersectionType} from '../../models/types/index';
4+
import {Component, ConverterTypeComponent, TypeConverter} from '../components';
5+
import {Context} from '../context';
6+
7+
@Component({name: 'type:intersection'})
8+
export class IntersectionConverter extends ConverterTypeComponent implements TypeConverter<ts.IntersectionType, ts.IntersectionTypeNode> {
9+
/**
10+
* Test whether this converter can handle the given TypeScript node.
11+
*/
12+
supportsNode(context: Context, node: ts.IntersectionTypeNode): boolean {
13+
return node.kind === ts.SyntaxKind.IntersectionType;
14+
}
15+
16+
/**
17+
* Test whether this converter can handle the given TypeScript type.
18+
*/
19+
supportsType(context: Context, type: ts.IntersectionType): boolean {
20+
return !!(type.flags & ts.TypeFlags.Intersection);
21+
}
22+
23+
/**
24+
* Convert the given intersection type node to its type reflection.
25+
*
26+
* This is a node based converter, see [[convertIntersectionType]] for the type equivalent.
27+
*
28+
* ```
29+
* let someValue: string|number;
30+
* ```
31+
*
32+
* @param context The context object describing the current state the converter is in.
33+
* @param node The intersection type node that should be converted.
34+
* @returns The type reflection representing the given intersection type node.
35+
*/
36+
convertNode(context: Context, node: ts.IntersectionTypeNode): IntersectionType {
37+
let types: Type[] = [];
38+
if (node.types) {
39+
types = node.types.map((n) => this.owner.convertType(context, n));
40+
} else {
41+
types = [];
42+
}
43+
44+
return new IntersectionType(types);
45+
}
46+
47+
/**
48+
* Convert the given intersection type to its type reflection.
49+
*
50+
* This is a type based converter, see [[convertIntersectionTypeNode]] for the node equivalent.
51+
*
52+
* ```
53+
* let someValue: string|number;
54+
* ```
55+
*
56+
* @param context The context object describing the current state the converter is in.
57+
* @param type The intersection type that should be converted.
58+
* @returns The type reflection representing the given intersection type.
59+
*/
60+
convertType(context: Context, type: ts.IntersectionType): IntersectionType {
61+
let types: Type[];
62+
if (type && type.types) {
63+
types = type.types.map((t) => this.owner.convertType(context, null, t));
64+
} else {
65+
types = [];
66+
}
67+
68+
return new IntersectionType(types);
69+
}
70+
}

src/lib/models/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export {Type} from './abstract';
2+
export {IntersectionType} from './intersection';
23
export {IntrinsicType} from './intrinsic';
34
export {ReferenceType} from './reference';
45
export {ReflectionType} from './reflection';

src/lib/models/types/intersection.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {Type} from './abstract';
2+
3+
/**
4+
* Represents an intersection type.
5+
*
6+
* ~~~
7+
* type t: string & string[];
8+
* ~~~
9+
*/
10+
export class IntersectionType extends Type {
11+
/**
12+
* The types this intersection consists of.
13+
*/
14+
types: Type[];
15+
16+
/**
17+
* Create a new TupleType instance.
18+
*
19+
* @param types The types this intersection consists of.
20+
*/
21+
constructor(types: Type[]) {
22+
super();
23+
this.types = types;
24+
}
25+
26+
/**
27+
* Clone this type.
28+
*
29+
* @return A clone of this type.
30+
*/
31+
clone(): Type {
32+
const clone = new IntersectionType(this.types);
33+
clone.isArray = this.isArray;
34+
return clone;
35+
}
36+
37+
/**
38+
* Test whether this type equals the given type.
39+
*
40+
* @param type The type that should be checked for equality.
41+
* @returns TRUE if the given type equals this type, FALSE otherwise.
42+
*/
43+
equals(type: IntersectionType): boolean {
44+
if (!(type instanceof IntersectionType)) {
45+
return false;
46+
}
47+
if (type.isArray !== this.isArray) {
48+
return false;
49+
}
50+
return Type.isTypeListSimiliar(type.types, this.types);
51+
}
52+
53+
/**
54+
* Return a raw object representation of this type.
55+
*/
56+
toObject(): any {
57+
const result: any = super.toObject();
58+
result.type = 'intersection';
59+
60+
if (this.types && this.types.length) {
61+
result.types = this.types.map((e) => e.toObject());
62+
}
63+
64+
return result;
65+
}
66+
67+
/**
68+
* Return a string representation of this type.
69+
*/
70+
toString() {
71+
const names: string[] = [];
72+
this.types.forEach((element) => {
73+
names.push(element.toString());
74+
});
75+
76+
return names.join(' | ');
77+
}
78+
}

0 commit comments

Comments
 (0)