Skip to content

Commit b15413a

Browse files
authored
analysis refactor: remove global state Shared_types.state (#8465)
* analysis refactor: remove global state `Shared_types.state` * remove state from package type * remove unused `state` param * rename `re_script` to `rescript` * Update CHANGELOG.md
1 parent 62ecbbc commit b15413a

26 files changed

Lines changed: 620 additions & 533 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- Catalog every named compiler error variant in `tests/ERROR_VARIANTS.md` and add fixtures for the remaining reachable ones. https://github.com/rescript-lang/rescript/pull/8446
6161
- Remove dead and unreachable compiler error and warning variants; add fixtures for the ones found to be reachable. https://github.com/rescript-lang/rescript/pull/8459
6262
- Convert OCaml codebase to snake case format. https://github.com/rescript-lang/rescript/pull/8456
63+
- Analysis refactor: remove global state `Shared_types.state`. https://github.com/rescript-lang/rescript/pull/8465
6364

6465

6566
# 13.0.0-alpha.4

analysis/bin/main.ml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Options:
9292
|}
9393

9494
let main () =
95+
let state = Shared_types.create_state () in
9596
let args = Array.to_list Sys.argv in
9697
let debug_level, args =
9798
match args with
@@ -118,7 +119,7 @@ let main () =
118119
| [_; "cache-project"; root_path] -> (
119120
Cfg.read_project_config_cache := false;
120121
let uri = Uri.from_path root_path in
121-
match Packages.get_package ~uri with
122+
match Packages.get_package ~state ~uri with
122123
| Some package -> Cache.cache_project package
123124
| None -> print_endline "\"ERR\"")
124125
| [_; "cache-delete"; root_path] -> (
@@ -136,18 +137,20 @@ let main () =
136137
print_header_info path line col;
137138
Cli.completion ~debug ~path
138139
~pos:(int_of_string line, int_of_string col)
139-
~current_file
140+
~current_file ~state
140141
| [_; "completionResolve"; path; module_path] ->
141-
Cli.completion_resolve ~path ~module_path
142+
Cli.completion_resolve ~state ~path ~module_path
142143
| [_; "definition"; path; line; col] ->
143-
Cli.definition ~path ~pos:(int_of_string line, int_of_string col) ~debug
144+
Cli.definition ~state ~path
145+
~pos:(int_of_string line, int_of_string col)
146+
~debug
144147
| [_; "typeDefinition"; path; line; col] ->
145-
Cli.type_definition ~path
148+
Cli.type_definition ~state ~path
146149
~pos:(int_of_string line, int_of_string col)
147150
~debug
148151
| [_; "documentSymbol"; path] -> Document_symbol.command ~path
149152
| [_; "hover"; path; line; col; current_file; supports_markdown_links] ->
150-
Cli.hover ~path
153+
Cli.hover ~state ~path
151154
~pos:(int_of_string line, int_of_string col)
152155
~current_file ~debug
153156
~supports_markdown_links:
@@ -163,22 +166,22 @@ let main () =
163166
current_file;
164167
allow_for_constructor_payloads;
165168
] ->
166-
Cli.signature_help ~path
169+
Cli.signature_help ~state ~path
167170
~pos:(int_of_string line, int_of_string col)
168171
~current_file ~debug
169172
~allow_for_constructor_payloads:
170173
(match allow_for_constructor_payloads with
171174
| "true" -> true
172175
| _ -> false)
173176
| [_; "inlayHint"; path; line_start; line_end; max_length] ->
174-
Cli.inlayhint ~path
177+
Cli.inlayhint ~state ~path
175178
~pos:(int_of_string line_start, int_of_string line_end)
176179
~max_length ~debug
177-
| [_; "codeLens"; path] -> Cli.code_lens ~path ~debug
180+
| [_; "codeLens"; path] -> Cli.code_lens ~state ~path ~debug
178181
| [
179182
_; "codeAction"; path; start_line; start_col; end_line; end_col; current_file;
180183
] ->
181-
Cli.code_action ~path
184+
Cli.code_action ~state ~path
182185
~start_pos:(int_of_string start_line, int_of_string start_col)
183186
~end_pos:(int_of_string end_line, int_of_string end_col)
184187
~current_file ~debug
@@ -197,23 +200,27 @@ let main () =
197200
|> print_endline
198201
| [_; "diagnosticSyntax"; path] -> Cli.diagnostic_syntax ~path
199202
| [_; "references"; path; line; col] ->
200-
Cli.references ~path ~pos:(int_of_string line, int_of_string col) ~debug
203+
Cli.references ~state ~path
204+
~pos:(int_of_string line, int_of_string col)
205+
~debug
201206
| [_; "prepareRename"; path; line; col] ->
202-
Cli.prepare_rename ~path ~pos:(int_of_string line, int_of_string col) ~debug
207+
Cli.prepare_rename ~state ~path
208+
~pos:(int_of_string line, int_of_string col)
209+
~debug
203210
| [_; "rename"; path; line; col; new_name] ->
204-
Cli.rename ~path
211+
Cli.rename ~state ~path
205212
~pos:(int_of_string line, int_of_string col)
206213
~new_name ~debug
207214
| [_; "semanticTokens"; current_file] ->
208215
Cli.semantic_tokens ~path:current_file
209216
| [_; "createInterface"; path; cmi_file] ->
210-
`String (Create_interface.command ~path ~cmi_file)
217+
`String (Create_interface.command ~state ~path ~cmi_file)
211218
|> Yojson.Safe.pretty_to_string ~std:true
212219
|> print_endline
213220
| [_; "format"; path] -> Cli.format ~path
214-
| [_; "test"; path] -> Cli.test ~path
221+
| [_; "test"; path] -> Cli.test ~state ~path
215222
| [_; "cmt"; rescript_json; cmt_path] ->
216-
Cmt_viewer.dump rescript_json cmt_path
223+
Cmt_viewer.dump ~state rescript_json cmt_path
217224
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
218225
| _ ->
219226
prerr_endline help;

analysis/reanalyze/src/paths.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let set_project_root_from_cwd () =
3535
| None -> run_config.project_root
3636
| Some s -> s)
3737

38-
let set_re_script_project_root = lazy (set_project_root_from_cwd ())
38+
let set_rescript_project_root = lazy (set_project_root_from_cwd ())
3939

4040
module Config = struct
4141
let read_suppress conf =

analysis/reanalyze/src/reanalyze.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ let collect_cmt_file_paths ~cmt_root : string list =
102102
in
103103
walk_sub_dirs ""
104104
| None ->
105-
Lazy.force Paths.set_re_script_project_root;
105+
Lazy.force Paths.set_rescript_project_root;
106106
(* Prefer explicit scan plan emitted by rewatch (v2 `.sourcedirs.json`).
107107
This supports monorepos without reanalyze-side package resolution. *)
108108
let scan_plan = Paths.read_cmt_scan () in

analysis/src/cli.ml

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,39 @@ let print_string json =
33
let print_null () = `Null |> print_string
44
let print_list l = `List l |> print_string
55

6-
let completion ~debug ~path ~pos ~current_file =
7-
let full = Cmt.load_full_cmt_from_path ~path in
6+
let completion ~state ~debug ~path ~pos ~current_file =
7+
let full = Cmt.load_full_cmt_from_path ~state ~path in
88
let kind_file = Files.classify_source_file current_file in
99
match Files.read_file current_file with
1010
| None | Some "" -> print_null ()
1111
| Some source ->
12-
Commands.completion ~debug ~source ~kind_file ~pos ~full
12+
Commands.completion ~state ~debug ~source ~kind_file ~pos ~full
1313
|> List.map (fun c -> Lsp.Types.CompletionItem.yojson_of_t c)
1414
|> print_list
1515

16-
let completion_resolve ~path ~module_path =
17-
let full = Cmt.load_full_cmt_from_path ~path in
18-
match Commands.completion_resolve ~full ~module_path with
16+
let completion_resolve ~state ~path ~module_path =
17+
let full = Cmt.load_full_cmt_from_path ~state ~path in
18+
match Commands.completion_resolve ~state ~full ~module_path with
1919
| None -> print_null ()
2020
| Some (`MarkupContent {value}) -> `String value |> print_string
2121

22-
let inlayhint ~path ~pos ~max_length ~debug =
23-
let full = Cmt.load_full_cmt_from_path ~path in
22+
let inlayhint ~state ~path ~pos ~max_length ~debug =
23+
let full = Cmt.load_full_cmt_from_path ~state ~path in
2424
let kind_file = Files.classify_source_file path in
2525
match Files.read_file path with
2626
| None -> print_null ()
2727
| Some source -> (
28-
match Hint.inlay ~source ~kind_file ~pos ~max_length ~full ~debug with
28+
match
29+
Hint.inlay ~source ~kind_file ~pos ~max_length ~full ~state ~debug
30+
with
2931
| Some hints ->
3032
hints
3133
|> List.map (fun h -> Lsp.Types.InlayHint.yojson_of_t h)
3234
|> print_list
3335
| None -> print_null ())
3436

35-
let code_lens ~path ~debug =
36-
let full = Cmt.load_full_cmt_from_path ~path in
37+
let code_lens ~state ~path ~debug =
38+
let full = Cmt.load_full_cmt_from_path ~state ~path in
3739
let kind_file = Files.classify_source_file path in
3840
match Files.read_file path with
3941
| None -> print_null ()
@@ -43,68 +45,68 @@ let code_lens ~path ~debug =
4345
lens |> List.map (fun l -> Lsp.Types.CodeLens.yojson_of_t l) |> print_list
4446
| None -> print_null ())
4547

46-
let hover ~path ~pos ~current_file ~debug ~supports_markdown_links =
47-
let full = Cmt.load_full_cmt_from_path ~path in
48+
let hover ~state ~path ~pos ~current_file ~debug ~supports_markdown_links =
49+
let full = Cmt.load_full_cmt_from_path ~state ~path in
4850
let kind_file = Files.classify_source_file current_file in
4951
match Files.read_file current_file with
5052
| None -> print_null ()
5153
| Some source -> (
5254
match
5355
Commands.hover ~source ~kind_file ~pos ~debug ~supports_markdown_links
54-
~full
56+
~state ~full
5557
with
5658
| Some value -> Lsp.Types.Hover.yojson_of_t value |> print_string
5759
| None -> print_null ())
5860

59-
let signature_help ~path ~pos ~current_file ~debug
61+
let signature_help ~state ~path ~pos ~current_file ~debug
6062
~allow_for_constructor_payloads =
61-
let full = Cmt.load_full_cmt_from_path ~path in
63+
let full = Cmt.load_full_cmt_from_path ~state ~path in
6264
let kind_file = Files.classify_source_file current_file in
6365
match Files.read_file current_file with
6466
| None -> print_null ()
6567
| Some source -> (
6668
match
67-
Signature_help.signature_help ~source ~kind_file ~pos
68-
~allow_for_constructor_payloads ~full ~debug
69+
Commands.signature_help ~source ~kind_file ~pos
70+
~allow_for_constructor_payloads ~full ~state ~debug
6971
with
7072
| None -> print_null ()
7173
| Some s -> Lsp.Types.SignatureHelp.yojson_of_t s |> print_string)
7274

73-
let code_action ~path ~start_pos ~end_pos ~current_file ~debug =
75+
let code_action ~state ~path ~start_pos ~end_pos ~current_file ~debug =
7476
let kind_file = Files.classify_source_file current_file in
7577
match Files.read_file current_file with
7678
| None -> print_null ()
7779
| Some source ->
78-
Xform.extract_code_actions ~path ~start_pos ~end_pos ~source ~kind_file
79-
~debug
80+
Xform.extract_code_actions ~state ~path ~start_pos ~end_pos ~source
81+
~kind_file ~debug
8082
|> List.map (fun c -> Lsp.Types.CodeAction.yojson_of_t c)
8183
|> print_list
8284

83-
let definition ~path ~pos ~debug =
84-
let full = Cmt.load_full_cmt_from_path ~path in
85+
let definition ~state ~path ~pos ~debug =
86+
let full = Cmt.load_full_cmt_from_path ~state ~path in
8587

86-
match Commands.definition ~full ~pos ~debug with
88+
match Commands.definition ~state ~full ~pos ~debug with
8789
| None -> print_null ()
8890
| Some location -> location |> Lsp.Types.Location.yojson_of_t |> print_string
8991

90-
let type_definition ~path ~pos ~debug =
91-
let full = Cmt.load_full_cmt_from_path ~path in
92-
match Commands.type_definition ~full ~pos ~debug with
92+
let type_definition ~state ~path ~pos ~debug =
93+
let full = Cmt.load_full_cmt_from_path ~state ~path in
94+
match Commands.type_definition ~state ~full ~pos ~debug with
9395
| None -> print_null ()
9496
| Some location -> location |> Lsp.Types.Location.yojson_of_t |> print_string
9597

96-
let references ~path ~pos ~debug =
97-
let full = Cmt.load_full_cmt_from_path ~path in
98-
let all_locs = Commands.references ~full ~pos ~debug in
98+
let references ~state ~path ~pos ~debug =
99+
let full = Cmt.load_full_cmt_from_path ~state ~path in
100+
let all_locs = Commands.references ~state ~full ~pos ~debug in
99101
if all_locs = [] then print_null ()
100102
else
101103
all_locs
102104
|> List.map (fun l -> Lsp.Types.Location.yojson_of_t l)
103105
|> print_list
104106

105-
let rename ~path ~pos ~new_name ~debug =
106-
let full = Cmt.load_full_cmt_from_path ~path in
107-
match Commands.rename ~full ~pos ~new_name ~debug with
107+
let rename ~state ~path ~pos ~new_name ~debug =
108+
let full = Cmt.load_full_cmt_from_path ~state ~path in
109+
match Commands.rename ~state ~full ~pos ~new_name ~debug with
108110
| Some {documentChanges = Some document_changes} ->
109111
document_changes
110112
|> List.map (fun c ->
@@ -116,8 +118,8 @@ let rename ~path ~pos ~new_name ~debug =
116118
|> print_list
117119
| _ -> print_null ()
118120

119-
let prepare_rename ~path ~pos ~debug =
120-
let full = Cmt.load_full_cmt_from_path ~path in
121+
let prepare_rename ~state ~path ~pos ~debug =
122+
let full = Cmt.load_full_cmt_from_path ~state ~path in
121123
match Commands.prepare_rename ~full ~pos ~debug with
122124
| None -> print_null ()
123125
| Some {range; placeholder = None} ->
@@ -159,7 +161,7 @@ let semantic_tokens ~path =
159161
let tokens = Semantic_tokens.semantic_tokens ~source ~kind_file in
160162
Lsp.Types.SemanticTokens.yojson_of_t tokens |> print_string
161163

162-
let test ~path =
164+
let test ~state ~path =
163165
Uri.strip_path := true;
164166
match Files.read_file path with
165167
| None -> assert false
@@ -224,19 +226,19 @@ let test ~path =
224226
print_endline
225227
("Definition " ^ path ^ " " ^ string_of_int line ^ ":"
226228
^ string_of_int col);
227-
definition ~path ~pos:(line, col) ~debug:true
229+
definition ~state ~path ~pos:(line, col) ~debug:true
228230
| "com" ->
229231
print_endline
230232
("Complete " ^ path ^ " " ^ string_of_int line ^ ":"
231233
^ string_of_int col);
232234
let current_file = create_current_file () in
233-
completion ~debug:true ~path ~pos:(line, col) ~current_file;
235+
completion ~state ~debug:true ~path ~pos:(line, col) ~current_file;
234236
Sys.remove current_file
235237
| "cre" ->
236238
let module_path = String.sub rest 3 (String.length rest - 3) in
237239
let module_path = String.trim module_path in
238240
print_endline ("Completion resolve: " ^ module_path);
239-
completion_resolve ~path ~module_path
241+
completion_resolve ~state ~path ~module_path
240242
| "dce" ->
241243
print_endline ("DCE " ^ path);
242244
Reanalyze.Run_config.run_config.suppress <- ["src"];
@@ -259,16 +261,16 @@ let test ~path =
259261
("Hover " ^ path ^ " " ^ string_of_int line ^ ":"
260262
^ string_of_int col);
261263
let current_file = create_current_file () in
262-
hover ~supports_markdown_links:true ~path ~pos:(line, col)
264+
hover ~state ~supports_markdown_links:true ~path ~pos:(line, col)
263265
~current_file ~debug:true;
264266
Sys.remove current_file
265267
| "she" ->
266268
print_endline
267269
("Signature help " ^ path ^ " " ^ string_of_int line ^ ":"
268270
^ string_of_int col);
269271
let current_file = create_current_file () in
270-
signature_help ~path ~pos:(line, col) ~current_file ~debug:true
271-
~allow_for_constructor_payloads:true;
272+
signature_help ~state ~path ~pos:(line, col) ~current_file
273+
~debug:true ~allow_for_constructor_payloads:true;
272274
Sys.remove current_file
273275
| "int" ->
274276
print_endline ("Create Interface " ^ path);
@@ -279,30 +281,30 @@ let test ~path =
279281
let dir = dirname path in
280282
dir ++ parent_dir_name ++ "lib" ++ "bs" ++ "src" ++ name
281283
in
282-
Printf.printf "%s" (Create_interface.command ~path ~cmi_file)
284+
Printf.printf "%s" (Create_interface.command ~state ~path ~cmi_file)
283285
| "ref" ->
284286
print_endline
285287
("References " ^ path ^ " " ^ string_of_int line ^ ":"
286288
^ string_of_int col);
287-
references ~path ~pos:(line, col) ~debug:true
289+
references ~state ~path ~pos:(line, col) ~debug:true
288290
| "pre" ->
289291
print_endline
290292
("PrepareRename " ^ path ^ " " ^ string_of_int line ^ ":"
291293
^ string_of_int col);
292-
prepare_rename ~path ~pos:(line, col) ~debug:true
294+
prepare_rename ~state ~path ~pos:(line, col) ~debug:true
293295
| "ren" ->
294296
let new_name = String.sub rest 4 (len - mlen - 4) in
295297
let () =
296298
print_endline
297299
("Rename " ^ path ^ " " ^ string_of_int line ^ ":"
298300
^ string_of_int col ^ " " ^ new_name)
299301
in
300-
rename ~path ~pos:(line, col) ~new_name ~debug:true
302+
rename ~state ~path ~pos:(line, col) ~new_name ~debug:true
301303
| "typ" ->
302304
print_endline
303305
("TypeDefinition " ^ path ^ " " ^ string_of_int line ^ ":"
304306
^ string_of_int col);
305-
type_definition ~path ~pos:(line, col) ~debug:true
307+
type_definition ~state ~path ~pos:(line, col) ~debug:true
306308
| "xfm" ->
307309
let current_file = create_current_file () in
308310
(* +2 is to ensure that the character ^ points to is what's considered the end of the selection. *)
@@ -323,8 +325,8 @@ let test ~path =
323325
in
324326
let kind_file = Files.classify_source_file current_file in
325327
let code_actions =
326-
Xform.extract_code_actions ~path ~start_pos ~end_pos ~source
327-
~kind_file ~debug:true
328+
Xform.extract_code_actions ~state ~path ~start_pos ~end_pos
329+
~source ~kind_file ~debug:true
328330
in
329331
Sys.remove current_file;
330332
code_actions
@@ -402,11 +404,11 @@ let test ~path =
402404
print_endline
403405
("Inlay Hint " ^ path ^ " " ^ string_of_int line_start ^ ":"
404406
^ string_of_int line_end);
405-
inlayhint ~path ~pos:(line_start, line_end) ~max_length:"25"
407+
inlayhint ~state ~path ~pos:(line_start, line_end) ~max_length:"25"
406408
~debug:false
407409
| "cle" ->
408410
print_endline ("Code Lens " ^ path);
409-
code_lens ~path ~debug:false
411+
code_lens ~state ~path ~debug:false
410412
| "ast" ->
411413
print_endline
412414
("Dump AST " ^ path ^ " " ^ string_of_int line ^ ":"

0 commit comments

Comments
 (0)