Skip to content

Commit

Permalink
[flow][EZ][apply-code-action] Factor out code to type check file
Browse files Browse the repository at this point in the history
Summary:
Next diff has a similar structure to this function so I'll make it re-usable
Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D69551137

fbshipit-source-id: 28f49490843cd2f2fd741b477904156c17b98ad5
  • Loading branch information
jbrown215 authored and facebook-github-bot committed Feb 13, 2025
1 parent 59c1531 commit ffd6440
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/services/code_action/code_action_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1592,9 +1592,7 @@ let autofix_imports ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~src
let opts = layout_options options in
Autofix_imports.add_imports ~options:opts ~added_imports ast

let autofix_imports_cli
~options ~profiling ~env ~loc_of_aloc ~module_system_info ~file_key ~file_content =
let src_dir = File_key.to_string file_key |> Filename.dirname |> Base.Option.return in
let with_type_checked_file ~options ~profiling ~env ~file_key ~file_content ~f =
let file_artifacts =
let ((_, parse_errs) as intermediate_result) =
Type_contents.parse_contents ~options ~profiling file_content file_key
Expand All @@ -1610,10 +1608,17 @@ let autofix_imports_cli
intermediate_result
in
match file_artifacts with
| Ok (Parse_artifacts { ast; _ }, Typecheck_artifacts { cx; _ }) ->
| Ok (Parse_artifacts { ast; _ }, Typecheck_artifacts { cx; _ }) -> f ~cx ~ast
| _ -> Error "Failed to parse or check file"

let autofix_imports_cli
~options ~profiling ~env ~loc_of_aloc ~module_system_info ~file_key ~file_content =
let src_dir = File_key.to_string file_key |> Filename.dirname |> Base.Option.return in
let get_edits ~cx ~ast =
let edits = autofix_imports ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~src_dir in
Ok (Replacement_printer.loc_patch_to_patch file_content edits)
| _ -> Error "Failed to parse or check file"
in
with_type_checked_file ~options ~profiling ~env ~file_key ~file_content ~f:get_edits

let autofix_imports_lsp ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~uri =
let src_dir = Lsp_helpers.lsp_uri_to_path uri |> Filename.dirname |> Base.Option.return in
Expand Down

0 comments on commit ffd6440

Please sign in to comment.