From 57603cf0ff44014b68c6668a3eba87c84c7a53ae Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Sun, 3 Dec 2017 14:57:44 +0300 Subject: [PATCH] Add support for $All and $Either --- src/collector/globals.js | 22 +++++++++++-- tests/samples/all/source.js | 16 +++++++++ tests/samples/all/types.yaml | 47 +++++++++++++++++++++++++++ tests/samples/either/source.js | 14 ++++++++ tests/samples/either/types.yaml | 27 +++++++++++++++ tests/samples/skipFunctions/source.js | 1 + 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tests/samples/all/source.js create mode 100644 tests/samples/all/types.yaml create mode 100644 tests/samples/either/source.js create mode 100644 tests/samples/either/types.yaml diff --git a/src/collector/globals.js b/src/collector/globals.js index 0f3f7bd..c1151de 100644 --- a/src/collector/globals.js +++ b/src/collector/globals.js @@ -178,6 +178,24 @@ function diff(params: (?Type)[], resolve: TypeId => Type): ?Type { return t.createRecord(fields); } +// $All. +function all(params: (?Type)[]): ?Type { + const parts = wu(params).filter().toArray(); + + return parts.length === 0 ? null + : parts.length === 1 ? parts[0] + : t.createIntersection(parts); +} + +// $Either. +function either(params: (?Type)[]): ?Type { + const variants = wu(params).filter().toArray(); + + return variants.length === 0 ? null + : variants.length === 1 ? variants[0] + : t.createUnion(variants); +} + export default { Object: object, Buffer: buffer, @@ -192,6 +210,6 @@ export default { $Keys: keys, $Values: values, $Diff: diff, - // TODO: $All - // TODO: $Either + $All: all, + $Either: either, }; diff --git a/tests/samples/all/source.js b/tests/samples/all/source.js new file mode 100644 index 0000000..86141aa --- /dev/null +++ b/tests/samples/all/source.js @@ -0,0 +1,16 @@ +type A = {a: number}; +type B = {b: string}; +type C = {c: boolean}; + +type X = $All; + +type Y = { + y: $All, + yy: $All<() => boolean, () => string>, +}; + +class Z { + z: $All void>; +} + +export {X, Y, Z}; diff --git a/tests/samples/all/types.yaml b/tests/samples/all/types.yaml new file mode 100644 index 0000000..ff68561 --- /dev/null +++ b/tests/samples/all/types.yaml @@ -0,0 +1,47 @@ +- kind: record + fields: + - name: a + value: {kind: number, repr: f64} + required: true + id: [all, A] +- kind: record + fields: + - name: b + value: {kind: string} + required: true + id: [all, B] +- kind: intersection + parts: + - kind: reference + to: [all, A] + - kind: reference + to: [all, B] + id: [all, X] +- kind: record + fields: + - name: c + value: {kind: boolean} + required: true + id: [all, C] +- kind: record + fields: + - name: y + value: + kind: intersection + parts: + - kind: reference + to: [all, A] + - kind: reference + to: [all, B] + - kind: reference + to: [all, C] + required: true + id: [all, Y] +- kind: record + fields: + - name: z + value: + kind: reference + to: [all, A] + required: true + id: [all, Z] diff --git a/tests/samples/either/source.js b/tests/samples/either/source.js new file mode 100644 index 0000000..2ee6075 --- /dev/null +++ b/tests/samples/either/source.js @@ -0,0 +1,14 @@ +type Type = { + a: $Either, +}; + +interface Interface { + a: $Either; + aa: $Either<() => number, () => boolean>; +} + +class Class { + a: $Either void>; +} + +export {Type, Interface, Class}; diff --git a/tests/samples/either/types.yaml b/tests/samples/either/types.yaml new file mode 100644 index 0000000..8070f9e --- /dev/null +++ b/tests/samples/either/types.yaml @@ -0,0 +1,27 @@ +- kind: record + fields: + - name: a + value: + kind: union + variants: + - {kind: string} + - {kind: boolean} + required: true + id: [either, Type] +- kind: record + fields: + - name: a + value: + kind: union + variants: + - {kind: string} + - {kind: boolean} + - {kind: number, repr: f64} + required: true + id: [either, Interface] +- kind: record + fields: + - name: a + value: {kind: string} + required: true + id: [either, Class] diff --git a/tests/samples/skipFunctions/source.js b/tests/samples/skipFunctions/source.js index eac7d76..b73b7f0 100644 --- a/tests/samples/skipFunctions/source.js +++ b/tests/samples/skipFunctions/source.js @@ -1,3 +1,4 @@ +// TODO: disassembly the test. type Type = { a: string,