Skip to content

Commit

Permalink
[flow][refactor] Return def_loc from CJSRequireTKit.on_ModuleT
Browse files Browse the repository at this point in the history
Summary:
This is just a refactor that makes the current def_loc determination logic explicit. It's not used yet.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision:
D67875258

------------------------------------------------------------------------
(from 84a021323bdc7af96c387b4214578e328edf1ad2)

fbshipit-source-id: 0eb3e8f5154a2ce4dead93726593fd3da333fa06
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 7, 2025
1 parent 0a3673d commit bb885d0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 40 deletions.
21 changes: 12 additions & 9 deletions src/typing/annotation_inference.ml
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,18 @@ module rec ConsGen : S = struct
Annot_CJSRequireT
{ reason; namespace_symbol; is_strict; standard_cjs_esm_interop; legacy_interop }
) ->
CJSRequireTKit.on_ModuleT
cx
~reposition:(fun _ _ t -> t)
~reason
~module_symbol:namespace_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
m
let (t, _def_loc) =
CJSRequireTKit.on_ModuleT
cx
~reposition:(fun _ _ t -> t)
~reason
~module_symbol:namespace_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
m
in
t
| (ModuleT m, Annot_ImportModuleNsT (reason, namespace_symbol, is_strict)) ->
let (values_type, types_tmap) = ImportModuleNsTKit.on_ModuleT cx (reason, is_strict) m in
NamespaceT { namespace_symbol; values_type; types_tmap }
Expand Down
20 changes: 11 additions & 9 deletions src/typing/flow_js_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ module CJSRequireTKit = struct
| Some t ->
(* reposition the export to point at the require(), like the object
we create below for non-CommonJS exports *)
reposition cx (loc_of_reason reason) t
(reposition cx (loc_of_reason reason) t, def_loc_of_t t)
| None ->
let value_exports_tmap = Context.find_exports cx exports.value_exports_tmap in
let type_exports_tmap = Context.find_exports cx exports.type_exports_tmap in
Expand All @@ -1559,11 +1559,13 @@ module CJSRequireTKit = struct
NamespaceT { namespace_symbol = module_symbol; values_type; types_tmap }
in
if standard_cjs_esm_interop then
lookup_builtin_typeapp
cx
reason
"$Flow$EsmModuleMarkerWrapperInModuleRef"
[mk_exports_namespace ()]
( lookup_builtin_typeapp
cx
reason
"$Flow$EsmModuleMarkerWrapperInModuleRef"
[mk_exports_namespace ()],
def_loc_of_reason reason
)
else
(* Use default export if option is enabled and module is not lib *)
let automatic_require_default =
Expand All @@ -1572,10 +1574,10 @@ module CJSRequireTKit = struct
in
if automatic_require_default then
match NameUtils.Map.find_opt (OrdinaryName "default") value_exports_tmap with
| Some { preferred_def_locs = _; name_loc = _; type_ } -> type_
| _ -> mk_exports_namespace ()
| Some { preferred_def_locs = _; name_loc = _; type_ } -> (type_, def_loc_of_t type_)
| _ -> (mk_exports_namespace (), def_loc_of_reason reason)
else
mk_exports_namespace ()
(mk_exports_namespace (), def_loc_of_reason reason)
end

(* import * as X from 'SomeModule'; *)
Expand Down
7 changes: 4 additions & 3 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ module Make
~import_kind_for_untyped_import_validation:(Some ImportValue)
(loc, mref)
in
let require_t =
let (_def_loc_opt, require_t) =
Import_export.cjs_require_type
cx
(mk_reason (RModule mref) loc)
Expand Down Expand Up @@ -3295,6 +3295,7 @@ module Make
~standard_cjs_esm_interop:false
~legacy_interop:false
module_t
|> snd
| Error err ->
Flow.add_output cx (Error_message.EInvalidGraphQL (loc, err));
let reason = mk_reason RAnyImplicit loc in
Expand Down Expand Up @@ -3655,7 +3656,7 @@ module Make
}
)
) ->
let t =
let (_def_loc_opt, t) =
Import_export.get_module_t
cx
(source_loc, module_name)
Expand Down Expand Up @@ -3698,7 +3699,7 @@ module Make
}
)
) ->
let t =
let (_def_loc_opt, t) =
Import_export.get_module_t
cx
(source_loc, module_name)
Expand Down
1 change: 1 addition & 0 deletions src/typing/ty_normalizer_imports.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let add_require_bindings_from_exports_map cx loc source_name binding acc =
~standard_cjs_esm_interop:false
~legacy_interop:false
module_t
|> snd
in
match binding with
| BindIdent (loc, name) ->
Expand Down
18 changes: 10 additions & 8 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1056,15 +1056,17 @@ module Make (Statement : Statement_sig.S) : Type_annotation_sig.S = struct
let { Ast.StringLiteral.value; _ } = str_lit in
let remote_module_t = Flow_js_utils.get_builtin_module cx value loc in
let str_t = mk_singleton_string str_loc value in
let (_def_loc_opt, require_t) =
Type_operation_utils.Import_export.cjs_require_type
cx
(mk_annot_reason (RModule value) loc)
~namespace_symbol:(FlowSymbol.mk_module_symbol ~name:value ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
remote_module_t
in
reconstruct_ast
(Type_operation_utils.Import_export.cjs_require_type
cx
(mk_annot_reason (RModule value) loc)
~namespace_symbol:(FlowSymbol.mk_module_symbol ~name:value ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
remote_module_t
)
require_t
(Some
( targs_loc,
{
Expand Down
23 changes: 13 additions & 10 deletions src/typing/type_operation_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,19 @@ module Import_export = struct
let is_strict = Context.is_strict cx in
match concretize_module_type cx reason source_module_t with
| Ok m ->
Flow_js_utils.CJSRequireTKit.on_ModuleT
cx
~reposition:Flow.reposition
~reason
~module_symbol:namespace_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
m
| Error (lreason, any_source) -> AnyT (lreason, any_source)
let (t, def_loc) =
Flow_js_utils.CJSRequireTKit.on_ModuleT
cx
~reposition:Flow.reposition
~reason
~module_symbol:namespace_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
m
in
(Some def_loc, t)
| Error (lreason, any_source) -> (None, AnyT (lreason, any_source))
end

module Operators = struct
Expand Down
2 changes: 1 addition & 1 deletion src/typing/type_operation_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Import_export : sig
standard_cjs_esm_interop:bool ->
legacy_interop:bool ->
Type.t ->
Type.t
ALoc.t option * Type.t
end

module Operators : sig
Expand Down

0 comments on commit bb885d0

Please sign in to comment.