From 51c5573023156cf2f0e400a09046f72e89d81b15 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Mon, 15 Apr 2024 18:48:48 -0700 Subject: [PATCH] [flow][errors] Eliminate last usage of `ExplanationAlreadyPrinted` Summary: I want to ensure that `intermediate_error` contains no `Friendly.message_feature`, which contains a lot of strings that are mostly constant, but very wasteful to send between processes. The end goal is that we will send `intermediate_error` to the main process instead, which will contain already decided primary error location. This diff starts with the easy one: we get rid of `ExplanationAlreadyPrinted`. We just need to add two more variants. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D56164157 fbshipit-source-id: 4e73c5a1a9ea7ffc97288b13b162d4911956f34d --- src/typing/errors/error_message.ml | 17 ++++------------- src/typing/errors/flow_intermediate_error.ml | 12 ++++++++++-- .../errors/flow_intermediate_error_types.ml | 3 ++- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/typing/errors/error_message.ml b/src/typing/errors/error_message.ml index 05428b04474..a3e720105c0 100644 --- a/src/typing/errors/error_message.ml +++ b/src/typing/errors/error_message.ml @@ -8,6 +8,7 @@ open Type open Reason open Utils_js +open Flow_intermediate_error_types exception EDebugThrow of ALoc.t @@ -2228,7 +2229,7 @@ type 'loc friendly_message_recipe = loc: 'loc; features: Loc.t Flow_errors_utils.Friendly.message_feature list; use_op: 'loc Type.virtual_use_op; - explanation: Loc.t Flow_errors_utils.Friendly.message_feature list option; + explanation: 'loc explanation option; } | PropPolarityMismatch of { prop: string option; @@ -4923,12 +4924,7 @@ let friendly_message_of_msg loc_of_aloc msg = loc; features = lower @ [text " but "] @ upper; use_op; - explanation = - Some - [ - text - "React hooks and other functions are not compatible with each other, because hooks cannot be called conditionally"; - ]; + explanation = Some ExplanationReactHookIncompatibleWithNormalFunctions; } | EHookUniqueIncompatible { use_op; lower; upper } -> UseOp @@ -4936,12 +4932,7 @@ let friendly_message_of_msg loc_of_aloc msg = loc = loc_of_reason lower; features = [ref lower; text " and "; ref upper; text " are different React hooks"]; use_op; - explanation = - Some - [ - text - "Different React hooks are not compatible with each other, because hooks cannot be called conditionally"; - ]; + explanation = Some ExplanationReactHookIncompatibleWithEachOther; } | EHookNaming _ -> Normal { features = [text "Hooks must have names that begin with "; code "use"; text "."] } diff --git a/src/typing/errors/flow_intermediate_error.ml b/src/typing/errors/flow_intermediate_error.ml index d9dd6b72395..7dee353d35e 100644 --- a/src/typing/errors/flow_intermediate_error.ml +++ b/src/typing/errors/flow_intermediate_error.ml @@ -1002,7 +1002,6 @@ let rec make_intermediate_error : (Flow_error.code_of_error error) (MessageAlreadyFriendlyPrinted features) | (None, UseOp { loc; features; use_op; explanation }) -> - let explanation = Base.Option.map explanation ~f:(fun e -> ExplanationAlreadyPrinted e) in mk_use_op_error loc use_op ?explanation (MessageAlreadyFriendlyPrinted features) | (None, PropMissing { loc; prop; reason_obj; use_op; suggestion }) -> mk_prop_missing_error loc prop reason_obj use_op suggestion @@ -1061,7 +1060,6 @@ let to_printable_error : let ref = Friendly.ref_map loc_of_aloc in let desc = Friendly.desc_of_reason_desc in let explanation_to_friendly_msgs = function - | ExplanationAlreadyPrinted features -> features | ExplanationAbstractEnumCasting -> [ text "You can explicitly cast your enum value to its representation type using "; @@ -1160,6 +1158,16 @@ let to_printable_error : ref (mk_reason (RCustom "hook arguments") props_loc); text " and their nested elements cannot be written to"; ] + | ExplanationReactHookIncompatibleWithEachOther -> + [ + text + "Different React hooks are not compatible with each other, because hooks cannot be called conditionally"; + ] + | ExplanationReactHookIncompatibleWithNormalFunctions -> + [ + text + "React hooks and other functions are not compatible with each other, because hooks cannot be called conditionally"; + ] | ExplanationReactHookReturnDeepReadOnly hook_loc -> [ text "The return value of a "; diff --git a/src/typing/errors/flow_intermediate_error_types.ml b/src/typing/errors/flow_intermediate_error_types.ml index 19875f2f15f..bbabc85d98d 100644 --- a/src/typing/errors/flow_intermediate_error_types.ml +++ b/src/typing/errors/flow_intermediate_error_types.ml @@ -9,7 +9,6 @@ open Reason type 'loc explanation = | ExplanationAbstractEnumCasting - | ExplanationAlreadyPrinted of Loc.t Flow_errors_utils.Friendly.message_feature list | ExplanationArrayInvariantTyping | ExplanationConstrainedAssign of { name: string; @@ -26,6 +25,8 @@ type 'loc explanation = | ExplanationReactComponentPropsDeepReadOnly of 'loc | ExplanationReactComponentRefRequirement | ExplanationReactHookArgsDeepReadOnly of 'loc + | ExplanationReactHookIncompatibleWithEachOther + | ExplanationReactHookIncompatibleWithNormalFunctions | ExplanationReactHookReturnDeepReadOnly of 'loc | ExplanationRenderTypeRequirement | ExplanationTypeGuardCompatibility