Skip to content

Commit

Permalink
[flow] abstract get_def_process_location mapper over annot position
Browse files Browse the repository at this point in the history
Summary:
This diff changes the signature of `get_def_process_location` mapper from a
```
 [ALoc.t, ALoc.t * Type.t, ALoc.t, ALoc.t * Type.t] Flow_polymorphic_ast_mapper.mapper
```
to a
```
[ALoc.t, 'T, ALoc.t, 'T] Typed_ast_finder.enclosing_node_mapper
```
The main change is in the type of the annotation position which is now abstract. For this to work we need to change places that before inspected this position and expected to find an `ALoc.t * Type.t`, to now first call into one of the following (virtual) methods
```
method virtual loc_of_annot : 'T -> ALoc.t
method virtual type_from_enclosing_node : 'T -> Type.t
method virtual get_module_t : 'T -> ALoc.t Ast.StringLiteral.t -> Type.t
method virtual remote_name_def_loc_of_import_named_specifier: (ALoc.t, 'T) Ast.Statement.ImportDeclaration.named_specifier -> ALoc.t option
method virtual remote_default_name_def_loc_of_import_declaration: 'T -> ALoc.t * (ALoc.t, 'T) Ast.Statement.ImportDeclaration.t -> ALoc.t option
method virtual component_name_of_jsx_element: 'T -> (ALoc.t, 'T) Ast.JSX.element -> ALoc.t * Type.t
```
Each class that finalizes `searcher`, will need to implement these methods. `typed_ast_searcher` which is included in this diff restores the behavior of `searcher` before this change. All these operations can be implemented by looking up relevant information in the typed AST.

Follow up diffs implement a searcher that create typed AST nodes on-demand, by calling facilities introduced in the previous diff.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D55053045

fbshipit-source-id: c52b0ea1eb90e9a4c86374970787cc7c60946ea6
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 20, 2024
1 parent 0fdc54f commit bd85ad0
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 65 deletions.
10 changes: 10 additions & 0 deletions src/parser_utils/ast_loc_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ let loc_to_aloc_mapper : (Loc.t, Loc.t, ALoc.t, ALoc.t) Flow_polymorphic_ast_map

method on_type_annot = ALoc.of_loc
end

let loc_to_aloc_unit_mapper :
(Loc.t, Loc.t, ALoc.t, ALoc.t * unit) Flow_polymorphic_ast_mapper.mapper =
object
inherit [Loc.t, Loc.t, ALoc.t, ALoc.t * unit] Flow_polymorphic_ast_mapper.mapper

method on_loc_annot = ALoc.of_loc

method on_type_annot x = (ALoc.of_loc x, ())
end
3 changes: 3 additions & 0 deletions src/parser_utils/ast_loc_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
(* Converts a (Loc.t, Loc.t) AST into a (ALoc.t, ALoc.t) AST. Leaves the underlying representation
* of the contained ALoc.ts concrete. *)
val loc_to_aloc_mapper : (Loc.t, Loc.t, ALoc.t, ALoc.t) Flow_polymorphic_ast_mapper.mapper

val loc_to_aloc_unit_mapper :
(Loc.t, Loc.t, ALoc.t, ALoc.t * unit) Flow_polymorphic_ast_mapper.mapper
2 changes: 1 addition & 1 deletion src/services/get_def/getDef_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ end

let get_def ~options ~loc_of_aloc ~cx ~file_sig ~ast ~typed_ast ~purpose requested_loc =
let require_loc_map = File_sig.require_loc_map file_sig in
let is_legit_require (source_aloc, _) =
let is_legit_require source_aloc =
let source_loc = loc_of_aloc source_aloc in
SMap.exists (fun _ locs -> List.exists (fun loc -> loc = source_loc) locs) require_loc_map
in
Expand Down
Loading

0 comments on commit bd85ad0

Please sign in to comment.