From 18717c9b0ea595af7b66635c8db92b41ad7b54f2 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Tue, 7 Jan 2025 10:53:14 -0800 Subject: [PATCH] [flow][refactor] Track def_loc of module ref in ast 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 --- src/parser/expression_parser.ml | 6 ++- src/parser/flow_ast.ml | 3 +- src/parser/flow_ast_mapper.ml | 4 +- .../flow_polymorphic_ast_mapper.ml | 15 +++++-- src/services/get_def/getDef_js.ml | 3 +- .../get_def/get_def_process_location.ml | 45 ++++++++++--------- .../get_def/get_def_process_location.mli | 3 +- src/typing/statement.ml | 6 +-- 8 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/parser/expression_parser.ml b/src/parser/expression_parser.ml index 429a9ab6376..ff804c23108 100644 --- a/src/parser/expression_parser.ml +++ b/src/parser/expression_parser.ml @@ -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; @@ -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; diff --git a/src/parser/flow_ast.ml b/src/parser/flow_ast.ml index ba080be13e8..3173032c22e 100644 --- a/src/parser/flow_ast.ml +++ b/src/parser/flow_ast.ml @@ -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; diff --git a/src/parser/flow_ast_mapper.ml b/src/parser/flow_ast_mapper.ml index 831ff2aaa9a..698df5a9ddf 100644 --- a/src/parser/flow_ast_mapper.ml +++ b/src/parser/flow_ast_mapper.ml @@ -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 diff --git a/src/parser_utils/flow_polymorphic_ast_mapper.ml b/src/parser_utils/flow_polymorphic_ast_mapper.ml index 0c5c05afc5c..f7e2ac0eb40 100644 --- a/src/parser_utils/flow_polymorphic_ast_mapper.ml +++ b/src/parser_utils/flow_polymorphic_ast_mapper.ml @@ -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 diff --git a/src/services/get_def/getDef_js.ml b/src/services/get_def/getDef_js.ml index 6a3f75e3c69..0f289a36ff3 100644 --- a/src/services/get_def/getDef_js.ml +++ b/src/services/get_def/getDef_js.ml @@ -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 diff --git a/src/services/get_def/get_def_process_location.ml b/src/services/get_def/get_def_process_location.ml index 4202ef97cc7..165394784bc 100644 --- a/src/services/get_def/get_def_process_location.ml +++ b/src/services/get_def/get_def_process_location.ml @@ -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 @@ -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 = @@ -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 @@ -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 _ -> () @@ -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 @@ -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) = @@ -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 = @@ -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 diff --git a/src/services/get_def/get_def_process_location.mli b/src/services/get_def/get_def_process_location.mli index bcae8812ee2..42ca6e0c1ea 100644 --- a/src/services/get_def/get_def_process_location.mli +++ b/src/services/get_def/get_def_process_location.mli @@ -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 diff --git a/src/typing/statement.ml b/src/typing/statement.ml index 0ea753daf9e..aa9dfbbfc0f 100644 --- a/src/typing/statement.ml +++ b/src/typing/statement.ml @@ -816,7 +816,7 @@ 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 @@ -824,7 +824,7 @@ module Make ~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) @@ -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