From c23d028122071e40b20499ba197f889d6b7277b6 Mon Sep 17 00:00:00 2001 From: Panos Vekris Date: Thu, 14 Mar 2024 10:04:39 -0700 Subject: [PATCH] [flow][autocomplete] Autocomplete on string literal types returns empty 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 --- .../autocomplete/autocompleteService_js.ml | 3 ++- src/services/autocomplete/autocomplete_js.ml | 17 ++++++++--------- src/services/autocomplete/autocomplete_js.mli | 2 +- src/typing/type_annotation.ml | 7 +------ 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/services/autocomplete/autocompleteService_js.ml b/src/services/autocomplete/autocompleteService_js.ml index 1fcb659acc7..f891bd85596 100644 --- a/src/services/autocomplete/autocompleteService_js.ml +++ b/src/services/autocomplete/autocompleteService_js.ml @@ -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 diff --git a/src/services/autocomplete/autocomplete_js.ml b/src/services/autocomplete/autocomplete_js.ml index 9272093401f..19ee2137858 100644 --- a/src/services/autocomplete/autocomplete_js.ml +++ b/src/services/autocomplete/autocomplete_js.ml @@ -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 *) @@ -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 = @@ -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 = @@ -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) = @@ -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) -> @@ -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 = @@ -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) -> diff --git a/src/services/autocomplete/autocomplete_js.mli b/src/services/autocomplete/autocomplete_js.mli index 96100a14e01..5fe9dbe166e 100644 --- a/src/services/autocomplete/autocomplete_js.mli +++ b/src/services/autocomplete/autocomplete_js.mli @@ -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 diff --git a/src/typing/type_annotation.ml b/src/typing/type_annotation.ml index 159d0d24b2a..ffe17d619f3 100644 --- a/src/typing/type_annotation.ml +++ b/src/typing/type_annotation.ml @@ -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)