diff --git a/src/typing/annotation_inference.ml b/src/typing/annotation_inference.ml index ec73108b263..3c4db77327d 100644 --- a/src/typing/annotation_inference.ml +++ b/src/typing/annotation_inference.ml @@ -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 } diff --git a/src/typing/flow_js_utils.ml b/src/typing/flow_js_utils.ml index 1d7d5f9a322..8a2c0a08bd9 100644 --- a/src/typing/flow_js_utils.ml +++ b/src/typing/flow_js_utils.ml @@ -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 @@ -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 = @@ -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'; *) diff --git a/src/typing/statement.ml b/src/typing/statement.ml index 2fc9b084a79..0ea753daf9e 100644 --- a/src/typing/statement.ml +++ b/src/typing/statement.ml @@ -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) @@ -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 @@ -3655,7 +3656,7 @@ module Make } ) ) -> - let t = + let (_def_loc_opt, t) = Import_export.get_module_t cx (source_loc, module_name) @@ -3698,7 +3699,7 @@ module Make } ) ) -> - let t = + let (_def_loc_opt, t) = Import_export.get_module_t cx (source_loc, module_name) diff --git a/src/typing/ty_normalizer_imports.ml b/src/typing/ty_normalizer_imports.ml index e05ccd81648..4f3bb62e1d1 100644 --- a/src/typing/ty_normalizer_imports.ml +++ b/src/typing/ty_normalizer_imports.ml @@ -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) -> diff --git a/src/typing/type_annotation.ml b/src/typing/type_annotation.ml index af6191f6e9f..95d01516d33 100644 --- a/src/typing/type_annotation.ml +++ b/src/typing/type_annotation.ml @@ -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, { diff --git a/src/typing/type_operation_utils.ml b/src/typing/type_operation_utils.ml index 8b74af75efa..5d5311a9551 100644 --- a/src/typing/type_operation_utils.ml +++ b/src/typing/type_operation_utils.ml @@ -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 diff --git a/src/typing/type_operation_utils.mli b/src/typing/type_operation_utils.mli index a05cfebcfc6..5bce8bc79ad 100644 --- a/src/typing/type_operation_utils.mli +++ b/src/typing/type_operation_utils.mli @@ -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