Skip to content

Commit

Permalink
[flow][refactor] Track def_loc of module ref in ast
Browse files Browse the repository at this point in the history
Summary:
This diff makes use of the explicit def_loc computed in the previous diff for module ref get-def. No behavior should change yet, since it's still the same result.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision:
D67875260

------------------------------------------------------------------------
(from f5852f3127e88ee404b4d9f5d23f48260407c9bc)

fbshipit-source-id: 64a96b65420d33b372f6a1db629d702c56847a62
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 7, 2025
1 parent bb885d0 commit 18717c9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/parser/expression_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,8 @@ module Expression
Expression.ModuleRefLiteral
{
Ast.ModuleRefLiteral.value;
require_out = loc;
require_loc = loc;
def_loc_opt = None;
prefix_len;
legacy_interop = false;
raw;
Expand All @@ -1288,7 +1289,8 @@ module Expression
Expression.ModuleRefLiteral
{
Ast.ModuleRefLiteral.value;
require_out = loc;
require_loc = loc;
def_loc_opt = None;
prefix_len;
legacy_interop = true;
raw;
Expand Down
3 changes: 2 additions & 1 deletion src/parser/flow_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ end =
and ModuleRefLiteral : sig
type ('M, 'T) t = {
value: string;
require_out: 'T;
require_loc: 'M;
def_loc_opt: 'M option;
prefix_len: int;
legacy_interop: bool;
raw: string;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/flow_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1683,12 +1683,12 @@ class ['loc] mapper =

method module_ref_literal _loc (lit : ('loc, 'loc) Ast.ModuleRefLiteral.t) =
let open Ast.ModuleRefLiteral in
let { value; require_out; prefix_len; legacy_interop; raw; comments } = lit in
let { value; require_loc; def_loc_opt; prefix_len; legacy_interop; raw; comments } = lit in
let comments' = this#syntax_opt comments in
if comments == comments' then
lit
else
{ value; require_out; prefix_len; legacy_interop; raw; comments }
{ value; require_loc; def_loc_opt; prefix_len; legacy_interop; raw; comments }

method nullable_type (t : ('loc, 'loc) Ast.Type.Nullable.t) =
let open Ast.Type.Nullable in
Expand Down
15 changes: 12 additions & 3 deletions src/parser_utils/flow_polymorphic_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1477,10 +1477,19 @@ class virtual ['M, 'T, 'N, 'U] mapper =
method module_ref_literal (mref : ('M, 'T) Ast.ModuleRefLiteral.t)
: ('N, 'U) Ast.ModuleRefLiteral.t =
let open Ast.ModuleRefLiteral in
let { value; require_out; prefix_len; legacy_interop; raw; comments } = mref in
let require_out' = this#on_type_annot require_out in
let { value; require_loc; def_loc_opt; prefix_len; legacy_interop; raw; comments } = mref in
let require_loc' = this#on_loc_annot require_loc in
let def_loc_opt' = Base.Option.map ~f:this#on_loc_annot def_loc_opt in
let comments' = this#syntax_opt comments in
{ value; require_out = require_out'; prefix_len; legacy_interop; raw; comments = comments' }
{
value;
require_loc = require_loc';
def_loc_opt = def_loc_opt';
prefix_len;
legacy_interop;
raw;
comments = comments';
}

method type_ ((annot, t) : ('M, 'T) Ast.Type.t) : ('N, 'U) Ast.Type.t =
let open Ast.Type in
Expand Down
3 changes: 2 additions & 1 deletion src/services/get_def/getDef_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ let get_def ~loc_of_aloc ~cx ~file_sig ~ast ~available_ast ~purpose requested_lo
match
process_location cx ~is_local_use ~is_legit_require ~available_ast ~purpose req_loc
with
| OwnDef (aloc, name) -> Def (LocSet.singleton (loc_of_aloc aloc), Some name)
| OwnNamedDef (aloc, name) -> Def (LocSet.singleton (loc_of_aloc aloc), Some name)
| OwnUnnamedDef aloc -> Def (LocSet.singleton (loc_of_aloc aloc), None)
| Request request -> begin
match
process_request
Expand Down
45 changes: 24 additions & 21 deletions src/services/get_def/get_def_process_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type internal_error =
(** This type is distinct from the one raised by the searcher because
it would never make sense for the searcher to raise LocNotFound *)
type 'loc result =
| OwnDef of 'loc * (* name *) string
| OwnNamedDef of 'loc * (* name *) string
| OwnUnnamedDef of 'loc
| Request of ('loc, 'loc * (Type.t[@opaque])) Get_def_request.t
| Empty of string
| LocNotFound
Expand Down Expand Up @@ -112,9 +113,14 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~

method private on_loc_annot x = x

method private own_def : 'a. ALoc.t -> string -> 'a =
method private own_named_def : 'a. ALoc.t -> string -> 'a =
fun loc name ->
found_loc_ <- OwnDef (loc, name);
found_loc_ <- OwnNamedDef (loc, name);
raise Found

method private own_unnamed_def : 'a. ALoc.t -> 'a =
fun loc ->
found_loc_ <- OwnUnnamedDef loc;
raise Found

method private found_empty : 'a. string -> 'a =
Expand Down Expand Up @@ -175,14 +181,14 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
in
( if this#annot_covers_target remote_annot then
match this#remote_name_def_loc_of_import_named_specifier decl with
| Some l -> this#own_def l name
| None -> this#own_def (this#loc_of_annot remote_annot) "default"
| Some l -> this#own_named_def l name
| None -> this#own_named_def (this#loc_of_annot remote_annot) "default"
);
Base.Option.iter local ~f:(fun (local_annot, _) ->
if this#annot_covers_target local_annot then
match this#remote_name_def_loc_of_import_named_specifier decl with
| Some l -> this#own_def l name
| None -> this#own_def (this#loc_of_annot local_annot) "default"
| Some l -> this#own_named_def l name
| None -> this#own_named_def (this#loc_of_annot local_annot) "default"
);
decl

Expand All @@ -192,8 +198,8 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
Base.Option.iter default ~f:(fun { identifier = (annot, _); _ } ->
if this#annot_covers_target annot then
match this#remote_default_name_def_loc_of_import_declaration (loc, decl) with
| Some l -> this#own_def l "default"
| None -> this#own_def (this#loc_of_annot annot) "default"
| Some l -> this#own_named_def l "default"
| None -> this#own_named_def (this#loc_of_annot annot) "default"
);
Base.Option.iter specifiers ~f:(function
| ImportNamedSpecifiers _ -> ()
Expand All @@ -208,7 +214,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
{ annot = (this#loc_of_annot source_annot, t); name = Some name }
)
| Get_def_types.Purpose.FindReferences ->
ignore @@ this#own_def (this#loc_of_annot name_annot) name
ignore @@ this#own_named_def (this#loc_of_annot name_annot) name
)
);
super#import_declaration loc decl
Expand Down Expand Up @@ -412,7 +418,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
let annot = (this#loc_of_annot annot, this#type_from_enclosing_node annot) in
this#request (Get_def_request.Type { annot; name = Some name })
else
this#own_def (this#loc_of_annot annot) name;
this#own_named_def (this#loc_of_annot annot) name;
super#pattern_identifier ?kind (annot, name_node)

method! expression (annot, expr) =
Expand Down Expand Up @@ -495,28 +501,25 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~

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

method! module_ref_literal mref =
let { Ast.ModuleRefLiteral.require_out; _ } = mref in
if this#annot_covers_target require_out then
let require_out =
(this#loc_of_annot require_out, this#type_from_enclosing_node require_out)
in
this#request (Get_def_request.Type { annot = require_out; name = None })
let { Ast.ModuleRefLiteral.require_loc; def_loc_opt; _ } = mref in
if covers_target require_loc then
this#own_unnamed_def (Base.Option.value ~default:require_loc def_loc_opt)
else
super#module_ref_literal mref

method! enum_member_identifier id =
let (loc, { Ast.Identifier.name; comments = _ }) = id in
if covers_target loc then this#own_def loc name;
if covers_target loc then this#own_named_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, { Ast.Identifier.name; comments = _ }) = id in
if this#annot_covers_target annot then this#own_def (this#loc_of_annot annot) name;
if this#annot_covers_target annot then this#own_named_def (this#loc_of_annot annot) name;
id

method! object_key_string_literal literal =
Expand Down Expand Up @@ -620,7 +623,7 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
if covers_target loc then
match SMap.find_opt name available_private_names with
| None -> this#found_empty "unbound private name"
| Some l -> this#own_def l name
| Some l -> this#own_named_def l name
else
pn
end
Expand Down
3 changes: 2 additions & 1 deletion src/services/get_def/get_def_process_location.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type internal_error =
[@@deriving show]

type 'loc result =
| OwnDef of 'loc * (* name *) string
| OwnNamedDef of 'loc * (* name *) string
| OwnUnnamedDef of 'loc
| Request of ('loc, 'loc * (Type.t[@opaque])) Get_def_request.t
| Empty of string
| LocNotFound
Expand Down
6 changes: 3 additions & 3 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,15 @@ module Make
Flow.get_builtin_type cx reason "RegExp"

let module_ref_literal cx loc lit =
let { Ast.ModuleRefLiteral.value; require_out; prefix_len; legacy_interop; _ } = lit in
let { Ast.ModuleRefLiteral.value; def_loc_opt = _; prefix_len; legacy_interop; _ } = lit in
let mref = Base.String.drop_prefix value prefix_len in
let module_t =
Import_export.get_module_t
cx
~import_kind_for_untyped_import_validation:(Some ImportValue)
(loc, mref)
in
let (_def_loc_opt, require_t) =
let (def_loc_opt, require_t) =
Import_export.cjs_require_type
cx
(mk_reason (RModule mref) loc)
Expand All @@ -835,7 +835,7 @@ module Make
in
let reason = mk_reason (RCustom "module reference") loc in
let t = Flow.get_builtin_typeapp cx reason "$Flow$ModuleRef" [require_t] in
(t, { lit with Ast.ModuleRefLiteral.require_out = (require_out, require_t) })
(t, { lit with Ast.ModuleRefLiteral.def_loc_opt })

let check_const_assertion cx (loc, e) =
let open Ast.Expression in
Expand Down

0 comments on commit 18717c9

Please sign in to comment.