Skip to content

Commit

Permalink
[flow] perform call check on Array [react-rule-unsafe-mutation] errors
Browse files Browse the repository at this point in the history
Summary:
In
```
component Foo(arr: Array<number>) {
    arr.sort((a: number, b: number) => {
        return 1;
    });
    return null;
}
```
the call to `.sort` is an error, but we should still try to populate some type in the entry for `sort`.

This diff makes it so that right after we raise the EPropNotReadable ReactDeepReadOnly error we perform the call as usual.

Changelog: [errors] Flow will now perform a complete function call check even when [react-rule-unsafe-mutation] errors are raised.

Reviewed By: SamChou19815

Differential Revision: D67320626

fbshipit-source-id: fc2c3df9661f43739c405103f59bd57870c0e2c8
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Dec 17, 2024
1 parent 54f9bee commit 1a33f6b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5097,7 +5097,8 @@ struct
(**********************)
(* Array library call *)
(**********************)
| ( DefT (reason, ArrT (ArrayAT { elem_t; react_dro = Some (dro_loc, dro_type); _ })),
| ( DefT
(reason, ArrT (ArrayAT { elem_t; react_dro = Some (dro_loc, dro_type); tuple_view })),
(GetPropT _ | SetPropT _ | MethodT _ | LookupT _)
) -> begin
match u with
Expand All @@ -5122,7 +5123,11 @@ struct
prop_name = Some (OrdinaryName name);
use_op = Frame (ReactDeepReadOnly (dro_loc, dro_type), use_op);
}
)
);
rec_flow
cx
trace
(DefT (reason, ArrT (ArrayAT { elem_t; react_dro = None; tuple_view })), u)
| _ ->
let l =
get_builtin_typeapp
Expand Down
19 changes: 18 additions & 1 deletion tests/react_rules/react_rules.exp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@ References:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]


Error ----------------------------------------------------------------------------------------------------- dro.js:84:13

Cannot call `droarr.push` because number [1] is incompatible with unknown element of empty array [2] in array element of
array element. Arrays are invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-an-arraystring-to-a-function-that-takes-an-arraystring-number.
[incompatible-call]

dro.js:84:13
84| droarr.push([]); // error
^^ [2]

References:
dro.js:83:51
83| declare const droarr: React$Immutable<Array<Array<number>>>;
^^^^^^ [1]


Error ------------------------------------------------------------------------------------------------------ dro.js:85:1

Cannot call `droarr[0].push` because property `push` is not readable. Values annotated using `React.Immutable` [1] are
Expand Down Expand Up @@ -829,4 +846,4 @@ Cannot read `current` from `otherRef` [1] because `ref` values may not be read d



Found 63 errors
Found 64 errors
1 change: 1 addition & 0 deletions tests/type_at_pos_react/.flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
[options]
all=true
no_flowlib=false
component_syntax=true

[strict]
6 changes: 6 additions & 0 deletions tests/type_at_pos_react/dro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
component Foo(arr: Array<number>) {
arr.sort((a: number, b: number) => {
return 1;
});
return null;
}
5 changes: 5 additions & 0 deletions tests/type_at_pos_react/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


# dro.js
printf "dro.js:2:9 = "
assert_ok "$FLOW" type-at-pos dro.js 2 9 --strip-root

# lazy_ref.js
printf "lazy_ref.js:14:9 = "
assert_ok "$FLOW" type-at-pos lazy_ref.js 14 9 --strip-root
Expand Down
2 changes: 2 additions & 0 deletions tests/type_at_pos_react/type_at_pos_react.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dro.js:2:9 = (compareFn?: (a: number, b: number) => number) => Array<number>
dro.js:2:9,2:12
lazy_ref.js:14:9 = {
default: component(ref: RefSetter<{moo(x: string): void}>, ...empty) renders null
}
Expand Down

0 comments on commit 1a33f6b

Please sign in to comment.