Skip to content

Commit

Permalink
[flow][name-def] pass "conditional" context to more expressions in co…
Browse files Browse the repository at this point in the history
…nditional positions

Summary:
In upcoming on natural inference, we need to determine if a primitive literals or variables appears in a context where increased precision is necessary.

Consider for example the code
```
const one = 1;
declare var x: number;
if (x === one) {
  x as 1; // okay
}
```
The position of `one` in the conditional needs to be considered as high precision, in other words we'd like to infer the instance of `one` there as `1` instead of `number`. This will allow the behavior of actually refining `x` to also be `1` and thus allow the cast, like we do today.

This diff achieves this by pass a "conditional" context to a few more cases in Name_def:
* members of the binary expression in `visit_binary_expression`
* switch statement cases

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D70640801

fbshipit-source-id: 34d7cbdc3e04e7f5c63d9713e79f41f01e7a40f4
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 5, 2025
1 parent 972b44a commit 36f36ce
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2993,16 +2993,16 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
ignore @@ this#expression value)
~on_member_eq_other:(fun expr value ->
this#visit_expression ~hints:[] ~cond expr;
ignore @@ this#expression value)
this#visit_expression ~hints:[] ~cond value)
~on_other_eq_member:(fun value expr ->
this#visit_expression ~hints:[] ~cond expr;
ignore @@ this#expression value)
this#visit_expression ~hints:[] ~cond value)
~is_switch_cond_context:false
~strict:false
~sense:false
~on_other_eq_test:(fun left right ->
ignore @@ this#expression left;
ignore @@ this#expression right)
ignore @@ this#visit_expression ~hints:[] ~cond left;
ignore @@ this#visit_expression ~hints:[] ~cond right)
ALoc.none
left
right
Expand Down Expand Up @@ -3148,6 +3148,13 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
);
res

method! switch_case case =
let open Ast.Statement.Switch.Case in
let (_loc, { test; consequent; comments = _ }) = case in
Base.Option.iter ~f:(this#visit_expression ~hints:[] ~cond:OtherConditionalTest) test;
ignore @@ this#statement_list consequent;
case

method! match_expression loc _ = fail loc "Should be visited by visit_match_expression"

method private visit_match_expression ~hints x =
Expand Down

0 comments on commit 36f36ce

Please sign in to comment.