Skip to content

Commit

Permalink
[flow][numeric-keys] Fix various places in env_builder to properly ha…
Browse files Browse the repository at this point in the history
…ndle numeric keys

Summary:
This code in env builder was written before we supported numeric literal keys. Either it was not supporting numeric keys, or was not correctly handling them by using `raw` as the name.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D66911505

fbshipit-source-id: af20857506e87426743ae4d7e46600b7375a800a
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 9, 2024
1 parent aa6212f commit d956235
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,13 @@ let expression_is_definitely_synthesizable ~autocomplete_hooks =
(* Autocompletion in LTI will use hints to find the expected type of the object
we are completing. There are similar case for identifiers and literals below. *)
false
| Property.NumberLiteral (loc, { Ast.NumberLiteral.value = key_value; _ })
when Js_number.is_float_safe_integer key_value ->
let name = Dtoa.ecma_string_of_float key_value in
if autocomplete_hooks.Env_api.With_ALoc.obj_prop_decl_hook name loc then
false
else
synthesizable value
| _ -> synthesizable value)
| (_, Get { key = _; value = (_, fn); comments = _ })
| (_, Set { key = _; value = (_, fn); comments = _ })
Expand Down Expand Up @@ -1985,6 +1992,12 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
Some h
) ->
Base.Continue_or_stop.Continue (ObjectPropPatternHint (name, l, h) :: acc)
| ( Ast.Pattern.Object.Property.NumberLiteral (l, { Ast.NumberLiteral.value; _ }),
Some h
)
when Js_number.is_float_safe_integer value ->
let name = Dtoa.ecma_string_of_float value in
Base.Continue_or_stop.Continue (ObjectPropPatternHint (name, l, h) :: acc)
| _ -> Base.Continue_or_stop.Stop None)
| Ast.Pattern.Object.RestElement (_, { Ast.Pattern.RestElement.argument; _ }) ->
other_pattern_hint_opt argument
Expand Down Expand Up @@ -3031,6 +3044,21 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
)
when autocomplete_hooks.Env_api.With_ALoc.obj_prop_decl_hook name loc ->
true
| ( _,
Init
{
key =
Property.NumberLiteral (loc, { Ast.NumberLiteral.value = key_value; _ });
value;
_;
}
)
when Js_number.is_float_safe_integer key_value ->
let name = Dtoa.ecma_string_of_float key_value in
if autocomplete_hooks.Env_api.With_ALoc.obj_prop_decl_hook name loc then
true
else
expression_has_autocomplete ~autocomplete_hooks value
| (_, Init { value; _ }) -> expression_has_autocomplete ~autocomplete_hooks value
| _ -> false)
| SpreadProperty _ -> false
Expand All @@ -3048,6 +3076,10 @@ class def_finder ~autocomplete_hooks ~react_jsx env_info toplevel_scope =
let visit_object_key_and_compute_hint = function
| Ast.Expression.Object.Property.StringLiteral (_, { Ast.StringLiteral.value = name; _ }) ->
decompose_hints (Decomp_ObjProp name) object_hints
| Ast.Expression.Object.Property.NumberLiteral (_, { Ast.NumberLiteral.value; _ })
when Js_number.is_float_safe_integer value ->
let name = Dtoa.ecma_string_of_float value in
decompose_hints (Decomp_ObjProp name) object_hints
| Ast.Expression.Object.Property.NumberLiteral _
| Ast.Expression.Object.Property.BigIntLiteral _ ->
[]
Expand Down
8 changes: 5 additions & 3 deletions src/analysis/env_builder/name_resolver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5578,9 +5578,11 @@ module Make (Context : C) (FlowAPIUtils : F with type cx = Context.t) :
let propname =
match property with
| PropertyIdentifier (_, { Flow_ast.Identifier.name; _ })
| PropertyExpression (_, StringLiteral { Flow_ast.StringLiteral.value = name; _ })
| PropertyExpression
(_, NumberLiteral { Flow_ast.NumberLiteral.value = _; raw = name; _ }) ->
| PropertyExpression (_, StringLiteral { Flow_ast.StringLiteral.value = name; _ }) ->
Some name
| PropertyExpression (_, NumberLiteral { Flow_ast.NumberLiteral.value; _ })
when Js_number.is_float_safe_integer value ->
let name = Dtoa.ecma_string_of_float value in
Some name
| _ -> None
in
Expand Down
10 changes: 7 additions & 3 deletions src/analysis/env_builder/refinement_key.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ module Make (L : Loc_sig.S) : REFINEMENT_KEY with module L = L = struct
let open Ast.Expression.Member in
match property with
| PropertyIdentifier (_, { Ast.Identifier.name; comments = _ })
| PropertyExpression (_, Ast.Expression.StringLiteral { Ast.StringLiteral.value = name; _ })
| PropertyExpression (_, Ast.Expression.StringLiteral { Ast.StringLiteral.value = name; _ }) ->
(match lookup_of_expression ~allow_optional _object with
| Some { base; projections } -> Some { base; projections = Prop name :: projections }
| None -> None)
| PropertyExpression
(_, Ast.Expression.NumberLiteral { Ast.NumberLiteral.value = _; raw = name; comments = _ })
->
(_, Ast.Expression.NumberLiteral { Ast.NumberLiteral.value; raw = _; comments = _ })
when Js_number.is_float_safe_integer value ->
let name = Dtoa.ecma_string_of_float value in
(match lookup_of_expression ~allow_optional _object with
| Some { base; projections } -> Some { base; projections = Prop name :: projections }
| None -> None)
Expand Down

0 comments on commit d956235

Please sign in to comment.