Skip to content

Commit 868d085

Browse files
committed
refactor : meaningful names for js_cmj_format
1 parent d7a9e1c commit 868d085

25 files changed

+2654
-2673
lines changed

jscomp/core/js_cmj_format.ml

+8-71
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ type effect = string option
4242

4343

4444
let single_na = Single Lam_arity.na
45-
(** we don't force people to use package *)
46-
type cmj_case = Ext_namespace.file_kind
4745

4846
type keyed_cmj_value = { name : string ; arity : arity ; persistent_closed_lambda : Lam.t option}
4947
type keyed_cmj_values
@@ -52,20 +50,20 @@ type keyed_cmj_values
5250
type t = {
5351
values : keyed_cmj_values ;
5452
pure : bool;
55-
npm_package_path : Js_packages_info.t ;
56-
cmj_case : cmj_case;
53+
package_spec : Js_packages_info.t ;
54+
js_file_kind : Ext_js_file_kind.t;
5755
}
5856

59-
let make ~(values:cmj_value Map_string.t) ~effect ~npm_package_path ~cmj_case : t =
57+
let make ~(values:cmj_value Map_string.t) ~effect ~package_spec ~js_file_kind : t =
6058
{
6159
values = Map_string.to_sorted_array_with_f values (fun k v -> {
6260
name = k ;
6361
arity = v.arity;
6462
persistent_closed_lambda = v.persistent_closed_lambda
6563
});
6664
pure = effect = None ;
67-
npm_package_path;
68-
cmj_case
65+
package_spec;
66+
js_file_kind
6967
}
7068

7169

@@ -178,70 +176,9 @@ let query_by_name (cmj_table : t ) name : keyed_cmj_value =
178176
let values = cmj_table.values in
179177
binarySearch values name
180178

181-
let is_pure (cmj_table : t ) =
182-
cmj_table.pure
183-
184-
let get_npm_package_path (cmj_table : t) =
185-
cmj_table.npm_package_path
186-
187-
let get_cmj_case (cmj_table : t) =
188-
cmj_table.cmj_case
189-
190-
191-
(* start dumping *)
192-
193-
let f fmt = Printf.fprintf stdout fmt
194-
195-
let pp_cmj_case (cmj_case : cmj_case) : unit =
196-
match cmj_case with
197-
| Little_js ->
198-
f "case : little, .js \n"
199-
| Little_bs ->
200-
f "case : little, .bs.js \n"
201-
| Upper_js ->
202-
f "case: upper, .js \n"
203-
| Upper_bs ->
204-
f "case: upper, .bs.js \n"
205-
206-
let pp_cmj
207-
({ values ; pure; npm_package_path ; cmj_case} : t) =
208-
f "package info: %s\n"
209-
(Format.asprintf "%a" Js_packages_info.dump_packages_info npm_package_path)
210-
;
211-
pp_cmj_case cmj_case;
212-
213-
f "effect: %s\n"
214-
(if pure then "pure" else "not pure");
215-
Ext_array.iter values
216-
(fun ({name = k ; arity; persistent_closed_lambda}) ->
217-
match arity with
218-
| Single arity ->
219-
f "%s: %s\n" k (Format.asprintf "%a" Lam_arity.print arity);
220-
(match persistent_closed_lambda with
221-
| None ->
222-
f "%s: not saved\n" k
223-
| Some lam ->
224-
begin
225-
f "%s: ======[start]\n" k ;
226-
f "%s\n" (Lam_print.lambda_to_string lam);
227-
f "%s: ======[finish]\n" k
228-
end )
229-
| Submodule xs ->
230-
(match persistent_closed_lambda with
231-
| None -> f "%s: not saved\n" k
232-
| Some lam ->
233-
begin
234-
f "%s: ======[start]\n" k ;
235-
f "%s" (Lam_print.lambda_to_string lam);
236-
f "%s: ======[finish]\n" k
237-
end
238-
);
239-
Array.iteri
240-
(fun i arity -> f "%s[%i] : %s \n"
241-
k i
242-
(Format.asprintf "%a" Lam_arity.print arity ))
243-
xs
244-
)
179+
180+
181+
245182

246183
type path = string
247184
type cmj_load_info = {

jscomp/core/js_cmj_format.mli

+14-19
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,34 @@ type cmj_value = {
6464

6565
type effect = string option
6666

67-
type cmj_case = Ext_namespace.file_kind
67+
type keyed_cmj_value = {
68+
name : string ;
69+
arity : arity ;
70+
persistent_closed_lambda : Lam.t option
71+
}
6872

69-
type t
73+
type t = private {
74+
values : keyed_cmj_value array ;
75+
pure : bool;
76+
package_spec : Js_packages_info.t ;
77+
js_file_kind : Ext_js_file_kind.t;
78+
}
7079

7180

7281
val make:
7382
values: cmj_value Map_string.t ->
7483
effect: effect ->
75-
npm_package_path: Js_packages_info.t ->
76-
cmj_case:cmj_case ->
84+
package_spec: Js_packages_info.t ->
85+
js_file_kind:Ext_js_file_kind.t ->
7786
t
7887

79-
type keyed_cmj_value = {
80-
name : string ;
81-
arity : arity ;
82-
persistent_closed_lambda : Lam.t option
83-
}
8488

8589
val query_by_name :
8690
t ->
8791
string ->
8892
keyed_cmj_value
8993

90-
val is_pure :
91-
t -> bool
9294

93-
val get_npm_package_path :
94-
t ->
95-
Js_packages_info.t
96-
97-
val get_cmj_case :
98-
t ->
99-
cmj_case
10095

10196
val single_na : arity
10297

@@ -115,7 +110,7 @@ val from_string : string -> t
115110
val to_file :
116111
string -> check_exists:bool -> t -> unit
117112

118-
val pp_cmj: t -> unit
113+
119114

120115

121116
type path = string

jscomp/core/lam_compile_env.ml

+26-27
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,31 @@ let query_external_id_info (module_id : Ident.t) (name : string) : ident_info =
129129

130130

131131

132-
let get_package_path_from_cmj
133-
( id : Lam_module_ident.t)
132+
let get_package_path_from_cmj ( id : Lam_module_ident.t)
134133
=
135-
match Lam_module_ident.Hash.find_opt cached_tbl id with
136-
| Some (Ml {cmj_table ; package_path}) ->
137-
(package_path,
138-
Js_cmj_format.get_npm_package_path cmj_table,
139-
Js_cmj_format.get_cmj_case cmj_table )
140-
| Some External ->
141-
assert false
142-
(* called by {!Js_name_of_module_id.string_of_module_id}
134+
let cmj_load_info =
135+
match Lam_module_ident.Hash.find_opt cached_tbl id with
136+
| Some (Ml cmj_load_info) -> cmj_load_info
137+
| Some External ->
138+
assert false
139+
(* called by {!Js_name_of_module_id.string_of_module_id}
143140
can not be External
144-
*)
145-
| None ->
146-
begin match id.kind with
147-
| Runtime
148-
| External _ -> assert false
149-
| Ml ->
150-
let cmj_load_info =
151-
Js_cmj_load.load_unit_exn (Lam_module_ident.name id) in
152-
let cmj_table = cmj_load_info.cmj_table in
153-
id +> Ml cmj_load_info;
154-
(cmj_load_info.package_path,
155-
Js_cmj_format.get_npm_package_path cmj_table,
156-
Js_cmj_format.get_cmj_case cmj_table )
157-
end
141+
*)
142+
| None ->
143+
begin match id.kind with
144+
| Runtime
145+
| External _ -> assert false
146+
| Ml ->
147+
let cmj_load_info =
148+
Js_cmj_load.load_unit_exn (Lam_module_ident.name id) in
149+
id +> Ml cmj_load_info;
150+
cmj_load_info
151+
152+
end in
153+
let cmj_table = cmj_load_info.cmj_table in
154+
(cmj_load_info.package_path,
155+
cmj_table.package_spec,
156+
cmj_table.js_file_kind)
158157

159158
let add = Lam_module_ident.Hash_set.add
160159

@@ -172,11 +171,11 @@ let is_pure_module (oid : Lam_module_ident.t) =
172171
match Js_cmj_load.load_unit_exn (Lam_module_ident.name oid) with
173172
| cmj_load_info ->
174173
oid +> Ml cmj_load_info ;
175-
Js_cmj_format.is_pure cmj_load_info.cmj_table
174+
cmj_load_info.cmj_table.pure
176175
| exception _ -> false
177176
end
178-
| Some (Ml{cmj_table}(*|Runtime {cmj_table}*)) ->
179-
Js_cmj_format.is_pure cmj_table
177+
| Some (Ml{cmj_table}) ->
178+
cmj_table.pure
180179
| Some External -> false
181180
end
182181

jscomp/core/lam_compile_env.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ val is_pure_module : Lam_module_ident.t -> bool
8585

8686
val get_package_path_from_cmj :
8787
Lam_module_ident.t ->
88-
(string * Js_packages_info.t * Js_cmj_format.cmj_case)
88+
(string * Js_packages_info.t * Ext_js_file_kind.t)
8989

9090

9191

jscomp/core/lam_compile_main.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
(* module E = Js_exp_make *)
3333
(* module S = Js_stmt_make *)
3434

35-
let get_cmj_case output_prefix : Ext_namespace.file_kind =
35+
let get_cmj_case output_prefix : Ext_js_file_kind.t =
3636
let little =
3737
Ext_char.is_lower_case (Filename.basename output_prefix).[0]
3838
in

jscomp/core/lam_stats_export.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ let export_to_cmj
139139
(meta : Lam_stats.t )
140140
effect
141141
export_map
142-
cmj_case
142+
js_file_kind
143143
: Js_cmj_format.t =
144144
let values = values_of_export meta export_map in
145145

146146
Js_cmj_format.make
147147
~values
148148
~effect
149-
~npm_package_path: (Js_packages_state.get_packages_info ())
150-
~cmj_case
149+
~package_spec: (Js_packages_state.get_packages_info ())
150+
~js_file_kind
151151
(* FIXME: make sure [-o] would not change its case
152152
add test for ns/non-ns
153153
*)

jscomp/core/lam_stats_export.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ val export_to_cmj :
3535
Lam_stats.t ->
3636
Js_cmj_format.effect ->
3737
Lam.t Map_ident.t ->
38-
Js_cmj_format.cmj_case ->
38+
Ext_js_file_kind.t ->
3939
Js_cmj_format.t
4040

jscomp/ext/ext_js_file_kind.ml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(* Copyright (C) 2020- Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t =
26+
| Upper_js
27+
| Upper_bs
28+
| Little_js
29+
| Little_bs
30+

jscomp/ext/ext_namespace.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,15 @@ let try_split_module_name name =
5656
else
5757
Some (String.sub name (i+1) (len - i - 1),
5858
String.sub name 0 i )
59-
type file_kind =
60-
| Upper_js
61-
| Upper_bs
62-
| Little_js
63-
| Little_bs
59+
6460

6561

6662

6763
(* let js_name_of_basename bs_suffix s =
6864
change_ext_ns_suffix s
6965
(if bs_suffix then Literals.suffix_bs_js else Literals.suffix_js ) *)
7066

71-
let js_name_of_modulename s little =
67+
let js_name_of_modulename s (little : Ext_js_file_kind.t) : string =
7268
match little with
7369
| Little_js ->
7470
change_ext_ns_suffix (Ext_string.uncapitalize_ascii s) Literals.suffix_js

jscomp/ext/ext_namespace.mli

+2-7
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,13 @@ val change_ext_ns_suffix :
4646
string ->
4747
string
4848

49-
type file_kind =
50-
| Upper_js
51-
| Upper_bs
52-
| Little_js
53-
| Little_bs
54-
49+
5550

5651
(** [js_name_of_modulename ~little A-Ns]
5752
*)
5853
val js_name_of_modulename :
5954
string ->
60-
file_kind ->
55+
Ext_js_file_kind.t ->
6156
string
6257

6358
(* TODO handle cases like

0 commit comments

Comments
 (0)