Skip to content

Commit

Permalink
[flow][easy] Flow_ast -> Ast in get_def_process_location
Browse files Browse the repository at this point in the history
Reviewed By: SamChou19815

Differential Revision: D54868847

fbshipit-source-id: 50b3d310231f3a9463abd5f9dd2040e0c6e4dfd4
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 13, 2024
1 parent 7dfcec6 commit f55b7a9
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions src/services/get_def/get_def_process_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*)

module Ast = Flow_ast

(** stops walking the tree *)
exception Found

Expand Down Expand Up @@ -43,13 +45,13 @@ let rec process_type_request cx =
(** Determines if the given expression is a [require()] call, or a member expression
containing one, like [require('foo').bar]. *)
let rec is_require ~is_legit_require expr =
let open Flow_ast.Expression in
let open Ast.Expression in
match expr with
| (_, Member { Member._object; _ }) -> is_require ~is_legit_require _object
| ( _,
Call
{
Call.callee = (_, Identifier (_, { Flow_ast.Identifier.name = "require"; _ }));
Call.callee = (_, Identifier (_, { Ast.Identifier.name = "require"; _ }));
arguments = (_, { ArgList.arguments = Expression (source_annot, _) :: _; _ });
_;
}
Expand All @@ -59,7 +61,7 @@ let rec is_require ~is_legit_require expr =
| _ -> false

let annot_of_jsx_name =
let open Flow_ast.JSX in
let open Ast.JSX in
function
| Identifier (annot, _)
| NamespacedName (_, NamespacedName.{ name = (annot, _); _ })
Expand Down Expand Up @@ -109,7 +111,7 @@ class searcher
raise Found

method! variable_declarator
~kind ((_, { Flow_ast.Statement.VariableDeclaration.Declarator.id; init }) as x) =
~kind ((_, { Ast.Statement.VariableDeclaration.Declarator.id; init }) as x) =
(* If a variable declarator's initializer contains `require()`, then we want to jump
through it into the imported module. To do this, we set the `in_require_declarator`
flag, which we use when we visit the id, in lieu of parent pointers. *)
Expand All @@ -127,10 +129,10 @@ class searcher
super#import_source source_annot lit

method! import_named_specifier ~import_kind:_ decl =
let open Flow_ast.Statement.ImportDeclaration in
let open Ast.Statement.ImportDeclaration in
let {
local;
remote = (remote_annot, { Flow_ast.Identifier.name; _ });
remote = (remote_annot, { Ast.Identifier.name; _ });
remote_name_def_loc;
kind = _;
} =
Expand All @@ -150,7 +152,7 @@ class searcher
decl

method! import_declaration loc decl =
let open Flow_ast.Statement.ImportDeclaration in
let open Ast.Statement.ImportDeclaration in
let { default; specifiers; source; _ } = decl in
Base.Option.iter default ~f:(fun { identifier = (annot, _); remote_default_name_def_loc } ->
if annot_covers_target annot then
Expand All @@ -160,7 +162,7 @@ class searcher
);
Base.Option.iter specifiers ~f:(function
| ImportNamedSpecifiers _ -> ()
| ImportNamespaceSpecifier (l, ((name_loc, _), { Flow_ast.Identifier.name; _ })) ->
| ImportNamespaceSpecifier (l, ((name_loc, _), { Ast.Identifier.name; _ })) ->
if covers_target l then (
match purpose with
| Get_def_types.Purpose.GoToDefinition ->
Expand All @@ -176,12 +178,11 @@ class searcher
super#export_source source_annot lit

method! member loc expr =
let open Flow_ast.Expression.Member in
let open Ast.Expression.Member in
let { _object; property; comments = _ } = expr in
begin
match property with
| PropertyIdentifier (annot, { Flow_ast.Identifier.name; _ }) when annot_covers_target annot
->
| PropertyIdentifier (annot, { Ast.Identifier.name; _ }) when annot_covers_target annot ->
let (obj_annot, _) = _object in
let force_instance = Flow_ast_utils.is_super_member_access expr in
let result =
Expand All @@ -193,10 +194,10 @@ class searcher
super#member loc expr

method! indexed_access_type expr =
let open Flow_ast.Type.IndexedAccess in
let open Ast.Type.IndexedAccess in
let { _object; index; comments = _ } = expr in
(match index with
| (annot, Flow_ast.Type.StringLiteral { Flow_ast.StringLiteral.value; _ })
| (annot, Ast.Type.StringLiteral { Ast.StringLiteral.value; _ })
when annot_covers_target annot ->
let (obj_annot, _) = _object in
let result =
Expand All @@ -208,12 +209,12 @@ class searcher
| _ -> ());
super#indexed_access_type expr

method! t_identifier ((loc, { Flow_ast.Identifier.name; _ }) as id) =
method! t_identifier ((loc, { Ast.Identifier.name; _ }) as id) =
if annot_covers_target loc then this#request (Get_def_request.Identifier { name; loc });
super#t_identifier id

method! jsx_opening_element elt =
let open Flow_ast.JSX in
let open Ast.JSX in
let (_, Opening.{ name = component_name; attributes; _ }) = elt in
List.iter
(function
Expand All @@ -235,8 +236,7 @@ class searcher
attributes;
super#jsx_opening_element elt

method! jsx_element_name_identifier
((annot, { Flow_ast.JSX.Identifier.name; comments = _ }) as id) =
method! jsx_element_name_identifier ((annot, { Ast.JSX.Identifier.name; comments = _ }) as id) =
if annot_covers_target annot then
this#request (Get_def_request.Identifier { name; loc = annot });
super#jsx_element_name_identifier id
Expand All @@ -254,7 +254,7 @@ class searcher
super#jsx_member_expression expr

method! pattern ?kind ((pat_annot, p) as pat) =
let open Flow_ast.Pattern in
let open Ast.Pattern in
(* In const {foo: bar} = require('some_module'); foo and bar should jump to prop def of foo,
while in other cases, bar should be its own definition. *)
let is_id_pattern_of_obj_key_in_require_declarator = function
Expand All @@ -269,14 +269,14 @@ class searcher
function
| Property (_, { Property.key; pattern; _ }) -> begin
match key with
| Property.StringLiteral (loc, { Flow_ast.StringLiteral.value = name; _ })
| Property.StringLiteral (loc, { Ast.StringLiteral.value = name; _ })
when covers_target loc || is_id_pattern_of_obj_key_in_require_declarator pattern
->
this#request
Get_def_request.(
Member { prop_name = name; object_type = pat_annot; force_instance = false }
)
| Property.Identifier (id_annot, { Flow_ast.Identifier.name; _ })
| Property.Identifier (id_annot, { Ast.Identifier.name; _ })
when annot_covers_target id_annot
|| is_id_pattern_of_obj_key_in_require_declarator pattern ->
this#request
Expand All @@ -292,8 +292,7 @@ class searcher
in
super#pattern ?kind pat

method! pattern_identifier
?kind (annot, ({ Flow_ast.Identifier.name; comments = _ } as name_node)) =
method! pattern_identifier ?kind (annot, ({ Ast.Identifier.name; comments = _ } as name_node)) =
if kind != None && annot_covers_target annot then
if in_require_declarator then
this#request (Get_def_request.Type { annot; name = Some name })
Expand All @@ -302,7 +301,7 @@ class searcher
super#pattern_identifier ?kind (annot, name_node)

method! expression (annot, expr) =
let open Flow_ast in
let open Ast in
let open Expression in
if annot_covers_target annot then
match expr with
Expand Down Expand Up @@ -332,7 +331,7 @@ class searcher

method! type_ (annot, t) =
if annot_covers_target annot then
let open! Flow_ast.Type in
let open! Ast.Type in
match t with
| Any _
| Mixed _
Expand Down Expand Up @@ -379,25 +378,25 @@ class searcher
super#type_ (annot, t)

method! type_param_identifier id =
let (loc, { Flow_ast.Identifier.name; comments = _ }) = id in
let (loc, { Ast.Identifier.name; comments = _ }) = id in
if covers_target loc then this#own_def loc name;
id

method! module_ref_literal mref =
let { Flow_ast.ModuleRefLiteral.require_out; _ } = mref in
let { Ast.ModuleRefLiteral.require_out; _ } = mref in
if annot_covers_target require_out then
this#request (Get_def_request.Type { annot = require_out; name = None })
else
super#module_ref_literal mref

method! enum_member_identifier id =
let (loc, { Flow_ast.Identifier.name; comments = _ }) = id in
let (loc, { Ast.Identifier.name; comments = _ }) = id in
if covers_target loc then this#own_def loc name;
super#enum_member_identifier id

(* object keys would normally hit this#t_identifier; this circumvents that. *)
method! object_key_identifier id =
let (annot, { Flow_ast.Identifier.name; comments = _ }) = id in
let (annot, { Ast.Identifier.name; comments = _ }) = id in
if annot_covers_target annot then this#own_def (loc_of_annot annot) name;
id

Expand All @@ -423,17 +422,17 @@ class searcher
* process the value before the key so that the explicit-non-find in this#object_key_identifier
* doesn't make us miss the variable *)
method! object_property prop =
let open Flow_ast.Expression.Object.Property in
let open Ast.Expression.Object.Property in
(match prop with
| (_, Init { shorthand = true; value; _ }) -> ignore (this#expression value)
| _ -> ());
super#object_property prop

method! new_ expr =
let { Flow_ast.Expression.New.callee = (_, callee); _ } = expr in
let { Ast.Expression.New.callee = (_, callee); _ } = expr in
begin
match callee with
| Flow_ast.Expression.Identifier (annot, _) when annot_covers_target annot ->
| Ast.Expression.Identifier (annot, _) when annot_covers_target annot ->
this#request
Get_def_request.(
Member
Expand Down Expand Up @@ -474,16 +473,16 @@ class searcher
method! jsx_child child =
let (loc, c) = child in
match c with
| Flow_ast.JSX.Text _ when annot_covers_target loc -> this#found_empty "jsx text"
| Ast.JSX.Text _ when annot_covers_target loc -> this#found_empty "jsx text"
| _ -> super#jsx_child child

method! class_body cls_body =
let open Flow_ast.Class in
let open Ast.Class in
let (_, { Body.body; comments = _ }) = cls_body in
let new_available_private_names =
let add_private_name (loc, { Flow_ast.PrivateName.name; _ }) = SMap.add name loc in
let add_private_name (loc, { Ast.PrivateName.name; _ }) = SMap.add name loc in
Base.List.fold body ~init:available_private_names ~f:(fun acc -> function
| Body.Method (_, { Method.key = Flow_ast.Expression.Object.Property.PrivateName name; _ })
| Body.Method (_, { Method.key = Ast.Expression.Object.Property.PrivateName name; _ })
| Body.PrivateField (_, { PrivateField.key = name; _ }) ->
add_private_name name acc
| Body.Method _ -> acc
Expand All @@ -496,7 +495,7 @@ class searcher
available_private_names <- saved_available_private_names;
Base.Result.ok_exn result

method! private_name ((loc, { Flow_ast.PrivateName.name; comments = _ }) as pn) =
method! private_name ((loc, { Ast.PrivateName.name; comments = _ }) as pn) =
if covers_target loc then
match SMap.find_opt name available_private_names with
| None -> this#found_empty "unbound private name"
Expand Down

0 comments on commit f55b7a9

Please sign in to comment.