Refactor the definitions
parent
0dad186da2
commit
d2e2e6ea63
|
@ -94,6 +94,9 @@ function processClassDeclaration(ctx: Context, node: ClassDeclaration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
|
function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
|
||||||
|
// TODO: ThisTypeAnnotation
|
||||||
|
// TODO: VoidTypeAnnotation
|
||||||
|
// TODO: TypeofTypeAnnotation
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'NullLiteralTypeAnnotation':
|
case 'NullLiteralTypeAnnotation':
|
||||||
return t.createLiteral(null);
|
return t.createLiteral(null);
|
||||||
|
@ -108,13 +111,13 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
|
||||||
case 'NullableTypeAnnotation':
|
case 'NullableTypeAnnotation':
|
||||||
return makeMaybe(ctx, node);
|
return makeMaybe(ctx, node);
|
||||||
case 'ObjectTypeAnnotation':
|
case 'ObjectTypeAnnotation':
|
||||||
return makeComplexType(ctx, node);
|
return makeComplex(ctx, node);
|
||||||
case 'ArrayTypeAnnotation':
|
case 'ArrayTypeAnnotation':
|
||||||
return makeArrayType(ctx, node);
|
return makeArray(ctx, node);
|
||||||
case 'TupleTypeAnnotation':
|
case 'TupleTypeAnnotation':
|
||||||
return makeTupleType(ctx, node);
|
return makeTuple(ctx, node);
|
||||||
case 'UnionTypeAnnotation':
|
case 'UnionTypeAnnotation':
|
||||||
return makeUnionType(ctx, node);
|
return makeUnion(ctx, node);
|
||||||
case 'IntersectionTypeAnnotation':
|
case 'IntersectionTypeAnnotation':
|
||||||
return makeIntersection(ctx, node);
|
return makeIntersection(ctx, node);
|
||||||
case 'StringLiteralTypeAnnotation':
|
case 'StringLiteralTypeAnnotation':
|
||||||
|
@ -135,14 +138,10 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
|
||||||
function makeMaybe(ctx: Context, node: NullableTypeAnnotation): ?MaybeType {
|
function makeMaybe(ctx: Context, node: NullableTypeAnnotation): ?MaybeType {
|
||||||
const type = makeType(ctx, node.typeAnnotation);
|
const type = makeType(ctx, node.typeAnnotation);
|
||||||
|
|
||||||
if (!type) {
|
return type != null ? t.createMaybe(type) : null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.createMaybe(type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeComplexType(ctx: Context, node: ObjectTypeAnnotation): Type {
|
function makeComplex(ctx: Context, node: ObjectTypeAnnotation): Type {
|
||||||
const maps = wu(node.indexers)
|
const maps = wu(node.indexers)
|
||||||
.map(node => makeMap(ctx, node))
|
.map(node => makeMap(ctx, node))
|
||||||
.filter()
|
.filter()
|
||||||
|
@ -220,24 +219,16 @@ function makeMap(ctx: Context, node: ObjectTypeIndexer): ?MapType {
|
||||||
const keys = makeType(ctx, node.key);
|
const keys = makeType(ctx, node.key);
|
||||||
const values = makeType(ctx, node.value);
|
const values = makeType(ctx, node.value);
|
||||||
|
|
||||||
if (!(keys && values)) {
|
return keys && values ? t.createMap(keys, values) : null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.createMap(keys, values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeArrayType(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
|
function makeArray(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
|
||||||
const items = makeType(ctx, node.elementType);
|
const items = makeType(ctx, node.elementType);
|
||||||
|
|
||||||
if (!items) {
|
return items != null ? t.createArray(items) : null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.createArray(items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeTupleType(ctx: Context, node: TupleTypeAnnotation): ?TupleType {
|
function makeTuple(ctx: Context, node: TupleTypeAnnotation): ?TupleType {
|
||||||
// TODO: warning about nulls.
|
// TODO: warning about nulls.
|
||||||
const items = wu(node.types).map(node => makeType(ctx, node)).toArray();
|
const items = wu(node.types).map(node => makeType(ctx, node)).toArray();
|
||||||
|
|
||||||
|
@ -248,21 +239,15 @@ function makeTupleType(ctx: Context, node: TupleTypeAnnotation): ?TupleType {
|
||||||
return t.createTuple(items);
|
return t.createTuple(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeUnionType(ctx: Context, node: UnionTypeAnnotation): ?Type {
|
function makeUnion(ctx: Context, node: UnionTypeAnnotation): ?Type {
|
||||||
const variants = wu(node.types)
|
const variants = wu(node.types)
|
||||||
.map(node => makeType(ctx, node))
|
.map(node => makeType(ctx, node))
|
||||||
.filter()
|
.filter()
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
if (variants.length === 0) {
|
return variants.length === 0 ? null
|
||||||
return null;
|
: variants.length === 1 ? variants[0]
|
||||||
}
|
: t.createUnion(variants);
|
||||||
|
|
||||||
if (variants.length === 1) {
|
|
||||||
return variants[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.createUnion(variants);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeIntersection(ctx: Context, node: IntersectionTypeAnnotation): ?Type {
|
function makeIntersection(ctx: Context, node: IntersectionTypeAnnotation): ?Type {
|
||||||
|
@ -272,15 +257,9 @@ function makeIntersection(ctx: Context, node: IntersectionTypeAnnotation): ?Type
|
||||||
.filter()
|
.filter()
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
if (parts.length === 0) {
|
return parts.length === 0 ? null
|
||||||
return null;
|
: parts.length === 1 ? parts[0]
|
||||||
}
|
: t.createIntersection(parts);
|
||||||
|
|
||||||
if (parts.length === 1) {
|
|
||||||
return parts[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.createIntersection(parts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
|
function makeReference(ctx: Context, node: GenericTypeAnnotation): ?Type {
|
||||||
|
|
Loading…
Reference in New Issue