Skip to content

Commit

Permalink
[flow][match] Add autocomplete for match as a keyword in an express…
Browse files Browse the repository at this point in the history
…ion 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: 4aecd64be825474412af7927c0007eae68f0c605
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 7, 2025
1 parent c752e15 commit 24bb379
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/services/autocomplete/autocompleteService_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions src/services/autocomplete/keywords.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 :: _ ->
Expand All @@ -156,15 +161,16 @@ 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
try
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
6 changes: 5 additions & 1 deletion src/services/autocomplete/keywords.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions tests/autocomplete_match/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options]
all=true
experimental.pattern_matching_expressions=true
1 change: 1 addition & 0 deletions tests/autocomplete_match/.testconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shell: test.sh
19 changes: 19 additions & 0 deletions tests/autocomplete_match/autocomplete_match.exp
Original file line number Diff line number Diff line change
@@ -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":""}
]
}

2 changes: 2 additions & 0 deletions tests/autocomplete_match/keyword-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const e =
// ^
9 changes: 9 additions & 0 deletions tests/autocomplete_match/test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 24bb379

Please sign in to comment.