Unwrap nested maybes

master
Paul Loyd 2017-12-03 15:46:00 +03:00
parent b4244fc196
commit b8e69e10a4
4 changed files with 14 additions and 42 deletions

View File

@ -141,7 +141,11 @@ function makeType(ctx: Context, node: FlowTypeAnnotation): ?Type {
function makeMaybe(ctx: Context, node: NullableTypeAnnotation): ?MaybeType {
const type = makeType(ctx, node.typeAnnotation);
return type != null ? t.createMaybe(type) : null;
if (!type) {
return null;
}
return type.kind === 'maybe' ? type : t.createMaybe(type);
}
function makeComplex(ctx: Context, node: ObjectTypeAnnotation): Type {

View File

@ -59,7 +59,7 @@ function stripMaybe(params: (?Type)[], resolve: TypeId => Type): ?Type {
const maybe = resolve(ref.to);
// TODO: support for unions and nested maybe.
// TODO: support for unions.
if (maybe.kind !== 'maybe') {
return t.clone(ref);
}

View File

@ -1,19 +1,15 @@
type X = {
x: ?string,
xx: ?string[],
xxx: (?string)[],
xx: ?(?string),
};
interface Y {
y: ?string;
yy: ?string[];
yyy: (?string)[];
y: ?string[];
yy: ?(?(?string));
}
class Z {
z: ?string;
zz: ?string[];
zzz: (?string)[];
z: (?string)[];
}
export {X, Y, Z};

View File

@ -8,55 +8,27 @@
- name: xx
value:
kind: maybe
value:
kind: array
items: {kind: string}
required: true
- name: xxx
value:
kind: array
items:
kind: maybe
value: {kind: string}
value: {kind: string}
required: true
id: [maybe, X]
- kind: record
fields:
- name: y
value:
kind: maybe
value: {kind: string}
required: true
- name: yy
value:
kind: maybe
value:
kind: array
items: {kind: string}
required: true
- name: yyy
- name: yy
value:
kind: array
items:
kind: maybe
value: {kind: string}
kind: maybe
value: {kind: string}
required: true
id: [maybe, Y]
- kind: record
fields:
- name: z
value:
kind: maybe
value: {kind: string}
required: true
- name: zz
value:
kind: maybe
value:
kind: array
items: {kind: string}
required: true
- name: zzz
value:
kind: array
items: