From c96454a35d9dc7ba82fc6de5e723f4f875c39ee4 Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Fri, 3 Jan 2025 20:33:04 -0800 Subject: [PATCH] [flow][match] Add autocomplete for `match` as a keyword in an expression context if enabled Summary: Add autocomplete for `match` as a keyword in an expression context if pattern matching expressions are enabled. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D67817950 fbshipit-source-id: 2a01e14a71f4540adfd16f5418d05139227cef87 --- .../autocomplete/autocompleteService_js.ml | 2 ++ src/services/autocomplete/keywords.ml | 16 +++++++++++----- src/services/autocomplete/keywords.mli | 6 +++++- tests/autocomplete_match/.flowconfig | 3 +++ tests/autocomplete_match/.testconfig | 1 + .../autocomplete_match/autocomplete_match.exp | 19 +++++++++++++++++++ .../autocomplete_match/keyword-expression.js | 2 ++ tests/autocomplete_match/test.sh | 9 +++++++++ 8 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tests/autocomplete_match/.flowconfig create mode 100644 tests/autocomplete_match/.testconfig create mode 100644 tests/autocomplete_match/autocomplete_match.exp create mode 100644 tests/autocomplete_match/keyword-expression.js create mode 100644 tests/autocomplete_match/test.sh diff --git a/src/services/autocomplete/autocompleteService_js.ml b/src/services/autocomplete/autocompleteService_js.ml index e1f65c775ce..3a75943e57f 100644 --- a/src/services/autocomplete/autocompleteService_js.ml +++ b/src/services/autocomplete/autocompleteService_js.ml @@ -939,6 +939,8 @@ let autocomplete_id let keywords = Keywords.keywords_at_loc ~component_syntax_enabled:(Context.component_syntax typing.cx) + ~pattern_matching_expressions_enabled: + (Context.enable_pattern_matching_expressions typing.cx) typing.ast ac_loc |> Base.List.map ~f:(AcCompletion.of_keyword ~edit_locs) diff --git a/src/services/autocomplete/keywords.ml b/src/services/autocomplete/keywords.ml index 067e71b4943..6eb9514b216 100644 --- a/src/services/autocomplete/keywords.ml +++ b/src/services/autocomplete/keywords.ml @@ -19,8 +19,13 @@ type context_node = (* TODO: include `of`, `in`, and `instanceof`. We don't currently autocomplete at positions where those are valid. *) (* true, false, and null are not included here, because we already suggest those when we have type info *) -let expression_keywords = +let expression_keywords ~pattern_matching_expressions_enabled = ["async"; "await"; "class"; "delete"; "function"; "import"; "new"; "typeof"; "void"; "yield"] + @ + if pattern_matching_expressions_enabled then + ["match"] + else + [] (** keywords to suggest in a statement (or expression statement) context, in almost-alphabetical order. @@ -145,7 +150,7 @@ class mapper target = super#identifier (loc, id) end -let keywords_of_context ~component_syntax_enabled context = +let keywords_of_context ~component_syntax_enabled ~pattern_matching_expressions_enabled context = match context with | Expression :: ExpressionStatement :: _ | Statement :: _ -> @@ -156,10 +161,10 @@ let keywords_of_context ~component_syntax_enabled context = | Expression :: Member :: _ | Expression :: SwitchCase :: _ -> [] - | Expression :: _ -> expression_keywords + | Expression :: _ -> expression_keywords ~pattern_matching_expressions_enabled | _ -> [] -let keywords_at_loc ~component_syntax_enabled ast loc = +let keywords_at_loc ~component_syntax_enabled ~pattern_matching_expressions_enabled ast loc = (* We're looking for an identifier, considering the first character is equivalent. *) let target = Loc.first_char loc in let mapper = new mapper target in @@ -167,4 +172,5 @@ let keywords_at_loc ~component_syntax_enabled ast loc = ignore (mapper#program ast); [] with - | Found context -> keywords_of_context ~component_syntax_enabled context + | Found context -> + keywords_of_context ~component_syntax_enabled ~pattern_matching_expressions_enabled context diff --git a/src/services/autocomplete/keywords.mli b/src/services/autocomplete/keywords.mli index 16bca2ea94b..08bd06a275c 100644 --- a/src/services/autocomplete/keywords.mli +++ b/src/services/autocomplete/keywords.mli @@ -6,4 +6,8 @@ *) val keywords_at_loc : - component_syntax_enabled:bool -> (Loc.t, Loc.t) Flow_ast.Program.t -> Loc.t -> string list + component_syntax_enabled:bool -> + pattern_matching_expressions_enabled:bool -> + (Loc.t, Loc.t) Flow_ast.Program.t -> + Loc.t -> + string list diff --git a/tests/autocomplete_match/.flowconfig b/tests/autocomplete_match/.flowconfig new file mode 100644 index 00000000000..9eb58ffb294 --- /dev/null +++ b/tests/autocomplete_match/.flowconfig @@ -0,0 +1,3 @@ +[options] +all=true +experimental.pattern_matching_expressions=true diff --git a/tests/autocomplete_match/.testconfig b/tests/autocomplete_match/.testconfig new file mode 100644 index 00000000000..5a3e9f8a853 --- /dev/null +++ b/tests/autocomplete_match/.testconfig @@ -0,0 +1 @@ +shell: test.sh diff --git a/tests/autocomplete_match/autocomplete_match.exp b/tests/autocomplete_match/autocomplete_match.exp new file mode 100644 index 00000000000..ebe0cad4f00 --- /dev/null +++ b/tests/autocomplete_match/autocomplete_match.exp @@ -0,0 +1,19 @@ +keyword-expression.js:1:10 +Flags: --pretty +{ + "result":[ + {"name":"async","type":""}, + {"name":"await","type":""}, + {"name":"class","type":""}, + {"name":"delete","type":""}, + {"name":"e","type":"empty"}, + {"name":"function","type":""}, + {"name":"import","type":""}, + {"name":"match","type":""}, + {"name":"new","type":""}, + {"name":"typeof","type":""}, + {"name":"void","type":""}, + {"name":"yield","type":""} + ] +} + diff --git a/tests/autocomplete_match/keyword-expression.js b/tests/autocomplete_match/keyword-expression.js new file mode 100644 index 00000000000..b57435d9178 --- /dev/null +++ b/tests/autocomplete_match/keyword-expression.js @@ -0,0 +1,2 @@ +const e = +// ^ diff --git a/tests/autocomplete_match/test.sh b/tests/autocomplete_match/test.sh new file mode 100644 index 00000000000..6f30444ac61 --- /dev/null +++ b/tests/autocomplete_match/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +# shellcheck disable=SC2094 + +queries_in_file autocomplete "keyword-expression.js" --pretty