Skip to content

Commit f0636e0

Browse files
analysis: fix namespace parsing after Yojson migration (#8454)
* analysis: fix namespace parsing after Yojson migration * Changelog * Move yojson helpers to reanalyze and reuse them there * Some more tests
1 parent f390dfc commit f0636e0

9 files changed

Lines changed: 118 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- Rewatch: use a single timestamp per compile pass. https://github.com/rescript-lang/rescript/pull/8428
3232
- Fix rewatch warning replay after early compile errors. https://github.com/rescript-lang/rescript/pull/8408
3333
- Fix formatting of trailing comments before `=` in let bindings. https://github.com/rescript-lang/rescript/pull/8444
34+
- Fix analysis namespace parsing after the Yojson migration. https://github.com/rescript-lang/rescript/pull/8454
3435

3536
#### :memo: Documentation
3637

‎analysis/reanalyze/src/Paths.ml‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,14 @@ let readCmtScan () =
154154
get key json |> Option.to_list |> List.filter_map fn
155155
in
156156
let read_entry (json : Yojson.Safe.t) =
157-
let build_root =
158-
json |> get_fn "build_root" Yojson.Safe.Util.to_string_option
159-
in
157+
let build_root = json |> get_fn "build_root" YojsonHelpers.string_opt in
160158
let scan_dirs =
161159
match json |> get "scan_dirs" with
162-
| Some (`List arr) ->
163-
arr |> List.filter_map Yojson.Safe.Util.to_string_option
160+
| Some (`List arr) -> arr |> List.filter_map YojsonHelpers.string_opt
164161
| _ -> []
165162
in
166163
let also_scan_build_root =
167-
match
168-
json |> get_fn "also_scan_build_root" Yojson.Safe.Util.to_bool_option
169-
with
164+
match json |> get_fn "also_scan_build_root" YojsonHelpers.bool_opt with
170165
| [b] -> b
171166
| _ -> false
172167
in

‎analysis/reanalyze/src/Reanalyze.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,4 @@ module ReanalyzeServer = ReanalyzeServer
776776
module RunConfig = RunConfig
777777
module DceConfig = DceConfig
778778
module Log_ = Log_
779+
module YojsonHelpers = YojsonHelpers
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
let get key (t : Yojson.Safe.t) : Yojson.Safe.t option =
2+
match t with
3+
| `Assoc items -> List.assoc_opt key items
4+
| _ -> None
5+
6+
let bool_opt = function
7+
| `Bool value -> Some value
8+
| _ -> None
9+
10+
let string_opt = function
11+
| `String value -> Some value
12+
| _ -> None
13+
14+
let to_list_opt = function
15+
| `List items -> Some items
16+
| _ -> None
17+
18+
let from_string_opt text =
19+
try Some (Yojson.Safe.from_string text) with _ -> None

‎analysis/src/FindFiles.ml‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ let getSourceDirectories ~includeDev ~baseDir config =
1111
| `Assoc _ -> (
1212
let dir =
1313
item |> YojsonHelpers.get "dir"
14-
|> bind Yojson.Safe.Util.to_string_option
14+
|> bind YojsonHelpers.string_opt
1515
|> Option.value ~default:"Must specify directory"
1616
in
1717
let typ =
1818
if includeDev then "lib"
1919
else
2020
item |> YojsonHelpers.get "type"
21-
|> bind Yojson.Safe.Util.to_string_option
21+
|> bind YojsonHelpers.string_opt
2222
|> Option.value ~default:"lib"
2323
in
2424

@@ -94,22 +94,27 @@ let nameSpaceToName n =
9494
|> List.map String.capitalize_ascii
9595
|> String.concat ""
9696

97+
type namespace_config =
98+
| NamespaceDisabled
99+
| NamespaceFromPackageName
100+
| NamespaceExplicit of string
101+
102+
let getNamespaceConfig config =
103+
match config |> YojsonHelpers.get "namespace" with
104+
| None | Some (`Bool false) -> NamespaceDisabled
105+
| Some (`Bool true) -> NamespaceFromPackageName
106+
| Some (`String namespace) -> NamespaceExplicit namespace
107+
| Some _ -> NamespaceDisabled
108+
97109
let getNamespace config =
98-
let ns = config |> YojsonHelpers.get "namespace" in
99-
let fromString = ns |> bind Yojson.Safe.Util.to_string_option in
100-
let isNamespaced =
101-
ns
102-
|> bind Yojson.Safe.Util.to_bool_option
103-
|> Option.value ~default:(fromString |> Option.is_some)
104-
in
105-
let either x y = if x = None then y else x in
106-
if isNamespaced then
110+
match getNamespaceConfig config with
111+
| NamespaceDisabled -> None
112+
| NamespaceExplicit namespace -> Some (nameSpaceToName namespace)
113+
| NamespaceFromPackageName ->
107114
let fromName =
108-
config |> YojsonHelpers.get "name"
109-
|> bind Yojson.Safe.Util.to_string_option
115+
config |> YojsonHelpers.get "name" |> bind YojsonHelpers.string_opt
110116
in
111-
either fromString fromName |> Option.map nameSpaceToName
112-
else None
117+
fromName |> Option.map nameSpaceToName
113118

114119
module StringSet = Set.Make (String)
115120

@@ -122,9 +127,8 @@ let getPublic config =
122127
| None -> None
123128
| Some public ->
124129
Some
125-
(public
126-
|> List.filter_map Yojson.Safe.Util.to_string_option
127-
|> StringSet.of_list))
130+
(public |> List.filter_map YojsonHelpers.string_opt |> StringSet.of_list)
131+
)
128132

129133
let collectFiles directory =
130134
let allFiles = Files.readDirectory directory in
@@ -262,7 +266,7 @@ let findDependencyFiles base config =
262266
with
263267
| None, None -> []
264268
| Some deps, None | _, Some deps ->
265-
deps |> List.filter_map Yojson.Safe.Util.to_string_option
269+
deps |> List.filter_map YojsonHelpers.string_opt
266270
in
267271
let devDeps =
268272
match
@@ -275,7 +279,7 @@ let findDependencyFiles base config =
275279
with
276280
| None, None -> []
277281
| Some devDeps, None | _, Some devDeps ->
278-
devDeps |> List.filter_map (fun x -> Some (Yojson.Safe.Util.to_string x))
282+
devDeps |> List.filter_map YojsonHelpers.string_opt
279283
in
280284
let deps = deps @ devDeps in
281285
Log.log ("Dependencies: " ^ String.concat " " deps);

‎analysis/src/Packages.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ let newBsPackage ~rootPath =
151151
let opens_from_compiler_flags =
152152
List.fold_left
153153
(fun opens item ->
154-
match item |> Yojson.Safe.Util.to_string_option with
154+
match item |> YojsonHelpers.string_opt with
155155
| None -> opens
156156
| Some s -> (
157157
let parts = String.split_on_char ' ' s in

‎analysis/src/YojsonHelpers.ml‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
let get key (t : Yojson.Safe.t) : Yojson.Safe.t option =
2-
match t with
3-
| `Assoc items -> List.assoc_opt key items
4-
| _ -> None
5-
6-
let to_list_opt json = try Some (Yojson.Safe.Util.to_list json) with _ -> None
7-
8-
let from_string_opt text =
9-
try Some (Yojson.Safe.from_string text) with _ -> None
1+
include Reanalyze.YojsonHelpers
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: ))
2+
3+
let json raw =
4+
match Yojson.Safe.from_string raw with
5+
| json -> json
6+
| exception Yojson.Json_error message -> failwith message
7+
8+
let assert_namespace ~expected raw =
9+
OUnit.assert_equal
10+
~printer:(function
11+
| Some s -> "Some " ^ s
12+
| None -> "None")
13+
expected
14+
(Analysis.FindFiles.getNamespace (json raw))
15+
16+
let assert_string_opt ~expected actual =
17+
OUnit.assert_equal
18+
~printer:(function
19+
| Some s -> "Some " ^ s
20+
| None -> "None")
21+
expected actual
22+
23+
let assert_bool_opt ~expected actual =
24+
OUnit.assert_equal
25+
~printer:(function
26+
| Some b -> "Some " ^ string_of_bool b
27+
| None -> "None")
28+
expected actual
29+
30+
let suites =
31+
__FILE__
32+
>::: [
33+
( "yojson helpers do not raise on type mismatch" >:: fun _ ->
34+
assert_string_opt ~expected:(Some "value")
35+
(Analysis.YojsonHelpers.string_opt (`String "value"));
36+
assert_string_opt ~expected:None
37+
(Analysis.YojsonHelpers.string_opt (`Bool true));
38+
assert_bool_opt ~expected:(Some true)
39+
(Analysis.YojsonHelpers.bool_opt (`Bool true));
40+
assert_bool_opt ~expected:None
41+
(Analysis.YojsonHelpers.bool_opt (`String "true"));
42+
OUnit.assert_equal ~printer:string_of_int 1
43+
(Analysis.YojsonHelpers.to_list_opt (`List [`Null])
44+
|> Option.fold ~none:0 ~some:List.length);
45+
OUnit.assert_equal ~printer:string_of_int 0
46+
(Analysis.YojsonHelpers.to_list_opt (`String "not a list")
47+
|> Option.fold ~none:0 ~some:List.length);
48+
OUnit.assert_bool "valid JSON parses"
49+
(Analysis.YojsonHelpers.from_string_opt {|{"ok": true}|}
50+
|> Option.is_some);
51+
OUnit.assert_bool "invalid JSON is ignored"
52+
(Analysis.YojsonHelpers.from_string_opt {|{|} |> Option.is_none) );
53+
( "absent namespace is disabled" >:: fun _ ->
54+
assert_namespace ~expected:None {|{"name": "@tests/pkg"}|} );
55+
( "false namespace is disabled" >:: fun _ ->
56+
assert_namespace ~expected:None
57+
{|{"name": "@tests/pkg", "namespace": false}|} );
58+
( "non-string non-bool namespace is disabled" >:: fun _ ->
59+
assert_namespace ~expected:None
60+
{|{"name": "@tests/pkg", "namespace": 1}|} );
61+
( "true namespace uses package name" >:: fun _ ->
62+
assert_namespace ~expected:(Some "TestsNamespacedReferences")
63+
{|{"name": "@tests/namespaced-references", "namespace": true}|} );
64+
( "string namespace uses explicit value" >:: fun _ ->
65+
assert_namespace ~expected:(Some "MyNamespace")
66+
{|{"name": "@tests/pkg", "namespace": "my-namespace"}|} );
67+
]

‎tests/ounit_tests/ounit_tests_main.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let suites =
1919
Ounit_util_tests.suites;
2020
Ounit_js_analyzer_tests.suites;
2121
Ounit_jsx_loc_tests.suites;
22+
Ounit_analysis_config_tests.suites;
2223
]
2324

2425
let _ = OUnit.run_test_tt_main suites

0 commit comments

Comments
 (0)