Skip to content

Commit

Permalink
[flow][refactor] rename "cond" param of statement.ml to "encl_ctx"
Browse files Browse the repository at this point in the history
Summary:
Before this change the `cond` parameter in several function in statement.ml would represent the conditional syntactic context. It only had two variants, or was absent, which meant that we were not in a conditional context.

Moving forward we will leverage this parameter to inform us about the enclosing syntactic context. The information we will need for natural inference extends beyond just being in a conditional context. For example, we care about whether we are an an indexer context. To accommodate for this, we rename the type `Type.cond_context` to `Type.enclosing_context`. We also rename the parameter `cond` to `encl_ctx` in statement.ml.

Also, to make sure I've addressed all cites where this was used before, I've changed its type from optional to non-optional, but I've included a `NoContext` variant.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D70802814

fbshipit-source-id: 13d39f21054f8cf6312bd58af24cd812286ffab8
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 8, 2025
1 parent d08c3fd commit 501116a
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 136 deletions.
5 changes: 3 additions & 2 deletions src/services/autocomplete/autocomplete_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module Inference = struct
let type_of_match_member_pattern cx loc mem =
Match_pattern.type_of_member_pattern
cx
~on_identifier:(Statement.identifier ~cond:None)
~on_identifier:(Statement.identifier ~encl_ctx:Type.NoContext)
~on_expression:Statement.expression
(loc, mem)
end
Expand Down Expand Up @@ -966,7 +966,8 @@ class process_request_searcher cx ~from_trigger_character ~cursor =
let member_loc = Some (compute_member_loc ~expr_loc:loc ~obj_loc:base_loc) in
let obj_type () =
match base with
| BaseIdentifier (loc, id) -> Inference.type_of_identifier ~cond:None cx loc id
| BaseIdentifier (loc, id) ->
Inference.type_of_identifier ~encl_ctx:Type.NoContext cx loc id
| BaseMember (loc, mem) -> Inference.type_of_match_member_pattern cx loc mem
in
(match property with
Expand Down
40 changes: 20 additions & 20 deletions src/typing/env_resolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ let try_cache : 'l. check:(unit -> 'l) -> cache:('l -> unit) -> Context.t -> 'l
cache e;
e

let expression cx ?cond ?decl exp =
let expression cx ?encl_ctx ?decl exp =
let cache = Context.node_cache cx in
let ((_, t), _) =
try_cache
cx
~check:(fun () -> Statement.expression ?cond ?decl cx exp)
~check:(fun () -> Statement.expression ?encl_ctx ?decl cx exp)
~cache:(Node_cache.set_expression cache)
in
t
Expand Down Expand Up @@ -102,15 +102,15 @@ let resolve_annotation cx tparams_map ?(react_deep_read_only = None) anno =
if Context.typing_mode cx = Context.CheckingMode then Node_cache.set_annotation cache anno;
t

let rec synthesizable_expression cx ?cond exp =
let rec synthesizable_expression cx ?(encl_ctx = Type.NoContext) exp =
let open Ast.Expression in
match exp with
| (loc, Identifier (_, name)) -> Statement.identifier cx ~cond name loc
| (loc, StringLiteral lit) -> Statement.string_literal cx ~cond loc lit
| (loc, BooleanLiteral lit) -> Statement.boolean_literal cx ~cond loc lit
| (loc, Identifier (_, name)) -> Statement.identifier cx ~encl_ctx name loc
| (loc, StringLiteral lit) -> Statement.string_literal cx ~encl_ctx loc lit
| (loc, BooleanLiteral lit) -> Statement.boolean_literal cx ~encl_ctx loc lit
| (loc, NullLiteral _) -> Statement.null_literal loc
| (loc, NumberLiteral lit) -> Statement.number_literal cx ~cond loc lit
| (loc, BigIntLiteral lit) -> Statement.bigint_literal cx ~cond loc lit
| (loc, NumberLiteral lit) -> Statement.number_literal cx ~encl_ctx loc lit
| (loc, BigIntLiteral lit) -> Statement.bigint_literal cx ~encl_ctx loc lit
| (loc, RegExpLiteral _) -> Statement.regexp_literal cx loc
| (loc, ModuleRefLiteral lit) ->
let (t, _lit) = Statement.module_ref_literal cx loc lit in
Expand All @@ -127,7 +127,7 @@ let rec synthesizable_expression cx ?cond exp =
comments = _;
}
) ->
let t = synthesizable_expression cx ?cond _object in
let t = synthesizable_expression cx ~encl_ctx _object in
let tout =
match Refinement.get ~allow_optional:false cx exp loc with
| Some t -> t
Expand All @@ -138,14 +138,14 @@ let rec synthesizable_expression cx ?cond exp =
Statement.get_prop
~use_op (* TODO(jmbrown) This feels incorrect *)
~hint:(Type_env.get_hint cx loc)
~cond:None
~encl_ctx:NoContext
cx
expr_reason
t
(prop_reason, name)
in
tout
| _ -> expression cx ?cond exp
| _ -> expression cx ~encl_ctx exp

let mk_selector_reason_has_default cx loc = function
| Selector.Elem { index = n; has_default } ->
Expand Down Expand Up @@ -803,7 +803,7 @@ let rec resolve_binding cx reason loc b =
AnyT (mk_reason RAnyImplicit loc, AnyError (Some MissingAnnotation))
| Root (For (kind, exp)) ->
let reason = mk_reason (RCustom "for-in") loc (*TODO: loc should be loc of loop *) in
let right_t = expression cx ~cond:OtherTest exp in
let right_t = expression cx ~encl_ctx:OtherTest exp in
(match kind with
| In ->
TypeAssertions.assert_for_in_rhs cx right_t;
Expand Down Expand Up @@ -1037,22 +1037,22 @@ let resolve_type_param cx id_loc =

let resolve_chain_expression cx ~cond exp =
let cache = Context.node_cache cx in
let cond =
let encl_ctx =
match cond with
| NonConditionalContext -> None
| OtherConditionalTest -> Some OtherTest
| NonConditionalContext -> NoContext
| OtherConditionalTest -> OtherTest
in
let (t, _, exp) = Statement.optional_chain ~cond cx exp in
let (t, _, exp) = Statement.optional_chain ~encl_ctx cx exp in
Node_cache.set_expression cache exp;
t

let resolve_write_expression cx ~cond exp =
let cond =
let encl_ctx =
match cond with
| NonConditionalContext -> None
| OtherConditionalTest -> Some OtherTest
| NonConditionalContext -> NoContext
| OtherConditionalTest -> OtherTest
in
synthesizable_expression cx ?cond exp
synthesizable_expression cx ~encl_ctx exp

let resolve_generator_next cx reason gen =
let open TypeUtil in
Expand Down
2 changes: 1 addition & 1 deletion src/typing/func_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ struct
let use_op = Frame (ImplicitTypeParam, use_op) in
(use_op, t, None)
| Func.FieldInit e ->
let (((_, t), _) as ast) = Statement.expression ?cond:None cx e in
let (((_, t), _) as ast) = Statement.expression ~encl_ctx:Type.NoContext cx e in
let body = mk_expression_reason e in
let use_op = Op (InitField { op = reason_fn; body }) in
(use_op, t, Some ast)
Expand Down
19 changes: 15 additions & 4 deletions src/typing/primitive_literal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@
module Ast = Flow_ast

type syntactic_flags = {
cond: Type.cond_context option;
encl_ctx: Type.enclosing_context;
decl: Ast.Variable.kind option;
as_const: bool;
frozen: Type.frozen_kind;
has_hint: bool Lazy.t;
}

let empty_syntactic_flags =
{ cond = None; decl = None; as_const = false; frozen = Type.NotFrozen; has_hint = lazy false }
{
encl_ctx = Type.NoContext;
decl = None;
as_const = false;
frozen = Type.NotFrozen;
has_hint = lazy false;
}

let mk_syntactic_flags
?cond ?decl ?(as_const = false) ?(frozen = Type.NotFrozen) ?(has_hint = lazy false) () =
{ cond; decl; as_const; frozen; has_hint }
?(encl_ctx = Type.NoContext)
?decl
?(as_const = false)
?(frozen = Type.NotFrozen)
?(has_hint = lazy false)
() =
{ encl_ctx; decl; as_const; frozen; has_hint }

let loc_has_hint _cx _loc = (* TODO *) false
4 changes: 2 additions & 2 deletions src/typing/primitive_literal.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module Ast = Flow_ast

type syntactic_flags = {
cond: Type.cond_context option;
encl_ctx: Type.enclosing_context;
decl: Ast.Variable.kind option;
as_const: bool;
frozen: Type.frozen_kind;
Expand All @@ -18,7 +18,7 @@ type syntactic_flags = {
val empty_syntactic_flags : syntactic_flags

val mk_syntactic_flags :
?cond:Type.cond_context ->
?encl_ctx:Type.enclosing_context ->
?decl:Ast.Variable.kind ->
?as_const:bool ->
?frozen:Type.frozen_kind ->
Expand Down
Loading

0 comments on commit 501116a

Please sign in to comment.