diff --git a/src/collector/definitions.js b/src/collector/definitions.js index 2bd7be1..a256081 100644 --- a/src/collector/definitions.js +++ b/src/collector/definitions.js @@ -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 { diff --git a/src/collector/globals.js b/src/collector/globals.js index c1151de..3e0d7f2 100644 --- a/src/collector/globals.js +++ b/src/collector/globals.js @@ -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); } diff --git a/tests/samples/maybe/source.js b/tests/samples/maybe/source.js index ae31dc9..6eb8fc4 100644 --- a/tests/samples/maybe/source.js +++ b/tests/samples/maybe/source.js @@ -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}; diff --git a/tests/samples/maybe/types.yaml b/tests/samples/maybe/types.yaml index 5b59418..f9b5de5 100644 --- a/tests/samples/maybe/types.yaml +++ b/tests/samples/maybe/types.yaml @@ -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: