Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix object spreading, issues 6108, 7242, 5243, 7210, 7253, 6357, 5253, 4682, 6800, 981, 7048 and more #7298

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4870,14 +4870,16 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace =
|> replace_reason (fun desc -> RPropertyOf (x, desc))
|> repos_reason (aloc_of_reason reason_op)
in
match Property.read_t p with
| Some t ->
let propref = Named (reason_prop, x) in
let t = filter_optional cx ~trace reason_prop t in
rec_flow cx trace (to_obj, SetPropT (
unknown_use, reason_prop, propref, Normal, t, None
))
| None ->
match Property.read_t p, to_obj with
| Some _, (DefT (_, ObjT ob)) when not ob.flags.frozen ->
Context.set_prop cx ob.props_tmap x p
| Some t, to_obj ->
Copy link
Contributor Author

@villesau villesau Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/facebook/flow/blob/master/tests/destructuring/destructuring.js#L61 does not work without | Some t, to_obj -> branch because this generates OpenT instead of ObjT. Otherwise | Some t, to_obj -> branch could be unnecessary altogether.

let propref = Named (reason_prop, x) in
let t = filter_optional cx ~trace reason_prop t in
rec_flow cx trace (to_obj, SetPropT (
unknown_use, reason_prop, propref, Normal, t, None
))
| None, _ ->
add_output cx ~trace (FlowError.EPropAccess (
(reason_prop, reason_op), Some x, Property.polarity p, Read, unknown_use
))
Expand Down
3 changes: 3 additions & 0 deletions tests/friendly_errors/issue_7210.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
type Foo = {| +foo: ?string |};
const test = (bar: Foo): Foo => ({ foo: 'foo', ...bar});
9 changes: 9 additions & 0 deletions tests/friendly_errors/issue_7242.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow

type Ob = {|+foo: {| +bar: string |}|};
declare var update: Ob;
({ foo: { bar: 'valid' }, ...update }: Ob); // Ok

type NestedOb = {|+foo: {| +bar: {| +foo: number |} |}|};
declare var update2: NestedOb;
({ foo: { bar: { foo: 123 } }, ...update2 }: NestedOb); // Ok
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if firendly_errors folder is correct folder for these, but https://github.com/facebook/flow/blob/master/tests/friendly_errors/prop-variance.js is perhaps the closest to above tests that I found.

Empty file.
1 change: 1 addition & 0 deletions tests/inexact-object-spread/inexact-object-spread.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Found 0 errors
15 changes: 15 additions & 0 deletions tests/inexact-object-spread/issue_6751.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* @flow */

type A = { foo: ?string };

function foo(a: A) {
}

const a: A = { foo: 'foo' };
const b = { foo: 'foo' };

foo({ ...a, foo: 'foo' }); // OK
foo({ foo: 'foo', ...a }); // OK

foo({ ...b, foo: 'foo' }); // OK
foo({ foo: 'foo', ...b }); // OK
Loading