Skip to content

Commit

Permalink
[flow][match] Guards can apply refinements
Browse files Browse the repository at this point in the history
Summary:
Match pattern gaurds can apply refinements to values which are not the match argument.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D67779047

fbshipit-source-id: f5ed21536484e92b8a0e58690294eb8584d8f077
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 3, 2025
1 parent 238f030 commit 242f5d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/analysis/env_builder/name_resolver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2955,8 +2955,13 @@ module Make (Context : C) (FlowAPIUtils : F with type cx = Context.t) :
ignore @@ this#expression arg;
let completion_state =
this#run_to_completion (fun () ->
Base.Option.iter guard ~f:(fun guard -> ignore @@ this#expression guard);
ignore @@ this#expression body
match guard with
| Some guard ->
this#push_refinement_scope empty_refinements;
ignore @@ this#expression_refinement guard;
ignore @@ this#expression body;
this#pop_refinement_scope ()
| None -> ignore @@ this#expression body
)
in
completion_states := completion_state :: !completion_states;
Expand Down
11 changes: 11 additions & 0 deletions tests/match/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ function f2() {

out as number; // OK
}

// Guards can refine values which are not the argument
{
declare const y: number | string;

const out = match (x) {
1 if typeof y === 'number': y as number, // OK
const a if a === 1: a as 1,
_: 0,
};
}

0 comments on commit 242f5d7

Please sign in to comment.