Skip to content

Commit 505ba07

Browse files
authored
Small cleanup (#145)
* Small cleanup * Format
1 parent a0fbf01 commit 505ba07

11 files changed

+164
-195
lines changed

analysis/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ src/NewCompletions.cmx : src/Utils.cmx src/Uri2.cmx src/TopTypes.cmx \
1919
src/Packages.cmx : src/Uri2.cmx src/TopTypes.cmx src/SharedTypes.cmx \
2020
src/Log.cmx src/vendor/Json.cmx src/Infix.cmx src/FindFiles.cmx \
2121
src/Files.cmx src/BuildSystem.cmx
22-
src/PartialParser.cmx : src/SharedTypes.cmx src/Infix.cmx
22+
src/PartialParser.cmx : src/SharedTypes.cmx
2323
src/PrepareUtils.cmx :
2424
src/PrintType.cmx : src/vendor/res_outcome_printer/res_outcome_printer.cmx \
2525
src/vendor/res_outcome_printer/res_doc.cmx

analysis/src/Files.ml

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ let removeExtraDots path =
66

77
(* Win32 & MacOS are case-insensitive *)
88
let pathEq =
9-
match Sys.os_type = "Linux" with
10-
| true -> fun a b -> a = b
11-
| false -> fun a b -> String.lowercase_ascii a = String.lowercase_ascii b
9+
if Sys.os_type = "Linux" then fun a b -> a = b
10+
else fun a b -> String.lowercase_ascii a = String.lowercase_ascii b
1211

1312
let pathStartsWith text prefix =
1413
String.length prefix <= String.length text
@@ -38,9 +37,7 @@ let relpath base path =
3837
loop (split Filename.dir_sep base) (split Filename.dir_sep path)
3938
in
4039
String.concat Filename.dir_sep
41-
((match base = [] with
42-
| true -> ["."]
43-
| false -> List.map (fun _ -> "..") base)
40+
((match base with [] -> ["."] | _ -> List.map (fun _ -> "..") base)
4441
@ path)
4542
|> removeExtraDots
4643

@@ -61,7 +58,7 @@ let readFile ~filename =
6158

6259
let exists path = match maybeStat path with None -> false | Some _ -> true
6360

64-
let ifExists path = match exists path with true -> Some path | false -> None
61+
let ifExists path = if exists path then Some path else None
6562

6663
let readDirectory dir =
6764
match Unix.opendir dir with
@@ -99,7 +96,7 @@ let rec collect ?(checkDir = fun _ -> true) path test =
9996
collect ~checkDir (Filename.concat path name) test)
10097
|> List.concat
10198
else []
102-
| _ -> ( match test path with true -> [path] | false -> [])
99+
| _ -> if test path then [path] else []
103100
104101
let fileConcat a b =
105102
if

analysis/src/FindFiles.ml

+88-92
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ let getSourceDirectories ~includeDev base config =
1616
Json.get "dir" item |?> Json.string |? "Must specify directory"
1717
in
1818
let typ =
19-
match includeDev with
20-
| true -> "lib"
21-
| false -> item |> Json.get "type" |?> Json.string |? "lib"
19+
if includeDev then "lib"
20+
else item |> Json.get "type" |?> Json.string |? "lib"
2221
in
2322
if typ = "dev" then []
2423
else
@@ -29,10 +28,12 @@ let getSourceDirectories ~includeDev base config =
2928
(* |> ifDebug(true, "Subdirs", String.concat(" - ")) *)
3029
|> List.filter (fun name -> name <> Filename.current_dir_name)
3130
|> List.map (Files.relpath base)
32-
| Some item -> (current /+ dir) :: handleItem (current /+ dir) item )
31+
| Some item -> (current /+ dir) :: handleItem (current /+ dir) item)
3332
| _ -> failwith "Invalid subdirs entry"
3433
in
35-
config |> Json.get "sources" |?>> handleItem "" |? []
34+
match config |> Json.get "sources" with
35+
| None -> []
36+
| Some item -> handleItem "" item
3637

3738
let isCompiledFile name =
3839
Filename.check_suffix name ".cmt" || Filename.check_suffix name ".cmti"
@@ -73,10 +74,10 @@ let filterDuplicates cmts =
7374
cmts
7475
|> List.filter (fun path ->
7576
not
76-
( ( Filename.check_suffix path ".re"
77-
|| Filename.check_suffix path ".ml"
78-
|| Filename.check_suffix path ".cmt" )
79-
&& Hashtbl.mem intfs (getName path) ))
77+
((Filename.check_suffix path ".re"
78+
|| Filename.check_suffix path ".ml"
79+
|| Filename.check_suffix path ".cmt")
80+
&& Hashtbl.mem intfs (getName path)))
8081

8182
let nameSpaceToName n =
8283
n
@@ -89,13 +90,12 @@ let getNamespace config =
8990
let isNamespaced =
9091
ns |?> Json.bool |? (ns |?> Json.string |?> (fun _ -> Some true) |? false)
9192
in
92-
match isNamespaced with
93-
| true ->
93+
if isNamespaced then
9494
ns |?> Json.string
9595
|?? (Json.get "name" config |?> Json.string)
9696
|! "name is required if namespace is true" |> nameSpaceToName
9797
|> fun s -> Some s
98-
| false -> None
98+
else None
9999

100100
let collectFiles directory =
101101
let allFiles = Files.readDirectory directory in
@@ -108,9 +108,7 @@ let collectFiles directory =
108108
let source =
109109
Utils.find
110110
(fun name ->
111-
match getName name = modName with
112-
| true -> Some (directory /+ name)
113-
| false -> None)
111+
if getName name = modName then Some (directory /+ name) else None)
114112
sources
115113
in
116114
(modName, SharedTypes.Impl (compiled, source)))
@@ -148,45 +146,44 @@ let findProjectFiles ~debug namespace root sourceDirectories compiledBase =
148146
|| Filename.check_suffix path ".mli"
149147
then (
150148
Log.log ("Adding intf " ^ path);
151-
Hashtbl.replace interfaces (getName path) path ));
149+
Hashtbl.replace interfaces (getName path) path));
152150
let normals =
153151
files
154152
|> Utils.filterMap (fun path ->
155-
if
156-
Filename.check_suffix path ".re"
157-
|| Filename.check_suffix path ".res"
158-
|| Filename.check_suffix path ".ml"
159-
then (
160-
let mname = getName path in
161-
let intf = Hashtbl.find_opt interfaces mname in
162-
Hashtbl.remove interfaces mname;
163-
let base = compiledBaseName ~namespace (Files.relpath root path) in
164-
match intf with
165-
| Some intf ->
166-
let cmti = (compiledBase /+ base) ^ ".cmti" in
167-
let cmt = (compiledBase /+ base) ^ ".cmt" in
168-
if Files.exists cmti then
169-
if Files.exists cmt then
170-
(* Log.log("Intf and impl " ++ cmti ++ " " ++ cmt) *)
171-
Some (mname, SharedTypes.IntfAndImpl (cmti, intf, cmt, path))
172-
else Some (mname, Intf (cmti, intf))
173-
else (
174-
(* Log.log("Just intf " ++ cmti) *)
175-
Log.log ("Bad source file (no cmt/cmti/cmi) " ^ (compiledBase /+ base));
176-
None
177-
)
178-
| None ->
179-
let cmt = (compiledBase /+ base) ^ ".cmt" in
180-
if Files.exists cmt then Some (mname, Impl (cmt, Some path))
181-
else (
182-
Log.log ("Bad source file (no cmt/cmi) " ^ (compiledBase /+ base));
183-
None
184-
)
185-
) else (
186-
Log.log ("Bad source file (extension) " ^ path);
187-
None
188-
)
189-
)
153+
if
154+
Filename.check_suffix path ".re"
155+
|| Filename.check_suffix path ".res"
156+
|| Filename.check_suffix path ".ml"
157+
then (
158+
let mname = getName path in
159+
let intf = Hashtbl.find_opt interfaces mname in
160+
Hashtbl.remove interfaces mname;
161+
let base = compiledBaseName ~namespace (Files.relpath root path) in
162+
match intf with
163+
| Some intf ->
164+
let cmti = (compiledBase /+ base) ^ ".cmti" in
165+
let cmt = (compiledBase /+ base) ^ ".cmt" in
166+
if Files.exists cmti then
167+
if Files.exists cmt then
168+
(* Log.log("Intf and impl " ++ cmti ++ " " ++ cmt) *)
169+
Some (mname, SharedTypes.IntfAndImpl (cmti, intf, cmt, path))
170+
else Some (mname, Intf (cmti, intf))
171+
else (
172+
(* Log.log("Just intf " ++ cmti) *)
173+
Log.log
174+
("Bad source file (no cmt/cmti/cmi) " ^ (compiledBase /+ base)
175+
);
176+
None)
177+
| None ->
178+
let cmt = (compiledBase /+ base) ^ ".cmt" in
179+
if Files.exists cmt then Some (mname, Impl (cmt, Some path))
180+
else (
181+
Log.log
182+
("Bad source file (no cmt/cmi) " ^ (compiledBase /+ base));
183+
None))
184+
else (
185+
Log.log ("Bad source file (extension) " ^ path);
186+
None))
190187
in
191188
let result =
192189
List.append normals
@@ -200,9 +197,9 @@ let findProjectFiles ~debug namespace root sourceDirectories compiledBase =
200197
else res)
201198
interfaces [])
202199
|> List.map (fun (name, paths) ->
203-
match namespace with
204-
| None -> (name, paths)
205-
| Some namespace -> (name ^ "-" ^ namespace, paths))
200+
match namespace with
201+
| None -> (name, paths)
202+
| Some namespace -> (name ^ "-" ^ namespace, paths))
206203
in
207204
match namespace with
208205
| None -> result
@@ -234,35 +231,35 @@ let findDependencyFiles ~debug base config =
234231
let depFiles =
235232
deps
236233
|> List.map (fun name ->
237-
let result =
238-
ModuleResolution.resolveNodeModulePath ~startPath:base name
239-
|?> fun loc ->
240-
let innerPath = loc /+ "bsconfig.json" in
241-
Log.log ("Dep loc " ^ innerPath);
242-
match Files.readFile innerPath with
243-
| Some text -> (
244-
let inner = Json.parse text in
245-
let namespace = getNamespace inner in
246-
let directories =
247-
getSourceDirectories ~includeDev:false loc inner
248-
in
249-
match BuildSystem.getCompiledBase loc with
250-
| None -> None
251-
| Some compiledBase ->
252-
if debug then Log.log ("Compiled base: " ^ compiledBase);
253-
let compiledDirectories =
254-
directories |> List.map (Files.fileConcat compiledBase)
255-
in
256-
let compiledDirectories =
257-
match namespace = None with
258-
| true -> compiledDirectories
259-
| false -> compiledBase :: compiledDirectories
260-
in
261-
let files =
262-
findProjectFiles ~debug namespace loc directories
263-
compiledBase
264-
in
265-
(*
234+
let result =
235+
ModuleResolution.resolveNodeModulePath ~startPath:base name
236+
|?> fun loc ->
237+
let innerPath = loc /+ "bsconfig.json" in
238+
Log.log ("Dep loc " ^ innerPath);
239+
match Files.readFile innerPath with
240+
| Some text -> (
241+
let inner = Json.parse text in
242+
let namespace = getNamespace inner in
243+
let directories =
244+
getSourceDirectories ~includeDev:false loc inner
245+
in
246+
match BuildSystem.getCompiledBase loc with
247+
| None -> None
248+
| Some compiledBase ->
249+
if debug then Log.log ("Compiled base: " ^ compiledBase);
250+
let compiledDirectories =
251+
directories |> List.map (Files.fileConcat compiledBase)
252+
in
253+
let compiledDirectories =
254+
match namespace with
255+
| None -> compiledDirectories
256+
| Some _ -> compiledBase :: compiledDirectories
257+
in
258+
let files =
259+
findProjectFiles ~debug namespace loc directories
260+
compiledBase
261+
in
262+
(*
266263
let files = switch (namespace) {
267264
| None =>
268265
files
@@ -273,15 +270,14 @@ let findDependencyFiles ~debug base config =
273270
)
274271
};
275272
*)
276-
Some (compiledDirectories, files) )
277-
| None -> None
278-
in
279-
match result with
280-
| Some dependency -> dependency
281-
| None ->
282-
Log.log ("Skipping nonexistent dependency: " ^ name);
283-
([], [])
284-
)
273+
Some (compiledDirectories, files))
274+
| None -> None
275+
in
276+
match result with
277+
| Some dependency -> dependency
278+
| None ->
279+
Log.log ("Skipping nonexistent dependency: " ^ name);
280+
([], []))
285281
in
286282
let directories, files = List.split depFiles in
287283
let files = List.concat files in

analysis/src/ModuleResolution.ml

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ open Infix
22

33
let rec resolveNodeModulePath ~startPath name =
44
let path = startPath /+ "node_modules" /+ name in
5-
match startPath with
6-
| "/" -> ( match Files.exists path with true -> Some path | false -> None )
7-
| _ -> (
8-
match Files.exists path with
9-
| true -> Some path
10-
| false ->
11-
resolveNodeModulePath ~startPath:(Filename.dirname startPath) name )
5+
if Files.exists path then Some path
6+
else if startPath = "/" then None
7+
else resolveNodeModulePath ~startPath:(Filename.dirname startPath) name

analysis/src/NewCompletions.ml

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
open SharedTypes
22

33
let showConstructor {cname = {txt}; args; res} =
4-
let open Infix in
54
txt
6-
^ (match args = [] with
7-
| true -> ""
8-
| false ->
5+
^ (match args with
6+
| [] -> ""
7+
| _ ->
98
"("
10-
^ String.concat ", "
11-
(args |> List.map (fun (typ, _) -> typ |> Shared.typeToString))
9+
^ (args
10+
|> List.map (fun (typ, _) -> typ |> Shared.typeToString)
11+
|> String.concat ", ")
1212
^ ")")
13-
^ (res |?>> (fun typ -> "\n" ^ (typ |> Shared.typeToString)) |? "")
13+
^ match res with None -> "" | Some typ -> "\n" ^ (typ |> Shared.typeToString)
1414

1515
(* TODO: local opens *)
1616
let resolveOpens ~env ~previous opens ~package =
@@ -229,7 +229,7 @@ let valueCompletions ~(env : ProcessCmt.queryEnv) suffix =
229229
(* Get rid of lowercase modules (#417) *)
230230
env.qExported.modules
231231
|> Hashtbl.filter_map_inplace (fun name key ->
232-
match isCapitalized name with true -> Some key | false -> None);
232+
if isCapitalized name then Some key else None);
233233
let moduleCompletions =
234234
completionForExporteds env.qExported.modules env.qFile.stamps.modules
235235
suffix (fun m -> Module m)
@@ -416,7 +416,7 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring =
416416
match docstring with [] -> "" | _ :: _ -> docstring |> String.concat "\n"
417417
in
418418
let tags =
419-
match deprecated = None with true -> [] | false -> [1 (* deprecated *)]
419+
match deprecated with None -> [] | Some _ -> [1 (* deprecated *)]
420420
in
421421
Protocol.
422422
{
@@ -553,9 +553,8 @@ let processCompletable ~findItems ~package ~rawOpens
553553
|> String.concat "."
554554
in
555555
let completionName name =
556-
match modulePathMinusOpens = "" with
557-
| true -> name
558-
| false -> modulePathMinusOpens ^ "." ^ name
556+
if modulePathMinusOpens = "" then name
557+
else modulePathMinusOpens ^ "." ^ name
559558
in
560559
let parts = modulePath @ [partialName] in
561560
let items = parts |> findItems ~exact:false in

0 commit comments

Comments
 (0)