Skip to content

Commit

Permalink
[flow][autocomplete] Autocomplete on string literal types returns emp…
Browse files Browse the repository at this point in the history
…ty result

Summary:
I tried to figure out what the intention was behind capturing the case of `StringLiteral` type in autocomplete, given that we never return an actual result.

In D18509589 and D18616795, we started returning `Ac_literal` as the `autocomplete_type`. This did not return an actual result and so we would log these as `AcEmpty`. This answered the question about why we caught this case in the first place.

In D21183466 we started returning results for *some* literals. We still did not return any meaningful result for string literal types, but this case was now also mapped to `AcReturn` result since the same `Ac_literal` constructor was still used.

Finally, with D54659383 the type returned used for auto-completing string type literals was hard-coded to `empty`.

This diff restores the original behavior of returning `AcEmpty` when auto-completing in a string literal type. It also reverts the change of D54659383, so that we don't have to use the hook in type_annotation.ml.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D54884663

fbshipit-source-id: 29dfc8af6063e8e953bb7068e9cf942411f183b5
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 14, 2024
1 parent 7a6d3fd commit c23d028
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/services/autocomplete/autocompleteService_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,8 @@ let autocomplete_get_results typing ac_options trigger_character cursor =
| Ac_enum -> AcEmpty "Enum"
| Ac_key { obj_type; used_keys; spreads } ->
autocomplete_object_key ~typing ~edit_locs ~token ~used_keys ~spreads obj_type
| Ac_literal { lit_type } ->
| Ac_literal { lit_type = None } -> AcEmpty "Literal"
| Ac_literal { lit_type = Some lit_type } ->
let genv =
Ty_normalizer_flow.mk_genv
~options:ty_normalizer_options
Expand Down
17 changes: 8 additions & 9 deletions src/services/autocomplete/autocomplete_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type autocomplete_type =
used_keys: SSet.t;
spreads: (Loc.t * Type.t) list;
} (** object key *)
| Ac_literal of { lit_type: Type.t } (** inside a string literal *)
| Ac_literal of { lit_type: Type.t option } (** inside a string literal *)
| Ac_module (** a module name *)
| Ac_type of { allow_react_element_shorthand: bool } (** type identifiers *)
| Ac_type_binding (** introduces a new type name. like Ac_binding, but for types *)
Expand Down Expand Up @@ -420,7 +420,7 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
match value with
| StringLiteral ((loc, lit_type), { Flow_ast.StringLiteral.raw; _ })
when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type })
this#find loc raw (Ac_literal { lit_type = Some lit_type })
| _ -> super#jsx_attribute_value value

method! jsx_child child =
Expand All @@ -434,7 +434,7 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
match key with
(* TODO: we shouldn't have to fabricate a type here! *)
| StringLiteral (loc, { Flow_ast.StringLiteral.raw; _ }) when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type = Type.(AnyT.at Untyped loc) })
this#find loc raw (Ac_literal { lit_type = Some Type.(AnyT.at Untyped loc) })
| _ -> super#pattern_object_property_key ?kind key

method! class_key key =
Expand All @@ -453,7 +453,7 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
match key with
| StringLiteral ((loc, lit_type), Flow_ast.StringLiteral.{ raw; _ })
when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type })
this#find loc raw (Ac_literal { lit_type = Some lit_type })
| _ -> super#object_key key

method! enum_member_identifier ((loc, Flow_ast.Identifier.{ name; _ }) as ident) =
Expand Down Expand Up @@ -570,7 +570,7 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
match expr with
| ((loc, lit_type), StringLiteral Flow_ast.StringLiteral.{ raw; _ })
when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type })
this#find loc raw (Ac_literal { lit_type = Some lit_type })
| (annot, Member member) ->
(this#on_type_annot annot, Member (this#member_with_loc annot member))
| (((loc, _) as annot), OptionalMember opt_member) ->
Expand Down Expand Up @@ -632,7 +632,7 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
| (loc, Flow_ast.Expression.TemplateLiteral.Element.{ value = { raw; _ }; _ })
when this#covers_target loc ->
(* TODO: we shouldn't have to fabricate a type here! *)
this#find loc raw (Ac_literal { lit_type = Type.(AnyT.at Untyped loc) })
this#find loc raw (Ac_literal { lit_type = Some Type.(AnyT.at Untyped loc) })
| _ -> super#template_literal_element elem

method! import_declaration decl_loc decl =
Expand Down Expand Up @@ -749,9 +749,8 @@ class process_request_searcher (from_trigger_character : bool) (cursor : Loc.t)
method! type_ t =
let open Flow_ast.Type in
match t with
| ((loc, lit_type), StringLiteral { Flow_ast.StringLiteral.raw; _ })
when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type })
| ((loc, _), StringLiteral { Flow_ast.StringLiteral.raw; _ }) when this#covers_target loc ->
this#find loc raw (Ac_literal { lit_type = None })
| (((loc, _) as annot), IndexedAccess ia) ->
(this#on_type_annot annot, IndexedAccess (this#indexed_access_type_with_loc loc ia))
| (((loc, _) as annot), OptionalIndexedAccess ia) ->
Expand Down
2 changes: 1 addition & 1 deletion src/services/autocomplete/autocomplete_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type autocomplete_type =
used_keys: SSet.t;
spreads: (Loc.t * Type.t) list;
} (** object key *)
| Ac_literal of { lit_type: Type.t } (** inside a literal like a string or regex *)
| Ac_literal of { lit_type: Type.t option } (** inside a literal like a string or regex *)
| Ac_module (** a module name *)
| Ac_type of { allow_react_element_shorthand: bool } (** type identifiers *)
| Ac_type_binding
Expand Down
7 changes: 1 addition & 6 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,7 @@ module Make (ConsGen : Type_annotation_sig.ConsGen) (Statement : Statement_sig.S
let (tparam, t) = ALocMap.find tparam_loc env.infer_tparams_map in
((loc, t), Infer { Infer.tparam; comments }))
| (loc, (StringLiteral { Ast.StringLiteral.value; _ } as t_ast)) ->
let t =
if Type_inference_hooks_js.dispatch_literal_hook env.cx loc then
EmptyT.at loc
else
mk_singleton_string loc value
in
let t = mk_singleton_string loc value in
((loc, t), t_ast)
| (loc, (NumberLiteral { Ast.NumberLiteral.value; raw; _ } as t_ast)) ->
((loc, mk_singleton_number loc value raw), t_ast)
Expand Down

0 comments on commit c23d028

Please sign in to comment.