Skip to content

Commit

Permalink
[flow][codemod][EZ] Log reasons why we still have to keep the react i…
Browse files Browse the repository at this point in the history
…mport

Summary:
It helps to uncover the reason we missed some opportunities to remove, which is fixed in the next diff.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D55078973

fbshipit-source-id: ff81a409310bda957692271e23b5a156bf921ac2
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Mar 20, 2024
1 parent bd85ad0 commit 57f47e4
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/codemods/remove_react_import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,17 @@ let has_unaccounted_react_value_usage_visitor =
)
-> name = "ref"
)
then
this#update_acc (fun _ -> true);
then (
Hh_logger.info "Skipping component with ref %s" (Reason.string_of_loc loc);
this#update_acc (fun _ -> true)
);
super#component_declaration loc c

method! identifier ((_, { Ast.Identifier.name; _ }) as id) =
if in_typeof_type && name = "React" then this#update_acc (fun _ -> true);
method! identifier ((loc, { Ast.Identifier.name; _ }) as id) =
if in_typeof_type && name = "React" then (
Hh_logger.info "Skipping React in typeof %s" (Reason.string_of_loc loc);
this#update_acc (fun _ -> true)
);
id
end

Expand All @@ -132,14 +137,24 @@ let mapper ctx =
(* We intentionally exclude types so that the type uses of React is not counted. *)
let scope_info = Scope_builder.program ~enable_enums:false ~with_types:false prog in
let unused =
Scope_api.With_Loc.def_is_unused
scope_info
(Scope_api.With_Loc.def_of_use scope_info react_import_def_loc)
&& not
(has_unaccounted_react_value_usage_visitor#eval
has_unaccounted_react_value_usage_visitor#program
prog
)
let def = Scope_api.With_Loc.def_of_use scope_info react_import_def_loc in
let uses = Scope_api.With_Loc.uses_of_def scope_info ~exclude_def:true def in
if Loc_collections.LocSet.is_empty uses then
not
(has_unaccounted_react_value_usage_visitor#eval
has_unaccounted_react_value_usage_visitor#program
prog
)
else (
Hh_logger.info
"Skipping due to value uses %s"
(uses
|> Loc_collections.LocSet.elements
|> Base.List.map ~f:Reason.string_of_loc
|> String.concat ", "
);
false
)
in
this#update_acc (fun acc ->
let extra =
Expand Down

0 comments on commit 57f47e4

Please sign in to comment.