Skip to content

Commit

Permalink
[flow] Introduce temporary `haste_module_ref_prefix.standard_cjs_esm_…
Browse files Browse the repository at this point in the history
…interop` config

Reviewed By: panagosg7

Differential Revision: D67207709

fbshipit-source-id: 11418bba61e8f575c290167ae559d3fbf3122b16
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Dec 18, 2024
1 parent f48b56d commit f8e6384
Show file tree
Hide file tree
Showing 25 changed files with 156 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,7 @@ declare var exports: {-[key: string]: mixed};

/* Opaque type for module reference magic strings */
declare opaque type $Flow$ModuleRef<+T>;

declare opaque type $Flow$EsmModuleMarkerWrapperInModuleRef<+T>: T;
/* Commonly available, shared between node and dom */
declare var console: {
assert(condition: mixed, ...data: Array<any>): void,
Expand Down
2 changes: 1 addition & 1 deletion prelude/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type AsyncIterator<+T> = $AsyncIterator<T,void,void>;
type AsyncIterable<+T> = $AsyncIterable<T,void,void>;

declare opaque type $Flow$ModuleRef<+T>;

declare opaque type $Flow$EsmModuleMarkerWrapperInModuleRef<+T>: T;
declare opaque type React$CreateElement;

declare var module: {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,8 @@ let make_options
|| FlowConfig.include_warnings flowconfig;
opt_max_header_tokens = FlowConfig.max_header_tokens flowconfig;
opt_haste_module_ref_prefix = FlowConfig.haste_module_ref_prefix flowconfig;
opt_haste_module_ref_prefix_standard_cjs_esm_interop =
FlowConfig.haste_module_ref_prefix_standard_cjs_esm_interop flowconfig;
opt_haste_module_ref_prefix_LEGACY_INTEROP =
FlowConfig.haste_module_ref_prefix_LEGACY_INTEROP flowconfig;
opt_haste_name_reducers = FlowConfig.haste_name_reducers flowconfig;
Expand Down
8 changes: 8 additions & 0 deletions src/commands/config/flowConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module Opts = struct
gc_worker_space_overhead: int option; (** Gc.control's space_overhead *)
gc_worker_window_size: int option; (** Gc.control's window_size *)
haste_module_ref_prefix: string option;
haste_module_ref_prefix_standard_cjs_esm_interop: bool;
haste_module_ref_prefix_LEGACY_INTEROP: string option;
haste_name_reducers: (Str.regexp * string) list;
haste_namespaces: string list;
Expand Down Expand Up @@ -218,6 +219,7 @@ module Opts = struct
gc_worker_space_overhead = None;
gc_worker_window_size = None;
haste_module_ref_prefix = None;
haste_module_ref_prefix_standard_cjs_esm_interop = false;
haste_module_ref_prefix_LEGACY_INTEROP = None;
haste_name_reducers =
[(Str.regexp "^\\(.*/\\)?\\([a-zA-Z0-9$_.-]+\\)\\.js\\(\\.flow\\)?$", "\\2")];
Expand Down Expand Up @@ -1056,6 +1058,9 @@ module Opts = struct
("module.missing_module_generators", missing_module_generators_parser);
("module.system", module_system_parser);
("module.system.haste.module_ref_prefix", haste_module_ref_prefix_parser);
( "module.system.haste.module_ref_prefix.standard_cjs_esm_interop",
boolean (fun opts v -> Ok { opts with haste_module_ref_prefix_standard_cjs_esm_interop = v })
);
( "module.system.haste.module_ref_prefix_LEGACY_INTEROP",
haste_module_ref_prefix_LEGACY_INTEROP_parser
);
Expand Down Expand Up @@ -1736,6 +1741,9 @@ let gc_worker_window_size c = c.options.Opts.gc_worker_window_size

let haste_module_ref_prefix c = c.options.Opts.haste_module_ref_prefix

let haste_module_ref_prefix_standard_cjs_esm_interop c =
c.options.Opts.haste_module_ref_prefix_standard_cjs_esm_interop

let haste_module_ref_prefix_LEGACY_INTEROP c = c.options.Opts.haste_module_ref_prefix_LEGACY_INTEROP

let haste_name_reducers c = c.options.Opts.haste_name_reducers
Expand Down
2 changes: 2 additions & 0 deletions src/commands/config/flowConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ val gc_worker_window_size : config -> int option

val haste_module_ref_prefix : config -> string option

val haste_module_ref_prefix_standard_cjs_esm_interop : config -> bool

val haste_module_ref_prefix_LEGACY_INTEROP : config -> string option

val haste_name_reducers : config -> (Str.regexp * string) list
Expand Down
4 changes: 4 additions & 0 deletions src/common/options.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type t = {
opt_format: format;
opt_gc_worker: gc_control;
opt_haste_module_ref_prefix: string option;
opt_haste_module_ref_prefix_standard_cjs_esm_interop: bool;
opt_haste_module_ref_prefix_LEGACY_INTEROP: string option;
opt_haste_name_reducers: (Str.regexp * string) list;
opt_haste_namespaces_options: Haste_namespaces.options;
Expand Down Expand Up @@ -257,6 +258,9 @@ let gc_worker opts = opts.opt_gc_worker

let haste_module_ref_prefix opts = opts.opt_haste_module_ref_prefix

let haste_module_ref_prefix_standard_cjs_esm_interop opts =
opts.opt_haste_module_ref_prefix_standard_cjs_esm_interop

let haste_module_ref_prefix_LEGACY_INTEROP opts = opts.opt_haste_module_ref_prefix_LEGACY_INTEROP

let haste_name_reducers opts = opts.opt_haste_name_reducers
Expand Down
1 change: 1 addition & 0 deletions src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ let stub_metadata ~root ~checked =
facebook_fbt = None;
facebook_module_interop = false;
file_options = Files.default_options;
haste_module_ref_prefix_standard_cjs_esm_interop = false;
ignore_non_literal_requires = false;
max_literal_length = 100;
max_workers = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ let stub_metadata ~root ~checked =
facebook_fbt = None;
facebook_module_interop = false;
file_options;
haste_module_ref_prefix_standard_cjs_esm_interop = false;
ignore_non_literal_requires = false;
max_literal_length = 100;
max_workers = 0;
Expand Down
1 change: 1 addition & 0 deletions src/typing/__tests__/type_hint_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let metadata =
facebook_fbt = None;
facebook_module_interop = false;
file_options = Files.default_options;
haste_module_ref_prefix_standard_cjs_esm_interop = false;
ignore_non_literal_requires = false;
max_literal_length = 100;
max_workers = 0;
Expand Down
1 change: 1 addition & 0 deletions src/typing/__tests__/typed_ast_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let metadata =
facebook_fbt = None;
facebook_module_interop = false;
file_options = Files.default_options;
haste_module_ref_prefix_standard_cjs_esm_interop = false;
ignore_non_literal_requires = false;
max_literal_length = 100;
max_workers = 0;
Expand Down
17 changes: 14 additions & 3 deletions src/typing/annotation_inference.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module type S = sig
Reason.t ->
FlowSymbol.symbol ->
is_strict:bool ->
standard_cjs_esm_interop:bool ->
legacy_interop:bool ->
Type.t

Expand Down Expand Up @@ -672,13 +673,17 @@ module rec ConsGen : S = struct
(******************)
(* Module imports *)
(******************)
| (ModuleT m, Annot_CJSRequireT { reason; namespace_symbol; is_strict; legacy_interop }) ->
| ( ModuleT m,
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
| (ModuleT m, Annot_ImportModuleNsT (reason, namespace_symbol, is_strict)) ->
Expand Down Expand Up @@ -1390,8 +1395,14 @@ module rec ConsGen : S = struct
in
mk_lazy_tvar cx reason f

and cjs_require cx t reason namespace_symbol ~is_strict ~legacy_interop =
elab_t cx t (Annot_CJSRequireT { reason; namespace_symbol; is_strict; legacy_interop })
and cjs_require cx t reason namespace_symbol ~is_strict ~standard_cjs_esm_interop ~legacy_interop
=
elab_t
cx
t
(Annot_CJSRequireT
{ reason; namespace_symbol; is_strict; standard_cjs_esm_interop; legacy_interop }
)

and export_named cx reason export_kind value_exports_tmap type_exports_tmap t =
elab_t cx t (Annot_ExportNamedT { reason; value_exports_tmap; type_exports_tmap; export_kind })
Expand Down
6 changes: 6 additions & 0 deletions src/typing/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type metadata = {
facebook_fbt: string option;
facebook_module_interop: bool;
file_options: Files.options;
haste_module_ref_prefix_standard_cjs_esm_interop: bool;
ignore_non_literal_requires: bool;
max_literal_length: int;
max_workers: int;
Expand Down Expand Up @@ -280,6 +281,8 @@ let metadata_of_options options =
facebook_fbt = Options.facebook_fbt options;
facebook_module_interop = Options.facebook_module_interop options;
file_options = Options.file_options options;
haste_module_ref_prefix_standard_cjs_esm_interop =
Options.haste_module_ref_prefix_standard_cjs_esm_interop options;
ignore_non_literal_requires = Options.should_ignore_non_literal_requires options;
max_literal_length = Options.max_literal_length options;
max_workers = Options.max_workers options;
Expand Down Expand Up @@ -634,6 +637,9 @@ let verbose cx = cx.metadata.verbose

let slow_to_check_logging cx = cx.metadata.slow_to_check_logging

let haste_module_ref_prefix_standard_cjs_esm_interop cx =
cx.metadata.haste_module_ref_prefix_standard_cjs_esm_interop

let max_workers cx = cx.metadata.max_workers

let missing_module_generators cx = cx.metadata.missing_module_generators
Expand Down
3 changes: 3 additions & 0 deletions src/typing/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type metadata = {
facebook_fbt: string option;
facebook_module_interop: bool;
file_options: Files.options;
haste_module_ref_prefix_standard_cjs_esm_interop: bool;
ignore_non_literal_requires: bool;
max_literal_length: int;
max_workers: int;
Expand Down Expand Up @@ -297,6 +298,8 @@ val verbose : t -> Verbose.t option

val slow_to_check_logging : t -> Slow_to_check_logging.t

val haste_module_ref_prefix_standard_cjs_esm_interop : t -> bool

val max_workers : t -> int

val missing_module_generators : t -> (Str.regexp * string) list
Expand Down
37 changes: 26 additions & 11 deletions src/typing/flow_js_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,15 @@ end

module CJSRequireTKit = struct
(* require('SomeModule') *)
let on_ModuleT cx ~reposition ~reason ~module_symbol ~is_strict ~legacy_interop module_ =
let on_ModuleT
cx
~reposition
~reason
~module_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
module_ =
let {
module_reason;
module_export_types = exports;
Expand All @@ -1534,11 +1542,6 @@ module CJSRequireTKit = struct
we create below for non-CommonJS exports *)
reposition cx (loc_of_reason reason) t
| None ->
(* Use default export if option is enabled and module is not lib *)
let automatic_require_default =
(legacy_interop || Context.automatic_require_default cx)
&& not (is_lib_reason_def module_reason)
in
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
(* Convert ES module's named exports to an object *)
Expand All @@ -1555,12 +1558,24 @@ module CJSRequireTKit = struct
let types_tmap = Context.generate_property_map cx type_props in
NamespaceT { namespace_symbol = module_symbol; values_type; types_tmap }
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 ()
if standard_cjs_esm_interop then
lookup_builtin_typeapp
cx
reason
"$Flow$EsmModuleMarkerWrapperInModuleRef"
[mk_exports_namespace ()]
else
mk_exports_namespace ()
(* Use default export if option is enabled and module is not lib *)
let automatic_require_default =
(legacy_interop || Context.automatic_require_default cx)
&& not (is_lib_reason_def module_reason)
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 ()
else
mk_exports_namespace ()
end

(* import * as X from 'SomeModule'; *)
Expand Down
4 changes: 4 additions & 0 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ module Make
cx
(mk_reason (RModule mref) loc)
~namespace_symbol:(mk_module_symbol ~name:mref ~def_loc:loc)
~standard_cjs_esm_interop:(Context.haste_module_ref_prefix_standard_cjs_esm_interop cx)
~legacy_interop
module_t
in
Expand Down Expand Up @@ -3291,6 +3292,7 @@ module Make
cx
(mk_reason (RModule module_name) loc)
~namespace_symbol:(mk_module_symbol ~name:module_name ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
module_t
| Error err ->
Expand Down Expand Up @@ -3663,6 +3665,7 @@ module Make
cx
(mk_reason (RModule module_name) loc)
~namespace_symbol:(mk_module_symbol ~name:module_name ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
in
(t, (args_loc, { ArgList.arguments = [Expression (expression cx lit_exp)]; comments }))
Expand Down Expand Up @@ -3705,6 +3708,7 @@ module Make
cx
(mk_reason (RModule module_name) loc)
~namespace_symbol:(mk_module_symbol ~name:module_name ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
in
(t, (args_loc, { ArgList.arguments = [Expression (expression cx lit_exp)]; comments }))
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 @@ -88,6 +88,7 @@ let add_require_bindings_from_exports_map cx loc source_name binding acc =
cx
reason
~namespace_symbol:(FlowSymbol.mk_module_symbol ~name:source_name ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
module_t
in
Expand Down
1 change: 1 addition & 0 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3370,6 +3370,7 @@ module AConstraint = struct
reason: Reason.t;
namespace_symbol: FlowSymbol.symbol;
is_strict: bool;
standard_cjs_esm_interop: bool;
legacy_interop: bool;
}
(* Exports *)
Expand Down
1 change: 1 addition & 0 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ module Make (Statement : Statement_sig.S) : Type_annotation_sig.S = struct
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
)
Expand Down
4 changes: 3 additions & 1 deletion src/typing/type_operation_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ module Import_export = struct
~remote_name:"default"
~local_name

let cjs_require_type cx reason ~namespace_symbol ~legacy_interop source_module_t =
let cjs_require_type
cx reason ~namespace_symbol ~standard_cjs_esm_interop ~legacy_interop source_module_t =
let is_strict = Context.is_strict cx in
match concretize_module_type cx reason source_module_t with
| Ok m ->
Expand All @@ -278,6 +279,7 @@ module Import_export = struct
~reason
~module_symbol:namespace_symbol
~is_strict
~standard_cjs_esm_interop
~legacy_interop
m
| Error (lreason, any_source) -> AnyT (lreason, any_source)
Expand Down
8 changes: 7 additions & 1 deletion src/typing/type_operation_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ module Import_export : sig
ALoc.t option * Type.t

val cjs_require_type :
Context.t -> reason -> namespace_symbol:symbol -> legacy_interop:bool -> Type.t -> Type.t
Context.t ->
reason ->
namespace_symbol:symbol ->
standard_cjs_esm_interop:bool ->
legacy_interop:bool ->
Type.t ->
Type.t
end

module Operators : sig
Expand Down
21 changes: 18 additions & 3 deletions src/typing/type_sig_merge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ let rec merge ?(hooklike = false) ?(as_const = false) ?(const_decl = false) env
let op = merge_op env file op in
let t = eval file loc t op in
make_hooklike file hooklike t
| Pack.Require { loc; index } -> require file loc index ~legacy_interop:false
| Pack.Require { loc; index } ->
require file loc index ~standard_cjs_esm_interop:false ~legacy_interop:false
| Pack.ImportDynamic { loc; index } ->
let (mref, _) = Module_refs.get file.dependencies index in
let ns_reason = Reason.(mk_reason (RModule mref) loc) in
Expand All @@ -544,7 +545,14 @@ let rec merge ?(hooklike = false) ?(as_const = false) ?(const_decl = false) env
let t = Flow_js_utils.lookup_builtin_typeapp file.cx reason "Promise" [ns_t] in
make_hooklike file hooklike t
| Pack.ModuleRef { loc; index; legacy_interop } ->
let t = require file loc index ~legacy_interop in
let t =
require
file
loc
index
~standard_cjs_esm_interop:(Context.haste_module_ref_prefix_standard_cjs_esm_interop file.cx)
~legacy_interop
in
let reason = Reason.(mk_reason (RCustom "module reference") loc) in
let t = Flow_js_utils.lookup_builtin_typeapp file.cx reason "$Flow$ModuleRef" [t] in
make_hooklike file hooklike t
Expand Down Expand Up @@ -804,7 +812,14 @@ and merge_annot env file = function
let module_t = Flow_js_utils.get_builtin_module file.cx ref loc in
let reason = Reason.(mk_annot_reason (RModule ref) loc) in
let symbol = FlowSymbol.mk_module_symbol ~name:ref ~def_loc:loc in
ConsGen.cjs_require file.cx module_t reason symbol ~is_strict:false ~legacy_interop:false
ConsGen.cjs_require
file.cx
module_t
reason
symbol
~is_strict:false
~standard_cjs_esm_interop:false
~legacy_interop:false
| Conditional
{ loc; distributive_tparam; infer_tparams; check_type; extends_type; true_type; false_type }
->
Expand Down
Loading

0 comments on commit f8e6384

Please sign in to comment.