From ea7dadd09677ca251812ddb0fedf8b176669614b Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Fri, 20 Dec 2024 13:06:29 -0800 Subject: [PATCH] [flow][match] Error on invalid numeric object property literals Summary: Error on invalid numeric object property literals. The wording is as it is so in the future when we add support for parsing bigint property names here, we can use the same error for them. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D67502327 fbshipit-source-id: 3741e274b861af84af22e206759e291bf25df305 --- src/analysis/env_builder/name_def.ml | 1 - src/typing/debug_js.ml | 2 ++ src/typing/errors/error_message.ml | 9 ++++++++- src/typing/errors/flow_intermediate_error.ml | 5 +++++ src/typing/errors/flow_intermediate_error_types.ml | 1 + src/typing/match_pattern.ml | 9 +++++---- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/analysis/env_builder/name_def.ml b/src/analysis/env_builder/name_def.ml index e504f9d052f..83a2760a296 100644 --- a/src/analysis/env_builder/name_def.ml +++ b/src/analysis/env_builder/name_def.ml @@ -304,7 +304,6 @@ end = struct let selector = Selector.Prop { prop; prop_loc = loc; has_default = false } in (Select { selector; parent = acc }, prop) else - (* TODO:match error condition *) (snd acc, "") let object_rest acc used_props = diff --git a/src/typing/debug_js.ml b/src/typing/debug_js.ml index 82054e8322b..c5bdd4dcb2a 100644 --- a/src/typing/debug_js.ml +++ b/src/typing/debug_js.ml @@ -1905,6 +1905,8 @@ let dump_error_message = "EMatchInvalidBindingKind (%s) (%s)" (string_of_aloc loc) (Flow_ast_utils.string_of_variable_kind kind) + | EMatchInvalidObjectPropertyLiteral { loc } -> + spf "EMatchInvalidObjectPropertyLiteral (%s)" (string_of_aloc loc) | EDevOnlyRefinedLocInfo { refined_loc; refining_locs = _ } -> spf "EDevOnlyRefinedLocInfo {refined_loc=%s}" (string_of_aloc refined_loc) | EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info = _ } -> diff --git a/src/typing/errors/error_message.ml b/src/typing/errors/error_message.ml index c91635b5774..11df5685ab5 100644 --- a/src/typing/errors/error_message.ml +++ b/src/typing/errors/error_message.ml @@ -622,6 +622,7 @@ and 'loc t' = loc: 'loc; kind: Flow_ast.Variable.kind; } + | EMatchInvalidObjectPropertyLiteral of { loc: 'loc } (* Dev only *) | EDevOnlyRefinedLocInfo of { refined_loc: 'loc; @@ -1426,6 +1427,7 @@ let rec map_loc_of_error_message (f : 'a -> 'b) : 'a t' -> 'b t' = | EMatchNotExhaustive { loc; reason } -> EMatchNotExhaustive { loc = f loc; reason = map_reason reason } | EMatchInvalidBindingKind { loc; kind } -> EMatchInvalidBindingKind { loc = f loc; kind } + | EMatchInvalidObjectPropertyLiteral { loc } -> EMatchInvalidObjectPropertyLiteral { loc = f loc } | EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info } -> EDevOnlyInvalidatedRefinementInfo { @@ -1724,7 +1726,8 @@ let util_use_op_of_msg nope util = function | EUnionOptimizationOnNonUnion _ | ECannotCallReactComponent _ | EMatchNotExhaustive _ - | EMatchInvalidBindingKind _ -> + | EMatchInvalidBindingKind _ + | EMatchInvalidObjectPropertyLiteral _ -> nope (* Not all messages (i.e. those whose locations are based on use_ops) have locations that can be @@ -1927,6 +1930,7 @@ let loc_of_msg : 'loc t' -> 'loc option = function | EUnusedPromise { loc; _ } -> Some loc | EMatchNotExhaustive { loc; _ } -> Some loc | EMatchInvalidBindingKind { loc; _ } -> Some loc + | EMatchInvalidObjectPropertyLiteral { loc } -> Some loc | EDevOnlyRefinedLocInfo { refined_loc; refining_locs = _ } -> Some refined_loc | EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info = _ } -> Some read_loc | EUnableToSpread _ @@ -2876,6 +2880,8 @@ let friendly_message_of_msg = function | ECannotCallReactComponent { reason } -> Normal (MessageCannotCallReactComponent reason) | EMatchNotExhaustive { loc = _; reason } -> Normal (MessageMatchNotExhaustive reason) | EMatchInvalidBindingKind { loc = _; kind } -> Normal (MessageMatchInvalidBindingKind { kind }) + | EMatchInvalidObjectPropertyLiteral { loc = _ } -> + Normal MessageMatchInvalidObjectPropertyLiteral let defered_in_speculation = function | EUntypedTypeImport _ @@ -3214,3 +3220,4 @@ let error_code_of_message err : error_code option = | ECannotCallReactComponent _ -> Some ReactRuleCallComponent | EMatchNotExhaustive _ -> Some MatchNotExhaustive | EMatchInvalidBindingKind _ -> Some MatchInvalidPattern + | EMatchInvalidObjectPropertyLiteral _ -> Some MatchInvalidPattern diff --git a/src/typing/errors/flow_intermediate_error.ml b/src/typing/errors/flow_intermediate_error.ml index 0862c98a095..45f84d8bcd0 100644 --- a/src/typing/errors/flow_intermediate_error.ml +++ b/src/typing/errors/flow_intermediate_error.ml @@ -3997,6 +3997,11 @@ let to_printable_error : code "const"; text " is allowed."; ] + | MessageMatchInvalidObjectPropertyLiteral -> + [ + text "Unsupported object property literal in match pattern. "; + text "String literals and int-like number literals are supported."; + ] in let rec convert_error_message { kind; loc; error_code; root; message; misplaced_source_file = _ } = diff --git a/src/typing/errors/flow_intermediate_error_types.ml b/src/typing/errors/flow_intermediate_error_types.ml index 18e5e86935b..9b3e3dc304c 100644 --- a/src/typing/errors/flow_intermediate_error_types.ml +++ b/src/typing/errors/flow_intermediate_error_types.ml @@ -899,6 +899,7 @@ type 'loc message = } | MessageMatchNotExhaustive of 'loc virtual_reason | MessageMatchInvalidBindingKind of { kind: Flow_ast.Variable.kind } + | MessageMatchInvalidObjectPropertyLiteral type 'loc intermediate_error = { kind: Flow_errors_utils.error_kind; diff --git a/src/typing/match_pattern.ml b/src/typing/match_pattern.ml index 1c68aa23a01..d2b22d1996e 100644 --- a/src/typing/match_pattern.ml +++ b/src/typing/match_pattern.ml @@ -37,7 +37,7 @@ let object_named_property acc loc name = } ) -let object_property_key acc key : +let object_property_key cx acc key : (ALoc.t, ALoc.t) Flow_ast.Expression.t * (ALoc.t, ALoc.t * Type.t) Ast.MatchPattern.ObjectPattern.Property.key = let open Ast.MatchPattern.ObjectPattern in @@ -54,9 +54,10 @@ let object_property_key acc key : let prop = Dtoa.ecma_string_of_float value in let acc = object_named_property acc loc prop in (acc, Property.NumberLiteral (loc, lit)) - else - (* TODO:match custom error *) + else ( + Flow_js.add_output cx (Error_message.EMatchInvalidObjectPropertyLiteral { loc }); (acc, Property.NumberLiteral (loc, lit)) + ) let binding cx ~on_binding ~kind acc name_loc name = let reason = mk_reason (RIdentifier (OrdinaryName name)) name_loc in @@ -189,7 +190,7 @@ and object_properties cx ~on_identifier ~on_expression ~on_binding acc props = let rec loop acc rev_props = function | [] -> List.rev rev_props | (loc, { Property.key; pattern = p; shorthand; comments }) :: props -> - let (acc, key) = object_property_key acc key in + let (acc, key) = object_property_key cx acc key in let p = pattern cx ~on_identifier ~on_expression ~on_binding acc p in let prop = (loc, { Property.key; pattern = p; shorthand; comments }) in loop acc (prop :: rev_props) props