Skip to content

Commit 46881dd

Browse files
SamChou19815facebook-github-bot
authored andcommitted
[flow] Scoped libdef support 2/n: Filter out unavailable results for IDE services
Summary: In this diff, we will address the shortcoming from the previous diff, where we index every single globals, even if the scoped libdef config says that it won't be active for the current file. This diff addresses it in a simple way: we will post-process the auto-import results to filter these away, based on `Context.active_global_builtins.` Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D70532319 fbshipit-source-id: f213d61f70e97ec7e94bf8fce5177f5d54932655
1 parent c721df6 commit 46881dd

File tree

16 files changed

+132
-2
lines changed

16 files changed

+132
-2
lines changed

src/services/autocomplete/autocompleteService_js.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ type typing = {
358358
canonical: Autocomplete_sigil.Canonical.token option;
359359
}
360360

361+
let search_with_filtered_auto_import_results cx =
362+
let is_available_autoimport_result = Lsp_import_edits.is_available_autoimport_result cx in
363+
fun f ~ac_options name ->
364+
let open Export_search_types in
365+
let { results; is_incomplete } = f ~ac_options name in
366+
let results =
367+
Base.List.filter results ~f:(fun { search_result = { name; source; _ }; _ } ->
368+
is_available_autoimport_result ~name ~source
369+
)
370+
in
371+
{ results; is_incomplete }
372+
361373
let mk_typing_artifacts
362374
~layout_options
363375
~loc_of_aloc
@@ -378,8 +390,8 @@ let mk_typing_artifacts
378390
loc_of_aloc;
379391
get_ast_from_shared_mem;
380392
module_system_info;
381-
search_exported_values;
382-
search_exported_types;
393+
search_exported_values = search_with_filtered_auto_import_results cx search_exported_values;
394+
search_exported_types = search_with_filtered_auto_import_results cx search_exported_types;
383395
cx;
384396
file_sig;
385397
ast;

src/services/code_action/code_action_service.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ let maybe_sort_by_usage ~imports_ranked_usage imports =
476476
imports
477477

478478
let suggest_imports
479+
~cx
479480
~layout_options
480481
~module_system_info
481482
~ast
@@ -504,8 +505,10 @@ let suggest_imports
504505
source = Some "Flow" && code = lsp_code && Lsp_helpers.ranges_overlap range error_range
505506
)
506507
in
508+
let is_available_autoimport_result = Lsp_import_edits.is_available_autoimport_result cx in
507509
files
508510
|> Export_index.ExportMap.bindings
511+
|> Base.List.filter ~f:(fun ((source, _), _) -> is_available_autoimport_result ~name ~source)
509512
|> maybe_sort_by_usage ~imports_ranked_usage
510513
|> Base.List.fold ~init:[] ~f:(fun acc ((source, export_kind), _num) ->
511514
match
@@ -1154,6 +1157,7 @@ let code_actions_of_errors
11541157
let actions =
11551158
if include_quick_fixes && Loc.intersects error_loc loc then
11561159
suggest_imports
1160+
~cx
11571161
~layout_options:(Code_action_utils.layout_options options)
11581162
~module_system_info
11591163
~ast
@@ -1632,6 +1636,7 @@ let suggest_imports_cli
16321636
when Options.autoimports options ->
16331637
let ranked_imports =
16341638
suggest_imports
1639+
~cx
16351640
~layout_options:(Code_action_utils.layout_options options)
16361641
~module_system_info
16371642
~ast

src/services/code_action/lsp_import_edits.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
open Code_action_text_edits
99
open Lsp_module_system_info
1010

11+
let is_available_autoimport_result cx =
12+
let (available_globals, available_modules) =
13+
let builtins = Context.active_global_builtins cx in
14+
(Builtins.builtin_ordinary_name_set builtins, Builtins.builtin_modules_set builtins)
15+
in
16+
fun ~name ~source ->
17+
let open Export_index in
18+
match source with
19+
| Global -> SSet.mem name available_globals
20+
| Builtin mref -> SSet.mem mref available_modules
21+
| File_key _ -> true
22+
1123
let main_of_package ~get_package_info package_dir =
1224
let file_key = File_key.JsonFile (package_dir ^ Filename.dir_sep ^ "package.json") in
1325
match get_package_info file_key with

src/services/code_action/lsp_import_edits.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*)
77

8+
val is_available_autoimport_result : Context.t -> name:string -> source:Export_index.source -> bool
9+
810
(** Generates the 'from' part of 'import ... from ...' required to import [source] from
911
a file in [src_dir] *)
1012
val from_of_source :

src/typing/builtins.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ let builtin_ordinary_name_set { original_global_values; original_global_types; _
2020
let add = SMap.fold (fun k _ acc -> SSet.add k acc) in
2121
SSet.empty |> add original_global_values |> add original_global_types
2222

23+
let builtin_modules_set { original_global_modules; _ } =
24+
SMap.fold (fun k _ acc -> SSet.add k acc) original_global_modules SSet.empty
25+
2326
let get_builtin_value_opt { original_global_values; type_mapper; mapped_global_names; _ } name =
2427
match Hashtbl.find_opt mapped_global_names name with
2528
| Some v -> Some v

src/typing/builtins.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type t
99

1010
val builtin_ordinary_name_set : t -> SSet.t
1111

12+
val builtin_modules_set : t -> SSet.t
13+
1214
val get_builtin_value_opt : t -> string -> (ALoc.t * Type.t) option
1315

1416
val get_builtin_type_opt : t -> string -> (ALoc.t * Type.t) option
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[libs]
2+
common_lib.js
3+
'<PROJECT_ROOT>/foo' -> 'foo_lib.js'
4+
'<PROJECT_ROOT>/bar' -> 'bar_lib.js'
5+
6+
[options]
7+
all=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shell: test.sh
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
common
2+
// ^
3+
fooS
4+
// ^
5+
barS
6+
// ^
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const commonValueWithOverride: boolean;
2+
declare const barSpecific: string;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const commonValueNoOverride: string;
2+
declare const commonValueWithOverride: string;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
common
2+
// ^
3+
fooS
4+
// ^
5+
barS
6+
// ^
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
common
2+
// ^
3+
fooS
4+
// ^
5+
barS
6+
// ^
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const commonValueWithOverride: number;
2+
declare const fooSpecific: string;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
common_test.js:1:7
2+
Flags: --pretty --imports
3+
{
4+
"result":[
5+
{"name":"commonValueNoOverride","type":"(global)"},
6+
{"name":"commonValueWithOverride","type":"(global)"}
7+
]
8+
}
9+
10+
common_test.js:3:5
11+
Flags: --pretty --imports
12+
{"result":[]}
13+
14+
common_test.js:5:5
15+
Flags: --pretty --imports
16+
{"result":[]}
17+
18+
foo/test.js:1:7
19+
Flags: --pretty --imports
20+
{
21+
"result":[
22+
{"name":"commonValueNoOverride","type":"(global)"},
23+
{"name":"commonValueWithOverride","type":"(global)"}
24+
]
25+
}
26+
27+
foo/test.js:3:5
28+
Flags: --pretty --imports
29+
{"result":[{"name":"fooSpecific","type":"(global)"}]}
30+
31+
foo/test.js:5:5
32+
Flags: --pretty --imports
33+
{"result":[]}
34+
35+
bar/test.js:1:7
36+
Flags: --pretty --imports
37+
{
38+
"result":[
39+
{"name":"commonValueNoOverride","type":"(global)"},
40+
{"name":"commonValueWithOverride","type":"(global)"}
41+
]
42+
}
43+
44+
bar/test.js:3:5
45+
Flags: --pretty --imports
46+
{"result":[]}
47+
48+
bar/test.js:5:5
49+
Flags: --pretty --imports
50+
{"result":[{"name":"barSpecific","type":"(global)"}]}
51+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# shellcheck disable=SC2094
8+
9+
queries_in_file autocomplete "common_test.js" --pretty --imports
10+
queries_in_file autocomplete "foo/test.js" --pretty --imports
11+
queries_in_file autocomplete "bar/test.js" --pretty --imports

0 commit comments

Comments
 (0)