From 51c0e973f36d294c3b574ce75efd5a0a811990c1 Mon Sep 17 00:00:00 2001 From: Boris Date: Thu, 30 Mar 2017 17:38:03 +0200 Subject: [PATCH 1/6] fix(flow): Change 'new' to 'constructor' Change definition for Record where constructor was specified with 'new'. --- type-definitions/immutable.js.flow | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/type-definitions/immutable.js.flow b/type-definitions/immutable.js.flow index 7d6bf441e7..d3f3cfd700 100644 --- a/type-definitions/immutable.js.flow +++ b/type-definitions/immutable.js.flow @@ -1144,7 +1144,7 @@ declare class Record { declare interface RecordClass { (values: $Shape | Iterable<[string, any]>): RecordInstance & T; - new (values: $Shape | Iterable<[string, any]>): RecordInstance & T; + constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; } declare class RecordInstance { From ec1a4a62814fcf74fef266a12ce8c5bc41ce61e2 Mon Sep 17 00:00:00 2001 From: oreqizer Date: Thu, 30 Mar 2017 18:31:00 +0200 Subject: [PATCH 2/6] fix(flow): Record returns a Class Makes Record return a Class type instead of an interface. Fixes an issue with constructor calls. --- dist/immutable.js.flow | 10 +++++----- type-definitions/immutable.js.flow | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dist/immutable.js.flow b/dist/immutable.js.flow index 7d6bf441e7..81f1742d00 100644 --- a/dist/immutable.js.flow +++ b/dist/immutable.js.flow @@ -1134,17 +1134,17 @@ declare function Repeat(value: T, times?: number): IndexedSeq; declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordInstance); declare class Record { - static (spec: Values, name?: string): RecordClass; - constructor(spec: Values, name?: string): RecordClass; + static (spec: Values, name?: string): Class>; + constructor(spec: Values, name?: string): Class>; static isRecord: typeof isRecord; static getDescriptiveName(record: RecordInstance<*>): string; } -declare interface RecordClass { - (values: $Shape | Iterable<[string, any]>): RecordInstance & T; - new (values: $Shape | Iterable<[string, any]>): RecordInstance & T; +declare class RecordClass { + static (values: $Shape | Iterable<[string, any]>): RecordInstance & T; + constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; } declare class RecordInstance { diff --git a/type-definitions/immutable.js.flow b/type-definitions/immutable.js.flow index d3f3cfd700..81f1742d00 100644 --- a/type-definitions/immutable.js.flow +++ b/type-definitions/immutable.js.flow @@ -1134,17 +1134,17 @@ declare function Repeat(value: T, times?: number): IndexedSeq; declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordInstance); declare class Record { - static (spec: Values, name?: string): RecordClass; - constructor(spec: Values, name?: string): RecordClass; + static (spec: Values, name?: string): Class>; + constructor(spec: Values, name?: string): Class>; static isRecord: typeof isRecord; static getDescriptiveName(record: RecordInstance<*>): string; } -declare interface RecordClass { - (values: $Shape | Iterable<[string, any]>): RecordInstance & T; - constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; +declare class RecordClass { + static (values: $Shape | Iterable<[string, any]>): RecordInstance & T; + constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; } declare class RecordInstance { From 94a54a0e4eb624049fe3d11cd152b04d776c6f47 Mon Sep 17 00:00:00 2001 From: oreqizer Date: Thu, 30 Mar 2017 19:02:26 +0200 Subject: [PATCH 3/6] refactor(flow): Directly return RecordClass There was an in-between step when constructing Records. Records now directly return a RecordClass that's the former RecordInstance. --- dist/immutable.js.flow | 12 +++++------- type-definitions/immutable.js.flow | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/dist/immutable.js.flow b/dist/immutable.js.flow index 81f1742d00..9e9f391836 100644 --- a/dist/immutable.js.flow +++ b/dist/immutable.js.flow @@ -1132,22 +1132,20 @@ declare class Stack<+T> extends IndexedCollection { declare function Range(start?: number, end?: number, step?: number): IndexedSeq; declare function Repeat(value: T, times?: number): IndexedSeq; -declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordInstance); +declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordClass); declare class Record { static (spec: Values, name?: string): Class>; constructor(spec: Values, name?: string): Class>; static isRecord: typeof isRecord; - static getDescriptiveName(record: RecordInstance<*>): string; + static getDescriptiveName(record: RecordClass<*>): string; } -declare class RecordClass { - static (values: $Shape | Iterable<[string, any]>): RecordInstance & T; - constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; -} +declare class RecordClass { + static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; + constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; -declare class RecordInstance { size: number; has(key: string): boolean; diff --git a/type-definitions/immutable.js.flow b/type-definitions/immutable.js.flow index 81f1742d00..9e9f391836 100644 --- a/type-definitions/immutable.js.flow +++ b/type-definitions/immutable.js.flow @@ -1132,22 +1132,20 @@ declare class Stack<+T> extends IndexedCollection { declare function Range(start?: number, end?: number, step?: number): IndexedSeq; declare function Repeat(value: T, times?: number): IndexedSeq; -declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordInstance); +declare function isRecord(maybeRecord: any): boolean %checks(maybeRecord instanceof RecordClass); declare class Record { static (spec: Values, name?: string): Class>; constructor(spec: Values, name?: string): Class>; static isRecord: typeof isRecord; - static getDescriptiveName(record: RecordInstance<*>): string; + static getDescriptiveName(record: RecordClass<*>): string; } -declare class RecordClass { - static (values: $Shape | Iterable<[string, any]>): RecordInstance & T; - constructor(values: $Shape | Iterable<[string, any]>): RecordInstance & T; -} +declare class RecordClass { + static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; + constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; -declare class RecordInstance { size: number; has(key: string): boolean; From 54d4025d17bce32373ca389da14510d61f12dc00 Mon Sep 17 00:00:00 2001 From: oreqizer Date: Thu, 30 Mar 2017 19:10:37 +0200 Subject: [PATCH 4/6] fix(flow): Remove in constructors It was overriding RecordClass' generic signature. --- dist/immutable.js.flow | 4 ++-- type-definitions/immutable.js.flow | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/immutable.js.flow b/dist/immutable.js.flow index 9e9f391836..d979d3b3d3 100644 --- a/dist/immutable.js.flow +++ b/dist/immutable.js.flow @@ -1143,8 +1143,8 @@ declare class Record { } declare class RecordClass { - static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; - constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; + static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; + constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; size: number; diff --git a/type-definitions/immutable.js.flow b/type-definitions/immutable.js.flow index 9e9f391836..d979d3b3d3 100644 --- a/type-definitions/immutable.js.flow +++ b/type-definitions/immutable.js.flow @@ -1143,8 +1143,8 @@ declare class Record { } declare class RecordClass { - static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; - constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; + static (values?: $Shape | Iterable<[string, any]>): RecordClass & T; + constructor(values?: $Shape | Iterable<[string, any]>): RecordClass & T; size: number; From f0addf868e5491346fa6fdbf7b6f797e2d4b67ac Mon Sep 17 00:00:00 2001 From: oreqizer Date: Thu, 30 Mar 2017 19:11:11 +0200 Subject: [PATCH 5/6] feat(flow): Add test for Record constructor --- type-definitions/tests/record.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/type-definitions/tests/record.js b/type-definitions/tests/record.js index 725832c758..3461a6acd2 100644 --- a/type-definitions/tests/record.js +++ b/type-definitions/tests/record.js @@ -9,10 +9,12 @@ import { Record } from '../../'; const Point2 = Record({x:0, y:0}); const Point3 = Record({x:0, y:0, z:0}); +const PointNew = new Record({x:0, y:0}); const GeoPoint = Record({lat:(null: ?number), lon:(null: ?number)}); let origin2 = Point2({}); let origin3 = Point3({}); +let originNew = new PointNew(); let geo = GeoPoint({lat:34}); // $ExpectError const mistake = Point2({x:'string'}); From 1f66a82deb9bec6b90ed1e9812094a82dcd9f537 Mon Sep 17 00:00:00 2001 From: oreqizer Date: Thu, 30 Mar 2017 19:13:18 +0200 Subject: [PATCH 6/6] feat(flow): Add more tests for Record constructor --- type-definitions/tests/record.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/type-definitions/tests/record.js b/type-definitions/tests/record.js index 3461a6acd2..d569c5661b 100644 --- a/type-definitions/tests/record.js +++ b/type-definitions/tests/record.js @@ -18,6 +18,10 @@ let originNew = new PointNew(); let geo = GeoPoint({lat:34}); // $ExpectError const mistake = Point2({x:'string'}); +// $ExpectError - 'new Record' instantiated with invalid type +const mistakeNewRecord = PointNew({x:'string'}); +// $ExpectError - 'Record' instantiated with invalid type using 'new' +const mistakeNewInstance = new Point2({x:'string'}); origin3 = GeoPoint({lat:34}) geo = Point3({});