Skip to content

Commit

Permalink
[flow][match] Error on invalid numeric object property literals
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 20, 2024
1 parent db9f8a0 commit ea7dadd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 2 additions & 0 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = _ } ->
Expand Down
9 changes: 8 additions & 1 deletion src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 _
Expand Down Expand Up @@ -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 _
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/typing/errors/flow_intermediate_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = _ }
=
Expand Down
1 change: 1 addition & 0 deletions src/typing/errors/flow_intermediate_error_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/typing/match_pattern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea7dadd

Please sign in to comment.