Skip to content

Commit

Permalink
[resolved-env] Type check chain expressions in the first argument of …
Browse files Browse the repository at this point in the history
…invariant call under conditional context

Summary:
Changelog: [internal]

When checking `invariant(expr)`, `expr` should be in conditional context.

Reviewed By: mvitousek

Differential Revision:
D37727359 (6a0fd51)

------------------------------------------------------------------------
(from 945195ea053a9ba00fc373c610afd961f4de6c76)

fbshipit-source-id: a98cf0ec1641dd0e989b5c23921b6a4d190f2ef1
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jul 16, 2022
1 parent c2f7f23 commit 760e3cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1191,18 +1191,36 @@ class def_finder env_entries providers toplevel_scope =
in
visit_callee callee;
Base.Option.iter targs ~f:(fun targs -> ignore @@ this#call_type_args targs);
let call_argumemts_hint = Hint_t (ValueHint callee) in
Base.List.iteri arguments ~f:(fun i arg ->
let hint = decompose_hint (Decomp_FuncParam i) call_argumemts_hint in
match arg with
if Flow_ast_utils.is_call_to_invariant callee then
Base.List.iteri arguments ~f:(fun i -> function
| Ast.Expression.Expression expr ->
this#visit_expression ~hint ~cond:NonConditionalContext expr
let cond =
if i = 0 then
OtherConditionalTest
else
NonConditionalContext
in
(* In invariant(...) call, the first argument is under conditional context. *)
this#visit_expression ~hint:Hint_None ~cond expr
| Ast.Expression.Spread (_, spread) ->
this#visit_expression
~hint
~hint:Hint_None
~cond:NonConditionalContext
spread.Ast.Expression.SpreadElement.argument
)
)
else
let call_argumemts_hint = Hint_t (ValueHint callee) in
Base.List.iteri arguments ~f:(fun i arg ->
let hint = decompose_hint (Decomp_FuncParam i) call_argumemts_hint in
match arg with
| Ast.Expression.Expression expr ->
this#visit_expression ~hint ~cond:NonConditionalContext expr
| Ast.Expression.Spread (_, spread) ->
this#visit_expression
~hint
~cond:NonConditionalContext
spread.Ast.Expression.SpreadElement.argument
)

method! optional_call _ expr =
let open Ast.Expression.OptionalCall in
Expand Down
6 changes: 6 additions & 0 deletions tests/refinements/missing-property-cond.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ function foo9() {
function foo10() {
if (null.q) {} // error: property `q` on null
}

function foo11() {
declare function invariant(a: mixed): void;
declare var b: mixed;
invariant(b != null && b.foo != null); // ok
}

0 comments on commit 760e3cf

Please sign in to comment.