Skip to content

Commit

Permalink
[flow][refactor] Explicitly handle module defs
Browse files Browse the repository at this point in the history
Summary:
Compare to the only remaining `Get_def_request.Type` for pattern_identifier within requires, these changes ones' expected behavior is well defined: all of them are supposed to jump to the module of the import, and we don't necessarily care that the exact location in the module, but we might care that they are different from other reason based jumps.

Changelog: [internal]

Reviewed By: gkz

Differential Revision: D68166500

fbshipit-source-id: 06f232e59390d167282103de7b7a9820ef8a55f2
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 14, 2025
1 parent 35328df commit db6ad29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/services/get_def/getDef_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ let get_def ~loc_of_aloc ~cx ~file_sig ~ast ~available_ast ~purpose requested_lo
with
| OwnNamedDef (aloc, name) -> Def (LocSet.singleton (loc_of_aloc aloc), Some name)
| OwnUnnamedDef aloc -> Def (LocSet.singleton (loc_of_aloc aloc), None)
| ModuleDef t ->
(match Get_def_process_location.process_type_request cx t with
| Ok res_loc -> Def (LocSet.singleton (loc_of_aloc res_loc), None)
| Error e -> Def_error e)
| Request request -> begin
match
process_request
Expand Down
25 changes: 11 additions & 14 deletions src/services/get_def/get_def_process_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type internal_error =
type 'loc result =
| OwnNamedDef of 'loc * (* name *) string
| OwnUnnamedDef of 'loc
| ModuleDef of (Type.t[@opaque])
| Request of ('loc, 'loc * (Type.t[@opaque])) Get_def_request.t
| Empty of string
| LocNotFound
Expand Down Expand Up @@ -123,6 +124,11 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
found_loc_ <- OwnUnnamedDef loc;
raise Found

method private module_def : 'a. Type.t -> 'a =
fun t ->
found_loc_ <- ModuleDef t;
raise Found

method private found_empty : 'a. string -> 'a =
fun x ->
found_loc_ <- Empty x;
Expand Down Expand Up @@ -167,8 +173,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~

method! import_source source_annot lit =
if this#annot_covers_target source_annot then begin
let source_annot = (this#loc_of_annot source_annot, this#get_module_t source_annot lit) in
this#request (Get_def_request.Type { annot = source_annot; name = None })
this#module_def (this#get_module_t source_annot lit)
end;
super#import_source source_annot lit

Expand Down Expand Up @@ -207,10 +212,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
| Get_def_types.Purpose.GoToDefinition
| Get_def_types.Purpose.JSDoc ->
let t = this#type_from_enclosing_node source_annot in
this#request
(Get_def_request.Type
{ annot = (this#loc_of_annot source_annot, t); name = Some name }
)
this#module_def t
| Get_def_types.Purpose.FindReferences ->
ignore @@ this#own_named_def (this#loc_of_annot name_annot) name
)
Expand Down Expand Up @@ -266,10 +268,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
| Get_def_types.Purpose.GoToDefinition
| Get_def_types.Purpose.JSDoc ->
let t = this#type_from_enclosing_node source_annot in
this#request
(Get_def_request.Type
{ annot = (this#loc_of_annot source_annot, t); name = Some name }
)
this#module_def t
| Get_def_types.Purpose.FindReferences ->
ignore @@ this#own_named_def (this#loc_of_annot name_annot) name
)
Expand All @@ -278,8 +277,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~

method! export_source source_annot lit =
if this#annot_covers_target source_annot then begin
let source_annot = (this#loc_of_annot source_annot, this#get_module_t source_annot lit) in
this#request (Get_def_request.Type { annot = source_annot; name = None })
this#module_def (this#get_module_t source_annot lit)
end;
super#export_source source_annot lit

Expand Down Expand Up @@ -525,8 +523,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
_;
}
when this#is_legit_require source_annot ->
let annot = (this#loc_of_annot annot, this#type_from_enclosing_node annot) in
this#request (Get_def_request.Type { annot; name = None })
this#module_def (this#type_from_enclosing_node annot)
| _ -> super#expression (annot, expr)
else
(* it is tempting to not recurse here, but comments are not included in
Expand Down
1 change: 1 addition & 0 deletions src/services/get_def/get_def_process_location.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type internal_error =
type 'loc result =
| OwnNamedDef of 'loc * (* name *) string
| OwnUnnamedDef of 'loc
| ModuleDef of Type.t
| Request of ('loc, 'loc * (Type.t[@opaque])) Get_def_request.t
| Empty of string
| LocNotFound
Expand Down

0 comments on commit db6ad29

Please sign in to comment.