@@ -1950,13 +1950,89 @@ let with_type_checked_file ~options ~profiling ~env ~file_key ~file_content ~f =
1950
1950
intermediate_result
1951
1951
in
1952
1952
match file_artifacts with
1953
- | Ok (Parse_artifacts { ast; _ } , Typecheck_artifacts { cx; _ } ) -> f ~cx ~ast
1953
+ | Ok (Parse_artifacts { ast; file_sig; _ } , Typecheck_artifacts { cx; typed_ast; _ } ) ->
1954
+ f ~cx ~file_sig ~ast ~typed_ast
1954
1955
| _ -> Error " Failed to parse or check file"
1955
1956
1957
+ let autofix_errors_cli
1958
+ ~options
1959
+ ~profiling
1960
+ ~env
1961
+ ~loc_of_aloc
1962
+ ~get_ast_from_shared_mem
1963
+ ~module_system_info
1964
+ ~get_type_sig
1965
+ ~include_best_effort_fix
1966
+ ~file_key
1967
+ ~file_content =
1968
+ let get_edits ~cx ~file_sig ~ast ~typed_ast =
1969
+ let (safe_transforms, best_effort_transforms) =
1970
+ Flow_error.ErrorSet. fold
1971
+ (fun error acc ->
1972
+ let lazy_error_loc =
1973
+ lazy
1974
+ (let { Flow_intermediate_error_types. loc; _ } =
1975
+ Flow_intermediate_error. make_intermediate_error ~loc_of_aloc error
1976
+ in
1977
+ loc
1978
+ )
1979
+ in
1980
+ match
1981
+ ast_transforms_of_error
1982
+ ~loc_of_aloc
1983
+ ~lazy_error_loc
1984
+ ~get_ast_from_shared_mem
1985
+ ~get_haste_module_info: module_system_info.Lsp_module_system_info. get_haste_module_info
1986
+ ~get_type_sig
1987
+ (error |> Flow_error. map_loc_of_error loc_of_aloc |> Flow_error. msg_of_error)
1988
+ |> Base.List. hd
1989
+ with
1990
+ | None -> acc
1991
+ | Some transform ->
1992
+ let (safe_transforms, best_effort_transforms) = acc in
1993
+ (match transform.confidence with
1994
+ | WillFixErrorAndSafeForRunningOnSave ->
1995
+ (transform :: safe_transforms, best_effort_transforms)
1996
+ | BestEffort ->
1997
+ if include_best_effort_fix then
1998
+ (safe_transforms, transform :: best_effort_transforms)
1999
+ else
2000
+ acc))
2001
+ (Context. errors cx)
2002
+ ([] , [] )
2003
+ in
2004
+ let (new_ast, _) =
2005
+ Base.List. fold
2006
+ (safe_transforms @ best_effort_transforms)
2007
+ ~init: (ast, false )
2008
+ ~f: (fun (ast , has_run_best_effort_fix ) transform ->
2009
+ match (transform.confidence, has_run_best_effort_fix) with
2010
+ | (BestEffort, true ) -> (ast, true )
2011
+ | (WillFixErrorAndSafeForRunningOnSave, _ ) ->
2012
+ let new_ast =
2013
+ Base.Option. value
2014
+ ~default: ast
2015
+ (transform.transform ~cx ~file_sig ~ast ~typed_ast transform.target_loc)
2016
+ in
2017
+ (new_ast, has_run_best_effort_fix)
2018
+ | (BestEffort, false ) ->
2019
+ let new_ast =
2020
+ Base.Option. value
2021
+ ~default: ast
2022
+ (transform.transform ~cx ~file_sig ~ast ~typed_ast transform.target_loc)
2023
+ in
2024
+ (new_ast, new_ast != ast)
2025
+ )
2026
+ in
2027
+ let opts = layout_options options in
2028
+ Ok (Insert_type. mk_patch ~opts ast new_ast file_content)
2029
+ in
2030
+ with_type_checked_file ~options ~profiling ~env ~file_key ~file_content ~f: get_edits
2031
+
1956
2032
let suggest_imports_cli
1957
2033
~options ~profiling ~env ~loc_of_aloc ~module_system_info ~file_key ~file_content =
1958
2034
let uri = File_key. to_string file_key |> Lsp_helpers. path_to_lsp_uri ~default_path: " " in
1959
- let get_edits ~cx ~ast =
2035
+ let get_edits ~cx ~file_sig : _ ~ ast ~ typed_ast : _ =
1960
2036
let errors = Context. errors cx in
1961
2037
let (imports, _) =
1962
2038
Flow_error.ErrorSet. fold
@@ -1995,7 +2071,7 @@ let suggest_imports_cli
1995
2071
let autofix_imports_cli
1996
2072
~options ~profiling ~env ~loc_of_aloc ~module_system_info ~file_key ~file_content =
1997
2073
let src_dir = File_key. to_string file_key |> Filename. dirname |> Base.Option. return in
1998
- let get_edits ~cx ~ast =
2074
+ let get_edits ~cx ~file_sig : _ ~ ast ~ typed_ast : _ =
1999
2075
let edits = autofix_imports ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~src_dir in
2000
2076
Ok (Replacement_printer. loc_patch_to_patch file_content edits)
2001
2077
in
0 commit comments