Skip to content

Commit

Permalink
[flow] add "decl_kind" field in Name_def.Value
Browse files Browse the repository at this point in the history
Summary:
We will use this to determine if this value corresponds to a const-variable.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D70763512

fbshipit-source-id: 565c2f17318f8946d2a85d17ad79729aaa697381
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 7, 2025
1 parent f7579ce commit 0c99955
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,10 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
let this_write_locs = obj_this_write_locs obj in
begin
match obj_properties_synthesizable ~this_write_locs obj with
| Unsynthesizable -> Some (Value { hints = []; expr = init })
| Unsynthesizable -> Some (Value { hints = []; expr = init; decl_kind = None })
| synthesizable -> Some (ObjectValue { obj_loc = loc; obj; synthesizable })
end
| (Some init, _) -> Some (Value { hints = []; expr = init })
| (Some init, _) -> Some (Value { hints = []; expr = init; decl_kind = Some kind })
| (None, _) -> None
in
let (source, hints) =
Expand Down Expand Up @@ -2051,11 +2051,11 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
this#visit_expression ~hints:(expression_pattern_hints e) ~cond:NonConditionalContext right
| (None, Ast.Pattern.Expression e) ->
let (_ : (_, _) Ast.Pattern.t) = this#assignment_pattern (lhs_loc, lhs_node) in
this#add_destructure_bindings (Value { hints = []; expr = right }) left;
this#add_destructure_bindings (Value { hints = []; expr = right; decl_kind = None }) left;
this#visit_expression ~hints:(expression_pattern_hints e) ~cond:NonConditionalContext right
| (None, _) ->
let (_ : (_, _) Ast.Pattern.t) = this#assignment_pattern (lhs_loc, lhs_node) in
this#add_destructure_bindings (Value { hints = []; expr = right }) left;
this#add_destructure_bindings (Value { hints = []; expr = right; decl_kind = None }) left;
this#visit_expression ~hints:(other_pattern_hints left) ~cond:NonConditionalContext right
| ( Some operator,
Ast.Pattern.Identifier
Expand Down Expand Up @@ -3203,7 +3203,7 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
this#add_ordinary_binding
match_keyword_loc
(mk_reason RMatch match_keyword_loc)
(Binding (Root (Value { hints = []; expr = arg })));
(Binding (Root (Value { hints = []; expr = arg; decl_kind = None })));
let value_hints =
Base.List.foldi cases ~init:IMap.empty ~f:(fun i acc (_, { Case.body; _ }) ->
if expression_is_definitely_synthesizable ~autocomplete_hooks body then
Expand All @@ -3218,7 +3218,7 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
(case_loc, Ast.Expression.Identifier (Flow_ast_utils.match_root_ident case_loc))
in
ignore @@ this#expression match_root;
let acc = Value { hints = []; expr = match_root } in
let acc = Value { hints = []; expr = match_root; decl_kind = None } in
this#add_match_destructure_bindings acc pattern;
ignore @@ super#match_pattern pattern;
run_opt this#expression guard;
Expand All @@ -3235,13 +3235,13 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
this#add_ordinary_binding
match_keyword_loc
(mk_reason RMatch match_keyword_loc)
(Binding (Root (Value { hints = []; expr = arg })));
(Binding (Root (Value { hints = []; expr = arg; decl_kind = None })));
Base.List.iter cases ~f:(fun (case_loc, { Case.pattern; body; guard; comments = _ }) ->
let match_root =
(case_loc, Ast.Expression.Identifier (Flow_ast_utils.match_root_ident case_loc))
in
ignore @@ this#expression match_root;
let acc = Value { hints = []; expr = match_root } in
let acc = Value { hints = []; expr = match_root; decl_kind = None } in
this#add_match_destructure_bindings acc pattern;
ignore @@ super#match_pattern pattern;
run_opt this#expression guard;
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/env_builder/name_def_ordering.ml
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ struct
in
let depends_of_root state = function
| Annotation { annot; tparams_map; _ } -> depends_of_annotation tparams_map annot state
| Value { hints; expr } ->
| Value { hints; expr; decl_kind = _ } ->
let state = depends_of_hints state hints in
depends_of_expression expr state
| ObjectValue { obj; synthesizable = ObjectSynthesizable _; _ } ->
Expand Down
1 change: 1 addition & 0 deletions src/analysis/env_builder/name_def_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type dro_annot =
type value = {
hints: ast_hints;
expr: (ALoc.t, ALoc.t) Ast.Expression.t;
decl_kind: Ast.Variable.kind option;
}

type root =
Expand Down
6 changes: 3 additions & 3 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 exp =
let expression cx ?cond ?decl exp =
let cache = Context.node_cache cx in
let ((_, t), _) =
try_cache
cx
~check:(fun () -> Statement.expression ?cond cx exp)
~check:(fun () -> Statement.expression ?cond ?decl cx exp)
~cache:(Node_cache.set_expression cache)
in
t
Expand Down Expand Up @@ -466,7 +466,7 @@ let rec resolve_binding cx reason loc b =
TypeUtil.optional t
else
t
| Root (Value { hints = _; expr }) -> expression cx expr
| Root (Value { hints = _; expr; decl_kind }) -> expression cx ?decl:decl_kind expr
| Root (ObjectValue { obj_loc = loc; obj; synthesizable = ObjectSynthesizable _ }) ->
let open Ast.Expression.Object in
let resolve_prop ~bind_this ~prop_loc ~fn_loc fn =
Expand Down

0 comments on commit 0c99955

Please sign in to comment.