Skip to content

Commit 501116a

Browse files
panagosg7facebook-github-bot
authored andcommitted
[flow][refactor] rename "cond" param of statement.ml to "encl_ctx"
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
1 parent d08c3fd commit 501116a

File tree

10 files changed

+169
-136
lines changed

10 files changed

+169
-136
lines changed

src/services/autocomplete/autocomplete_js.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module Inference = struct
162162
let type_of_match_member_pattern cx loc mem =
163163
Match_pattern.type_of_member_pattern
164164
cx
165-
~on_identifier:(Statement.identifier ~cond:None)
165+
~on_identifier:(Statement.identifier ~encl_ctx:Type.NoContext)
166166
~on_expression:Statement.expression
167167
(loc, mem)
168168
end
@@ -966,7 +966,8 @@ class process_request_searcher cx ~from_trigger_character ~cursor =
966966
let member_loc = Some (compute_member_loc ~expr_loc:loc ~obj_loc:base_loc) in
967967
let obj_type () =
968968
match base with
969-
| BaseIdentifier (loc, id) -> Inference.type_of_identifier ~cond:None cx loc id
969+
| BaseIdentifier (loc, id) ->
970+
Inference.type_of_identifier ~encl_ctx:Type.NoContext cx loc id
970971
| BaseMember (loc, mem) -> Inference.type_of_match_member_pattern cx loc mem
971972
in
972973
(match property with

src/typing/env_resolution.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ let try_cache : 'l. check:(unit -> 'l) -> cache:('l -> unit) -> Context.t -> 'l
4545
cache e;
4646
e
4747

48-
let expression cx ?cond ?decl exp =
48+
let expression cx ?encl_ctx ?decl exp =
4949
let cache = Context.node_cache cx in
5050
let ((_, t), _) =
5151
try_cache
5252
cx
53-
~check:(fun () -> Statement.expression ?cond ?decl cx exp)
53+
~check:(fun () -> Statement.expression ?encl_ctx ?decl cx exp)
5454
~cache:(Node_cache.set_expression cache)
5555
in
5656
t
@@ -102,15 +102,15 @@ let resolve_annotation cx tparams_map ?(react_deep_read_only = None) anno =
102102
if Context.typing_mode cx = Context.CheckingMode then Node_cache.set_annotation cache anno;
103103
t
104104

105-
let rec synthesizable_expression cx ?cond exp =
105+
let rec synthesizable_expression cx ?(encl_ctx = Type.NoContext) exp =
106106
let open Ast.Expression in
107107
match exp with
108-
| (loc, Identifier (_, name)) -> Statement.identifier cx ~cond name loc
109-
| (loc, StringLiteral lit) -> Statement.string_literal cx ~cond loc lit
110-
| (loc, BooleanLiteral lit) -> Statement.boolean_literal cx ~cond loc lit
108+
| (loc, Identifier (_, name)) -> Statement.identifier cx ~encl_ctx name loc
109+
| (loc, StringLiteral lit) -> Statement.string_literal cx ~encl_ctx loc lit
110+
| (loc, BooleanLiteral lit) -> Statement.boolean_literal cx ~encl_ctx loc lit
111111
| (loc, NullLiteral _) -> Statement.null_literal loc
112-
| (loc, NumberLiteral lit) -> Statement.number_literal cx ~cond loc lit
113-
| (loc, BigIntLiteral lit) -> Statement.bigint_literal cx ~cond loc lit
112+
| (loc, NumberLiteral lit) -> Statement.number_literal cx ~encl_ctx loc lit
113+
| (loc, BigIntLiteral lit) -> Statement.bigint_literal cx ~encl_ctx loc lit
114114
| (loc, RegExpLiteral _) -> Statement.regexp_literal cx loc
115115
| (loc, ModuleRefLiteral lit) ->
116116
let (t, _lit) = Statement.module_ref_literal cx loc lit in
@@ -127,7 +127,7 @@ let rec synthesizable_expression cx ?cond exp =
127127
comments = _;
128128
}
129129
) ->
130-
let t = synthesizable_expression cx ?cond _object in
130+
let t = synthesizable_expression cx ~encl_ctx _object in
131131
let tout =
132132
match Refinement.get ~allow_optional:false cx exp loc with
133133
| Some t -> t
@@ -138,14 +138,14 @@ let rec synthesizable_expression cx ?cond exp =
138138
Statement.get_prop
139139
~use_op (* TODO(jmbrown) This feels incorrect *)
140140
~hint:(Type_env.get_hint cx loc)
141-
~cond:None
141+
~encl_ctx:NoContext
142142
cx
143143
expr_reason
144144
t
145145
(prop_reason, name)
146146
in
147147
tout
148-
| _ -> expression cx ?cond exp
148+
| _ -> expression cx ~encl_ctx exp
149149

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

10381038
let resolve_chain_expression cx ~cond exp =
10391039
let cache = Context.node_cache cx in
1040-
let cond =
1040+
let encl_ctx =
10411041
match cond with
1042-
| NonConditionalContext -> None
1043-
| OtherConditionalTest -> Some OtherTest
1042+
| NonConditionalContext -> NoContext
1043+
| OtherConditionalTest -> OtherTest
10441044
in
1045-
let (t, _, exp) = Statement.optional_chain ~cond cx exp in
1045+
let (t, _, exp) = Statement.optional_chain ~encl_ctx cx exp in
10461046
Node_cache.set_expression cache exp;
10471047
t
10481048

10491049
let resolve_write_expression cx ~cond exp =
1050-
let cond =
1050+
let encl_ctx =
10511051
match cond with
1052-
| NonConditionalContext -> None
1053-
| OtherConditionalTest -> Some OtherTest
1052+
| NonConditionalContext -> NoContext
1053+
| OtherConditionalTest -> OtherTest
10541054
in
1055-
synthesizable_expression cx ?cond exp
1055+
synthesizable_expression cx ~encl_ctx exp
10561056

10571057
let resolve_generator_next cx reason gen =
10581058
let open TypeUtil in

src/typing/func_sig.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ struct
465465
let use_op = Frame (ImplicitTypeParam, use_op) in
466466
(use_op, t, None)
467467
| Func.FieldInit e ->
468-
let (((_, t), _) as ast) = Statement.expression ?cond:None cx e in
468+
let (((_, t), _) as ast) = Statement.expression ~encl_ctx:Type.NoContext cx e in
469469
let body = mk_expression_reason e in
470470
let use_op = Op (InitField { op = reason_fn; body }) in
471471
(use_op, t, Some ast)

src/typing/primitive_literal.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88
module Ast = Flow_ast
99

1010
type syntactic_flags = {
11-
cond: Type.cond_context option;
11+
encl_ctx: Type.enclosing_context;
1212
decl: Ast.Variable.kind option;
1313
as_const: bool;
1414
frozen: Type.frozen_kind;
1515
has_hint: bool Lazy.t;
1616
}
1717

1818
let empty_syntactic_flags =
19-
{ cond = None; decl = None; as_const = false; frozen = Type.NotFrozen; has_hint = lazy false }
19+
{
20+
encl_ctx = Type.NoContext;
21+
decl = None;
22+
as_const = false;
23+
frozen = Type.NotFrozen;
24+
has_hint = lazy false;
25+
}
2026

2127
let mk_syntactic_flags
22-
?cond ?decl ?(as_const = false) ?(frozen = Type.NotFrozen) ?(has_hint = lazy false) () =
23-
{ cond; decl; as_const; frozen; has_hint }
28+
?(encl_ctx = Type.NoContext)
29+
?decl
30+
?(as_const = false)
31+
?(frozen = Type.NotFrozen)
32+
?(has_hint = lazy false)
33+
() =
34+
{ encl_ctx; decl; as_const; frozen; has_hint }
2435

2536
let loc_has_hint _cx _loc = (* TODO *) false

src/typing/primitive_literal.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module Ast = Flow_ast
99

1010
type syntactic_flags = {
11-
cond: Type.cond_context option;
11+
encl_ctx: Type.enclosing_context;
1212
decl: Ast.Variable.kind option;
1313
as_const: bool;
1414
frozen: Type.frozen_kind;
@@ -18,7 +18,7 @@ type syntactic_flags = {
1818
val empty_syntactic_flags : syntactic_flags
1919

2020
val mk_syntactic_flags :
21-
?cond:Type.cond_context ->
21+
?encl_ctx:Type.enclosing_context ->
2222
?decl:Ast.Variable.kind ->
2323
?as_const:bool ->
2424
?frozen:Type.frozen_kind ->

0 commit comments

Comments
 (0)