@@ -14,11 +14,53 @@ type member = Method of string
1414let findrefs_member_of_member = function
1515 | Method m -> SearchTypes.Find_refs. Method m
1616
17- let is_test_file path =
18- Option. is_some (String. substr_index path ~pattern: " /__tests__/" )
17+ let supported_test_framework_classes = SSet. of_list [" WWWTest" ]
1918
2019let strip_ns results = List. map results ~f: (fun (s , p ) -> (Utils. strip_ns s, p))
2120
21+ let is_test_file ctx file_path file_nast =
22+ let is_test_class class_name =
23+ match Decl_provider. get_class ctx class_name with
24+ | Decl_entry. DoesNotExist ->
25+ failwith
26+ (Printf. sprintf
27+ " Internal error: Could not find class %s, even though we encountered it during traversal"
28+ class_name)
29+ | NotYetAvailable ->
30+ failwith
31+ (Printf. sprintf
32+ " Internal error: Class %s, marked as not yet available"
33+ class_name)
34+ | Found class_ ->
35+ let ancestors = Folded_class. all_ancestor_names class_ in
36+ List. exists ancestors ~f: (fun ancestor ->
37+ SSet. mem (Utils. strip_ns ancestor) supported_test_framework_classes)
38+ in
39+
40+ match String. substr_index file_path ~pattern: " /__tests__/" with
41+ | None -> false
42+ | Some _ ->
43+ let classes_in_file =
44+ let visitor =
45+ object
46+ inherit [_] Aast. reduce
47+
48+ method! on_class_ _ctx class_ =
49+ SSet. singleton (Ast_defs. get_id class_.c_name)
50+
51+ method! on_method_ _ctx _ = SSet. empty
52+
53+ method! on_fun_ _ctx _ = SSet. empty
54+
55+ method plus = SSet. union
56+
57+ method zero = SSet. empty
58+ end
59+ in
60+ visitor#on_program () file_nast
61+ in
62+ SSet. exists is_test_class classes_in_file
63+
2264let search ctx target include_defs ~files genv =
2365 List. iter files ~f: (fun file ->
2466 Hh_logger. debug
@@ -109,16 +151,8 @@ type result_entry = ServerCommandTypes.Find_my_tests.result_entry
109151 Ideally, FindRefsService (or an alternative to it) would give us the enclosing SymbolDefinition
110152 directly, but that would require piping that information through IdentifySymbolService.
111153*)
112- let enclosing_def_of_symbol_occurrence ctx (symbol_occurrence_pos : Pos.t ) :
113- symbol_def option =
114- (* We could implement all this without NASTs, but just CSTs and their
115- Full_fidelity_positioned_syntax.parentage function.
116-
117- But those never seem to be cached, whereas the NAST may still be in cache after we just
118- constructed the TAST in FindRefsService. *)
119- let path = Pos. filename symbol_occurrence_pos in
120- let nast = Ast_provider. get_ast ~full: true ctx path in
121-
154+ let enclosing_def_of_symbol_occurrence
155+ (symbol_occurrence_pos : Pos.t ) symbol_occurrence_nast : symbol_def option =
122156 let visitor =
123157 object
124158 inherit [_] Aast. reduce as super
@@ -149,7 +183,7 @@ let enclosing_def_of_symbol_occurrence ctx (symbol_occurrence_pos : Pos.t) :
149183 | (Some _ , _ ) -> r1
150184 end
151185 in
152- visitor#on_program None nast
186+ visitor#on_program None symbol_occurrence_nast
153187
154188(* *
155189 Find references to the given symbol.
@@ -172,13 +206,22 @@ let get_references
172206 env
173207 in
174208 let resolve_found_position (acc_symbol_defs , acc_files ) (_ , referencing_pos ) =
175- let file = Pos. filename referencing_pos |> Relative_path. suffix in
176- if is_test_file file then
209+ let relative_path = Pos. filename referencing_pos in
210+ let path = Relative_path. suffix relative_path in
211+
212+ (* We could implement all of our traversals without NASTs, but just CSTs and their
213+ Full_fidelity_positioned_syntax.parentage function.
214+
215+ But those never seem to be cached, whereas the NAST may still be in cache after we just
216+ constructed the TAST in FindRefsService. *)
217+ let nast = Ast_provider. get_ast ~full: true ctx relative_path in
218+
219+ if is_test_file ctx path nast then
177220 Result. Ok
178221 ( acc_symbol_defs,
179222 (Pos. to_absolute referencing_pos |> Pos. filename) :: acc_files )
180223 else
181- match enclosing_def_of_symbol_occurrence ctx referencing_pos with
224+ match enclosing_def_of_symbol_occurrence referencing_pos nast with
182225 | Some referencing_def ->
183226 Result. Ok (referencing_def :: acc_symbol_defs, acc_files)
184227 | None ->
0 commit comments