Add tuples

master
Paul Loyd 2017-11-28 18:38:55 +03:00
parent 18d81b66db
commit c843463c0e
6 changed files with 83 additions and 2 deletions

View File

@ -21,6 +21,9 @@ declare module 'wu' {
pluck<K: $Keys<T>>(K): Wu<$ElementType<T, K>>;
some(): boolean;
some(T => boolean): boolean;
toArray(): T[];
}
}

View File

@ -6,6 +6,7 @@ import type {
GenericTypeAnnotation, InterfaceDeclaration, IntersectionTypeAnnotation, TypeAlias,
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
TupleTypeAnnotation,
} from '@babel/types';
import {
@ -115,6 +116,8 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
return makeComplexType(ctx, node);
case 'ArrayTypeAnnotation':
return makeArrayType(ctx, node);
case 'TupleTypeAnnotation':
return makeTupleType(ctx, node);
case 'UnionTypeAnnotation':
return makeUnionType(ctx, node);
case 'IntersectionTypeAnnotation':
@ -255,6 +258,20 @@ function makeArrayType(ctx: Context, node: ArrayTypeAnnotation): ?ArrayType {
};
}
function makeTupleType(ctx: Context, node: TupleTypeAnnotation): ?TupleType {
// TODO: warning about nulls.
const items = wu(node.types).map(node => makeType(ctx, node)).toArray();
if (items.length === 0 || !wu(items).some()) {
return null;
}
return {
kind: 'tuple',
items,
};
}
function makeUnionType(ctx: Context, node: UnionTypeAnnotation): ?Type {
const variants = wu(node.types)
.map(node => makeType(ctx, node))

View File

@ -38,7 +38,7 @@ export type ArrayType = BaseType & {
export type TupleType = BaseType & {
kind: 'tuple',
items: Type[],
items: (?Type)[],
};
export type MapType = BaseType & {

View File

@ -10,7 +10,9 @@ type Type = {
bar: () => void,
[string]: () => void,
c: string & () => void;
c: string & () => void,
kek: [() => void],
};
interface Interface {

15
tests/samples/tuples.js Normal file
View File

@ -0,0 +1,15 @@
type Type = {
a: [string, boolean],
};
interface Interface {
a: [string, null, boolean];
b: [string, () => void, boolean];
}
class Class {
a: [boolean];
b: [];
}
export {Type, Interface, Class};

44
tests/samples/tuples.json Normal file
View File

@ -0,0 +1,44 @@
{
"types": [
{
"id": ["tuples", "Type"],
"kind": "record",
"fields": [{
"name": "a",
"value": {"kind": "tuple", "items": [{"kind": "string"}, {"kind": "boolean"}]},
"required": true
}]
},
{
"id": ["tuples", "Interface"],
"kind": "record",
"fields": [
{
"name": "a",
"value": {
"kind": "tuple",
"items": [{"kind": "string"}, {"kind": "literal", "value": null}, {"kind": "boolean"}]
},
"required": true
},
{
"name": "b",
"value": {
"kind": "tuple",
"items": [{"kind": "string"}, null, {"kind": "boolean"}]
},
"required": true
}
]
},
{
"id": ["tuples", "Class"],
"kind": "record",
"fields": [{
"name": "a",
"value": {"kind": "tuple", "items": [{"kind": "boolean"}]},
"required": true
}]
}
]
}