-
Notifications
You must be signed in to change notification settings - Fork 692
/
array.ts
69 lines (64 loc) · 2.53 KB
/
array.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as ts from 'typescript';
import { Type, ArrayType } from '../../models/index';
import { Component, ConverterTypeComponent, TypeConverter } from '../components';
import { Context } from '../context';
@Component({name: 'type:array'})
export class ArrayConverter extends ConverterTypeComponent implements TypeConverter<ts.TypeReference, ts.ArrayTypeNode> {
/**
* Test whether this converter can handle the given TypeScript node.
*/
supportsNode(context: Context, node: ts.ArrayTypeNode): boolean {
return node.kind === ts.SyntaxKind.ArrayType;
}
/**
* Test whether this converter can handle the given TypeScript type.
*/
supportsType(context: Context, type: ts.TypeReference): boolean {
// Is there a better way to detect the {"type":"reference","name":"Array","typeArguments":{...}} types that are in fact arrays?
return !!(type.flags & ts.TypeFlags.Object)
&& !!type.symbol
&& type.symbol.name === 'Array'
&& !type.symbol.parent
&& !!type.typeArguments
&& type.typeArguments.length === 1;
}
/**
* Convert the given array type node to its type reflection.
*
* This is a node based converter with no type equivalent.
*
* ```
* let someValue: number[];
* ```
*
* @param context The context object describing the current state the converter is in.
* @param node The array type node that should be converted.
* @returns The type reflection representing the given array type node.
*/
convertNode(context: Context, node: ts.ArrayTypeNode): Type | undefined {
const result = this.owner.convertType(context, node.elementType);
if (result) {
return new ArrayType(result);
}
}
/**
* Convert the given type reference to its type reflection.
*
* This is a type based converter, see [[convertTypeReference]] for the node equivalent.
*
* ```
* class SomeClass { }
* let someValue: SomeClass;
* ```
*
* @param context The context object describing the current state the converter is in.
* @param type The type reference that should be converted.
* @returns The type reflection representing the given type reference.
*/
convertType(context: Context, type: ts.TypeReference): Type | undefined {
const result = this.owner.convertType(context, undefined, type.typeArguments && type.typeArguments[0]);
if (result) {
return new ArrayType(result);
}
}
}