Skip to content

Commit

Permalink
[flow][match] Autocomplete for identifier patterns in match expressions
Browse files Browse the repository at this point in the history
Summary:
Autocomplete for identifier patterns in match expressions.
Also, tests confirming that we don't autocomplete binding pattern names, and don't autocomplete object pattern keys with local identifiers.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D67830066

fbshipit-source-id: 50d03368b9c586ad4e1e19418c5877e52e2b3d7c
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 7, 2025
1 parent 24bb379 commit b62f486
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/services/autocomplete/autocomplete_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,20 @@ class process_request_searcher cx ~from_trigger_character ~cursor =
this#find loc name Ac_type_binding
else
id

(* Don't autocomplete match object pattern property keys. *)
method! match_object_pattern_property_key key =
let open Ast.MatchPattern.ObjectPattern.Property in
let (loc, token) =
match key with
| StringLiteral (loc, { Ast.StringLiteral.raw; _ }) -> (loc, raw)
| NumberLiteral (loc, { Ast.NumberLiteral.raw; _ }) -> (loc, raw)
| Identifier (loc, { Ast.Identifier.name; _ }) -> (loc, name)
in
if this#covers_target loc then
this#find loc token Ac_ignored
else
key
end

let autocomplete_id ~cursor _cx _ac_name ac_loc = covers_target cursor ac_loc
Expand Down
7 changes: 7 additions & 0 deletions src/services/autocomplete/keywords.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type context_node =
| ObjectKey
| Type
| SwitchCase
| MatchPattern

(* 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 *)
Expand Down Expand Up @@ -103,6 +104,8 @@ let export_default_keywords ~component_syntax_enabled =
else
[]

let match_pattern_keywords = ["const"]

exception Found of context_node list

class mapper target =
Expand Down Expand Up @@ -145,6 +148,9 @@ class mapper target =

method! switch_case case = this#with_context SwitchCase (fun () -> super#switch_case case)

method! match_pattern pattern =
this#with_context MatchPattern (fun () -> super#match_pattern pattern)

method! identifier (loc, id) =
if this#target_contained_by loc then raise (Found context);
super#identifier (loc, id)
Expand All @@ -162,6 +168,7 @@ let keywords_of_context ~component_syntax_enabled ~pattern_matching_expressions_
| Expression :: SwitchCase :: _ ->
[]
| Expression :: _ -> expression_keywords ~pattern_matching_expressions_enabled
| MatchPattern :: _ when pattern_matching_expressions_enabled -> match_pattern_keywords
| _ -> []

let keywords_at_loc ~component_syntax_enabled ~pattern_matching_expressions_enabled ast loc =
Expand Down
28 changes: 28 additions & 0 deletions tests/autocomplete_match/autocomplete_match.exp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ Flags: --pretty
]
}

pattern-identifier.js:6:5
Flags: --pretty
{"result":[{"name":"foo","type":"1"}]}

pattern-binding.js:6:14
Flags: --pretty
{"result":[]}

pattern-binding.js:8:14
Flags: --pretty
{"result":[]}

pattern-binding.js:10:9
Flags: --pretty
{"result":[]}

pattern-binding.js:12:10
Flags: --pretty
{"result":[]}

pattern-binding.js:15:4
Flags: --pretty
{"result":[{"name":"const","type":""}]}

pattern-object.js:6:5
Flags: --pretty
{"result":[]}

17 changes: 17 additions & 0 deletions tests/autocomplete_match/pattern-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare const x: number;

const foo = "foo"; // Will not be in autocomplete results below.

const e = match (x) {
[...const f ]: 0,
// ^
{...const f }: 0,
// ^
1 as f : 0,
// ^
const f : 0,
// ^

c
// ^
};
8 changes: 8 additions & 0 deletions tests/autocomplete_match/pattern-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const x: number;

declare const foo: 1;

const e = match (x) {
f
// ^
};
8 changes: 8 additions & 0 deletions tests/autocomplete_match/pattern-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const x: number;

const foo = "foo"; // Will not be in autocomplete results below.

const e = match (x) {
{f
// ^
};
3 changes: 3 additions & 0 deletions tests/autocomplete_match/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
# shellcheck disable=SC2094

queries_in_file autocomplete "keyword-expression.js" --pretty
queries_in_file autocomplete "pattern-identifier.js" --pretty
queries_in_file autocomplete "pattern-binding.js" --pretty
queries_in_file autocomplete "pattern-object.js" --pretty

0 comments on commit b62f486

Please sign in to comment.