Skip to content

Commit

Permalink
[flow] Rollback ability to create userland react module typing and ki…
Browse files Browse the repository at this point in the history
…ll unreleased declare global support

Summary:
Unfortunately, the userland typing for the react module has caused too many reliability issues, so this diff rolls back all the risky part of the changes, including

- declare global support
- making every module depend on react: for places where we do the `Context.find_require` lookup, we now change it to just builtin lookup.

This means we cannot completely ban override. However, it's less of an issue for multiplatform work, since the override we really need to ban is a directory-scoped overriding the builtin + common libdefs. We should still issue errors on these overrides to prevent surprises.

Changelog: [internal] The ability to have userland React module type definition is never announced.

Reviewed By: gkz

Differential Revision: D70270290

fbshipit-source-id: 54212086b31b3f4bd907d0ba8fd2d841a8931e33
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Feb 27, 2025
1 parent ee6d6d2 commit 0cbd61d
Show file tree
Hide file tree
Showing 32 changed files with 84 additions and 615 deletions.
51 changes: 9 additions & 42 deletions src/analysis/env_builder/name_resolver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6400,51 +6400,18 @@ module Make (Context : C) (FlowAPIUtils : F with type cx = Context.t) :
let v = val_simplify def_loc (Val.SourceLevelBinding kind) (Some name) v in
v

method! declare_module loc ({ Ast.Statement.DeclareModule.id = _; body; _ } as m) =
method! declare_module _loc ({ Ast.Statement.DeclareModule.id = _; body; _ } as m) =
let (block_loc, { Ast.Statement.Block.body = statements; comments = _ }) = body in
let within_possible_declare_globals ~f =
let declare_global_bodies =
Base.List.filter_map statements ~f:(function
| ( _,
Ast.Statement.DeclareNamespace
{
Ast.Statement.DeclareNamespace.id = Ast.Statement.DeclareNamespace.Global _;
body;
comments = _;
}
) ->
Some body
| _ -> None
)
let bindings =
let hoist =
new Hoister.hoister ~flowmin_compatibility:false ~enable_enums ~with_types:true
in
if Base.List.is_empty declare_global_bodies then
f ()
else
let hoist = new hoister ~flowmin_compatibility:false ~enable_enums ~with_types:true in
Base.List.iter declare_global_bodies ~f:(fun (loc, body) ->
Flow_ast_visitor.run (hoist#block loc) body
);
let global_bindings = hoist#acc in
this#with_bindings loc global_bindings (fun () ->
Base.List.iter declare_global_bodies ~f:(fun (loc, body) ->
Flow_ast_visitor.run (this#block loc) body
);
f ()
);
()
hoist#eval hoist#statement_list statements
in
within_possible_declare_globals ~f:(fun () ->
let bindings =
let hoist =
new Hoister.hoister ~flowmin_compatibility:false ~enable_enums ~with_types:true
in
hoist#eval hoist#statement_list statements
in
let saved_exclude_syms = env_state.exclude_syms in
env_state <- { env_state with exclude_syms = SSet.empty };
ignore @@ this#statements_with_bindings block_loc bindings statements;
env_state <- { env_state with exclude_syms = saved_exclude_syms }
);
let saved_exclude_syms = env_state.exclude_syms in
env_state <- { env_state with exclude_syms = SSet.empty };
ignore @@ this#statements_with_bindings block_loc bindings statements;
env_state <- { env_state with exclude_syms = saved_exclude_syms };
m

method! declare_namespace loc ({ Ast.Statement.DeclareNamespace.id; body; _ } as m) =
Expand Down
40 changes: 5 additions & 35 deletions src/analysis/scope_builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -574,42 +574,12 @@ module Make (L : Loc_sig.S) (Api : Scope_api_sig.S with module L = L) :
let open Ast.Statement.DeclareModule in
let { id = _; body; comments = _ } = m in
let (loc, body) = body in
let within_possible_declare_globals ~f =
let declare_global_bodies =
Base.List.filter_map body.Ast.Statement.Block.body ~f:(function
| ( _,
Ast.Statement.DeclareNamespace
{
Ast.Statement.DeclareNamespace.id = Ast.Statement.DeclareNamespace.Global _;
body;
comments = _;
}
) ->
Some body
| _ -> None
)
in
if Base.List.is_empty declare_global_bodies then
f ()
else
let hoist = new hoister ~flowmin_compatibility ~enable_enums ~with_types in
Base.List.iter declare_global_bodies ~f:(fun (loc, body) -> run (hoist#block loc) body);
let global_bindings = hoist#acc in
this#with_bindings loc global_bindings (fun () ->
Base.List.iter declare_global_bodies ~f:(fun (loc, body) ->
run (this#block loc) body
);
f ()
)
let bindings =
let hoist = new hoister ~flowmin_compatibility ~enable_enums ~with_types in
run (hoist#block loc) body;
hoist#acc
in
within_possible_declare_globals ~f:(fun () ->
let bindings =
let hoist = new hoister ~flowmin_compatibility ~enable_enums ~with_types in
run (hoist#block loc) body;
hoist#acc
in
this#with_bindings loc bindings (fun () -> run (this#block loc) body)
);
this#with_bindings loc bindings (fun () -> run (this#block loc) body);
m

method! declare_namespace _loc n =
Expand Down
1 change: 0 additions & 1 deletion src/codemods/annotate_exports.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ module SignatureVerification = struct
facebook_fbt = Options.facebook_fbt options;
max_literal_len = Options.max_literal_length options;
exact_by_default = Options.exact_by_default options;
enable_declare_global = Options.enable_declare_global options;
enable_enums = Options.enums options;
enable_component_syntax = Options.typecheck_component_syntax_in_file options file;
component_syntax_enabled_in_config = Options.component_syntax options;
Expand Down
1 change: 0 additions & 1 deletion src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,6 @@ let make_options
FlowConfig.dev_only_refinement_info_as_errors flowconfig;
opt_enable_const_params =
Base.Option.value (FlowConfig.enable_const_params flowconfig) ~default:false;
opt_enable_declare_global = FlowConfig.enable_declare_global flowconfig;
opt_enable_jest_integration = FlowConfig.jest_integration flowconfig;
opt_enable_pattern_matching =
Base.Option.value ~default:false (FlowConfig.pattern_matching flowconfig);
Expand Down
7 changes: 0 additions & 7 deletions src/commands/config/flowConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ module Opts = struct
dev_only_refinement_info_as_errors: bool;
emoji: bool option;
enable_const_params: bool option;
enable_declare_global: bool;
enums: bool;
estimate_recheck_time: bool option;
exact_by_default: bool option;
Expand Down Expand Up @@ -194,7 +193,6 @@ module Opts = struct
dev_only_refinement_info_as_errors = false;
emoji = None;
enable_const_params = None;
enable_declare_global = false;
enums = false;
estimate_recheck_time = None;
exact_by_default = None;
Expand Down Expand Up @@ -1029,9 +1027,6 @@ module Opts = struct
( "experimental.component_syntax.hooklike_functions.excludes",
hook_compatibility_excludes_parser
);
( "experimental.declare_global",
boolean (fun opts v -> Ok { opts with enable_declare_global = v })
);
("this_type_guards", boolean (fun opts v -> Ok { opts with this_type_guards = v }));
("experimental.facebook_module_interop", facebook_module_interop_parser);
("experimental.channel_mode", channel_mode_parser ~enabled:true);
Expand Down Expand Up @@ -1745,8 +1740,6 @@ let emoji c = c.options.Opts.emoji

let enable_const_params c = c.options.Opts.enable_const_params

let enable_declare_global c = c.options.Opts.enable_declare_global

let enums c = c.options.Opts.enums

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

val enable_const_params : config -> bool option

val enable_declare_global : config -> bool

val enums : config -> bool

val estimate_recheck_time : config -> bool option
Expand Down
3 changes: 0 additions & 3 deletions src/common/options.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type t = {
opt_debug: bool;
opt_dev_only_refinement_info_as_errors: bool;
opt_enable_const_params: bool;
opt_enable_declare_global: bool;
opt_enable_jest_integration: bool;
opt_enable_pattern_matching: bool;
opt_enable_relay_integration: bool;
Expand Down Expand Up @@ -223,8 +222,6 @@ let dev_only_refinement_info_as_errors opts = opts.opt_dev_only_refinement_info_

let enable_const_params opts = opts.opt_enable_const_params

let enable_declare_global opts = opts.opt_enable_declare_global

let enable_jest_integration opts = opts.opt_enable_jest_integration

let enable_pattern_matching opts = opts.opt_enable_pattern_matching
Expand Down
2 changes: 0 additions & 2 deletions src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ let load_lib_files files =
facebook_fbt = None;
max_literal_len = 100;
exact_by_default = true;
enable_declare_global = false;
enable_enums = true;
enable_component_syntax = true;
component_syntax_enabled_in_config = true;
Expand Down Expand Up @@ -112,7 +111,6 @@ let stub_metadata ~root ~checked =
react_rules_always = false;
dev_only_refinement_info_as_errors = false;
enable_const_params = false;
enable_declare_global = false;
enable_enums = true;
enable_jest_integration = false;
enable_pattern_matching = true;
Expand Down
Loading

0 comments on commit 0cbd61d

Please sign in to comment.