Skip to content

Commit

Permalink
[flow][match] Get def for match patterns
Browse files Browse the repository at this point in the history
Summary:
Get def for match patterns. Already mostly worked, implemented for member patterns.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D67830343

fbshipit-source-id: ac6ff8f676b1d5a2f5f57012586b9afff0f1b8c9
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 7, 2025
1 parent a8ea098 commit b9cc018
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/services/get_def/getDefUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ module Def_kind_search = struct
());
super#member loc expr

method! match_member_pattern member_pattern =
let open Flow_ast.MatchPattern.MemberPattern in
let (_, { base; property; _ }) = member_pattern in
(match property with
| PropertyIdentifier ((id_loc, _), { Flow_ast.Identifier.name; _ }) ->
if covers_target id_loc then
let base_t =
match base with
| BaseIdentifier ((_, t), _)
| BaseMember ((_, t), _) ->
t
in
raise (Found (Use (base_t, name)))
| _ -> ());
super#match_member_pattern member_pattern

method! object_property prop =
let open Flow_ast.Expression.Object.Property in
let (_prop_loc, prop') = prop in
Expand Down
41 changes: 41 additions & 0 deletions src/services/get_def/get_def_process_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
end;
super#member loc expr

method! match_member_pattern member_pattern =
let open Flow_ast.MatchPattern.MemberPattern in
let (_, { base; property; _ }) = member_pattern in
begin
match property with
| PropertyIdentifier (annot, { Ast.Identifier.name; _ }) when this#annot_covers_target annot
->
let base_annot =
match base with
| BaseIdentifier (annot, _)
| BaseMember (annot, _) ->
annot
in
let base_annot =
(this#loc_of_annot base_annot, this#type_from_enclosing_node base_annot)
in
let result =
Get_def_request.(
Member { prop_name = name; object_type = base_annot; force_instance = false }
)
in
this#request result
| _ -> ()
end;
super#match_member_pattern member_pattern

method! generic_type expr =
let open Ast.Type.Generic in
let { id; targs; comments = _ } = expr in
Expand Down Expand Up @@ -626,6 +652,21 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
| Some l -> this#own_named_def l name
else
pn

(* If shorthand syntax (e.g. `const foo`), don't map over the 'key' as its
location also covers the binding name. *)
method! match_object_pattern_property prop =
let open Ast.MatchPattern.ObjectPattern.Property in
let (loc, { key; pattern; shorthand; comments }) = prop in
let key =
if shorthand then
key
else
this#match_object_pattern_property_key key
in
let pattern = this#match_pattern pattern in
let comments = this#syntax_opt comments in
(this#on_loc_annot loc, { key; pattern; shorthand; comments })
end

class typed_ast_searcher _cx ~typed_ast:_ ~is_local_use ~is_legit_require ~covers_target ~purpose =
Expand Down
3 changes: 3 additions & 0 deletions tests/get_def_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/get_def_match/.testconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shell: test.sh
16 changes: 16 additions & 0 deletions tests/get_def_match/bindings-introduced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare const x: mixed;

const e = match (x) {
1 as foo: foo,
// ^
2 as const foo: foo,
// ^
{const foo}: foo,
// ^
{...const foo}: foo,
// ^
[...const foo]: foo,
// ^
const foo: foo,
// ^
};
44 changes: 44 additions & 0 deletions tests/get_def_match/get_def_match.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
bindings-introduced.js:4:13
Flags:
bindings-introduced.js:4:8,4:10

bindings-introduced.js:6:19
Flags:
bindings-introduced.js:6:14,6:16

bindings-introduced.js:8:16
Flags:
bindings-introduced.js:8:10,8:12

bindings-introduced.js:10:19
Flags:
bindings-introduced.js:10:13,10:15

bindings-introduced.js:12:19
Flags:
bindings-introduced.js:12:13,12:15

bindings-introduced.js:14:14
Flags:
bindings-introduced.js:14:9,14:11

pattern-identifier.js:6:5
Flags:
pattern-identifier.js:3:15,3:17

pattern-identifier.js:8:6
Flags:
pattern-identifier.js:3:15,3:17

pattern-member.js:6:5
Flags:
pattern-member.js:3:15,3:17

pattern-member.js:8:9
Flags:
pattern-member.js:3:21,3:23

pattern-object.js:6:6
Flags:
pattern-object.js:6:6,6:8

11 changes: 11 additions & 0 deletions tests/get_def_match/pattern-identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare const x: mixed;

declare const foo: 1;

const e = match (x) {
foo: 0,
// ^
[foo]: 0,
// ^
_: 0,
};
11 changes: 11 additions & 0 deletions tests/get_def_match/pattern-member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare const x: mixed;

declare const Obj: {bar: 1};

const e = match (x) {
Obj.foo: 0,
// ^
Obj.bar: 0,
// ^
_: 0,
};
9 changes: 9 additions & 0 deletions tests/get_def_match/pattern-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare const x: mixed;

declare const foo: 1; // Is not def

const e = match (x) {
{foo: true}: 0,
// ^
_: 0,
};
10 changes: 10 additions & 0 deletions tests/get_def_match/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/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.

queries_in_file "get-def" "bindings-introduced.js"
queries_in_file "get-def" "pattern-identifier.js"
queries_in_file "get-def" "pattern-member.js"
queries_in_file "get-def" "pattern-object.js"

0 comments on commit b9cc018

Please sign in to comment.