Skip to content

Commit e7c50e4

Browse files
authored
Merge pull request #4177 from BuckleScript/bs_config
make bs.config available in typing stage
2 parents 06db33b + 605919a commit e7c50e4

26 files changed

+60439
-60042
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jscomp/bin/*.exe
6565
odoc_gen/*.cmxs
6666

6767
# for npm publish
68-
./lib/*.exe
68+
*.exe
6969
!bin/bsppx
7070
!bin/bspack
7171
!/bin/bsb

lib/bsrefmt renamed to bsrefmt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var child_process = require('child_process')
55
var path = require('path')
66

77
// the underlying binary is called refmt
8-
var exe = path.join(__dirname, 'refmt.exe')
8+
var exe = path.join(__dirname, process.platform,'refmt.exe')
99
var delegate_args = process.argv.slice(2)
1010

1111
try {

jscomp/common/js_config.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ let js_stdout = ref true
111111

112112
let all_module_aliases = ref false
113113

114-
let no_stdlib = ref false
114+
let no_stdlib = ref false
115+
116+
let no_export = ref false
117+
118+
let record_as_js_object = ref false (* otherwise has an attribute *)

jscomp/common/js_config.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ val js_stdout : bool ref
108108

109109
val all_module_aliases : bool ref
110110

111-
val no_stdlib: bool ref
111+
val no_stdlib: bool ref
112+
val no_export: bool ref
113+
val record_as_js_object : bool ref

jscomp/core/js_implementation.ml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let process_with_gentype filename =
5050
)
5151

5252
let after_parsing_sig ppf outputprefix ast =
53+
Ast_config.iter_on_bs_config_sigi ast;
5354
if !Js_config.simple_binary_ast then begin
5455
let oc = open_out_bin (outputprefix ^ Literals.suffix_mliast_simple) in
5556
Ml_binary.write_ast Mli !Location.input_name ast oc;
@@ -137,11 +138,26 @@ let all_module_alias (ast : Parsetree.structure)=
137138
| Pstr_extension _ -> false
138139
)
139140

140-
let after_parsing_impl ppf outputprefix ast =
141+
let no_export (rest : Parsetree.structure) : Parsetree.structure =
142+
match rest with
143+
| head :: _ ->
144+
let loc = head.pstr_loc in
145+
Ast_helper.[Str.include_ ~loc
146+
(Incl.mk ~loc
147+
(Mod.constraint_ ~loc
148+
(Mod.structure ~loc rest )
149+
(Mty.signature ~loc [])
150+
))]
151+
| _ -> rest
152+
153+
let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
141154
Js_config.all_module_aliases :=
142155
!Clflags.assume_no_mli = Mli_non_exists &&
143-
all_module_alias ast
144-
;
156+
all_module_alias ast;
157+
Ast_config.iter_on_bs_config_stru ast;
158+
let ast =
159+
if !Js_config.no_export then
160+
no_export ast else ast in
145161
if !Js_config.simple_binary_ast then begin
146162
let oc = open_out_bin (outputprefix ^ Literals.suffix_mlast_simple) in
147163
Ml_binary.write_ast Ml !Location.input_name ast oc;

jscomp/syntax/ast_config.ml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
26+
type action_table =
27+
(Parsetree.expression option -> unit) Map_string.t
28+
(** global configurations below *)
29+
let common_actions_table :
30+
(string * (Parsetree.expression option -> unit)) list =
31+
[
32+
]
33+
34+
35+
let structural_config_table : action_table =
36+
Map_string.of_list
37+
(( "no_export" ,
38+
(fun x ->
39+
Js_config.no_export := (
40+
match x with
41+
|Some e -> Ast_payload.assert_bool_lit e
42+
| None -> true)
43+
))
44+
:: common_actions_table)
45+
46+
let signature_config_table : action_table =
47+
Map_string.of_list common_actions_table
48+
49+
50+
let rec iter_on_bs_config_stru (x :Parsetree.structure) =
51+
match x with
52+
| [] -> ()
53+
| {pstr_desc = Pstr_attribute (({txt = "bs.config"; loc}, payload) as attr)}::_ ->
54+
Bs_ast_invariant.mark_used_bs_attribute attr;
55+
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
56+
(Ast_payload.table_dispatch structural_config_table)
57+
| {pstr_desc = Pstr_attribute _} :: rest ->
58+
iter_on_bs_config_stru rest
59+
| non_attr :: _ -> ()
60+
61+
let rec iter_on_bs_config_sigi (x :Parsetree.signature) =
62+
match x with
63+
| [] -> ()
64+
| {psig_desc = Psig_attribute (({txt = "bs.config"; loc}, payload) as attr)}::_ ->
65+
Bs_ast_invariant.mark_used_bs_attribute attr;
66+
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
67+
(Ast_payload.table_dispatch signature_config_table)
68+
| {psig_desc = Psig_attribute _} :: rest ->
69+
iter_on_bs_config_sigi rest
70+
| non_attr :: _ -> ()

jscomp/syntax/ast_config.mli

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
26+
27+
28+
val iter_on_bs_config_stru :
29+
Parsetree.structure ->
30+
unit
31+
32+
val iter_on_bs_config_sigi :
33+
Parsetree.signature ->
34+
unit

jscomp/syntax/ast_payload.ml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ let raw_as_string_exp_exn
125125
Some e
126126
| _ -> None
127127

128-
let as_core_type loc x =
128+
let as_core_type loc (x : t) =
129129
match x with
130-
| Parsetree.PTyp x -> x
130+
| PTyp x -> x
131131
| _ -> Location.raise_errorf ~loc "except a core type"
132132

133133
let as_ident (x : t ) =
@@ -143,10 +143,6 @@ let as_ident (x : t ) =
143143
}
144144
] -> Some ident
145145
| _ -> None
146-
(* open Ast_helper *)
147-
148-
(* let raw_string_payload loc (s : string) : t =
149-
PStr [ Str.eval ~loc (Ast_compatible.const_exp_string ~loc s) ] *)
150146

151147

152148
type lid = string Asttypes.loc
@@ -164,7 +160,7 @@ type action =
164160

165161
let ident_or_record_as_config
166162
loc
167-
(x : Parsetree.payload)
163+
(x : t)
168164
: ( string Location.loc * Parsetree.expression option) list
169165
=
170166
match x with
@@ -211,7 +207,7 @@ let ident_or_record_as_config
211207

212208
let assert_strings loc (x : t) : string list
213209
=
214-
let module M = struct exception Not_str end in
210+
let exception Not_str in
215211
match x with
216212
| PStr [ {pstr_desc =
217213
Pstr_eval (
@@ -227,8 +223,8 @@ let assert_strings loc (x : t) : string list
227223
Pconst_string
228224
(name,_)); _} ->
229225
name
230-
| _ -> raise M.Not_str)
231-
with M.Not_str ->
226+
| _ -> raise Not_str)
227+
with Not_str ->
232228
Location.raise_errorf ~loc "expect string tuple list"
233229
)
234230
| PStr [ {

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,12 @@
5353
*)
5454

5555

56-
let record_as_js_object = ref false (* otherwise has an attribute *)
57-
let no_export = ref false
56+
5857

5958
let () =
6059
Ast_derive_projector.init ();
6160
Ast_derive_js_mapper.init ()
6261

63-
let reset () =
64-
record_as_js_object := false ;
65-
no_export := false
6662

6763

6864

@@ -74,7 +70,7 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
7470
match e.pexp_desc with
7571
(** Its output should not be rewritten anymore *)
7672
| Pexp_extension extension ->
77-
Ast_exp_extension.handle_extension record_as_js_object e self extension
73+
Ast_exp_extension.handle_extension Js_config.record_as_js_object e self extension
7874
| Pexp_constant (
7975
Pconst_string
8076
(s, (Some delim)))
@@ -140,7 +136,7 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
140136
constraint 'b :> 'a
141137
]}
142138
*)
143-
if !record_as_js_object then
139+
if !Js_config.record_as_js_object then
144140
(match opt_exp with
145141
| None ->
146142
{ e with
@@ -180,7 +176,7 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
180176

181177

182178
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
183-
Ast_core_type_class_type.typ_mapper record_as_js_object self typ
179+
Ast_core_type_class_type.typ_mapper Js_config.record_as_js_object self typ
184180

185181
let class_type_mapper (self : mapper) ({pcty_attributes; pcty_loc} as ctd : Parsetree.class_type) =
186182
match Ast_attributes.process_bs pcty_attributes with
@@ -283,6 +279,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
283279
Ast_exp_handle_external.handle_raw_structure loc payload
284280
| Pstr_extension (({txt = ("bs.debugger.chrome" | "debugger.chrome") ;loc}, payload),_)
285281
->
282+
Location.prerr_warning loc (Preprocessor "this extension can be safely removed");
286283
Ast_structure.dummy_item loc
287284
| Pstr_type (
288285
_rf,
@@ -356,73 +353,31 @@ let unsafe_mapper : mapper =
356353
}
357354

358355

359-
type action_table =
360-
(Parsetree.expression option -> unit) Map_string.t
361-
(** global configurations below *)
362-
let common_actions_table :
363-
(string * (Parsetree.expression option -> unit)) list =
364-
[
365-
]
366356

367357

368-
let structural_config_table : action_table =
369-
Map_string.of_list
370-
(( "no_export" ,
371-
(fun x ->
372-
no_export := (
373-
match x with
374-
|Some e -> Ast_payload.assert_bool_lit e
375-
| None -> true)
376-
))
377-
:: common_actions_table)
378358

379-
let signature_config_table : action_table =
380-
Map_string.of_list common_actions_table
381359

382360

383361
let rewrite_signature (x : Parsetree.signature) =
384362
Bs_ast_invariant.iter_warnings_on_sigi x;
385-
let result =
386-
match x with
387-
| {psig_desc = Psig_attribute ({txt = "ocaml.ppx.context"},_)}
388-
:: {psig_desc = Psig_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
389-
| {psig_desc = Psig_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
390-
->
391-
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
392-
(Ast_payload.table_dispatch signature_config_table) ;
393-
unsafe_mapper.signature unsafe_mapper rest
394-
| _ ->
363+
Ast_config.iter_on_bs_config_sigi x;
364+
let result =
395365
unsafe_mapper.signature unsafe_mapper x in
396-
reset ();
397366
(* Keep this check, since the check is not inexpensive*)
398367
Bs_ast_invariant.emit_external_warnings_on_signature result;
399368
result
400369

370+
371+
372+
373+
374+
401375
(* Note we also drop attributes like [@@@bs.deriving ] for convenience*)
402376
let rewrite_implementation (x : Parsetree.structure) =
403377
Bs_ast_invariant.iter_warnings_on_stru x ;
378+
Ast_config.iter_on_bs_config_stru x ;
404379
let result =
405-
match x with
406-
| {pstr_desc = Pstr_attribute ({txt = "ocaml.ppx.context"},_)}
407-
:: {pstr_desc = Pstr_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
408-
| {pstr_desc = Pstr_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
409-
->
410-
begin
411-
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
412-
(Ast_payload.table_dispatch structural_config_table) ;
413-
let rest = unsafe_mapper.structure unsafe_mapper rest in
414-
if !no_export then
415-
Ast_helper.[Str.include_ ~loc
416-
(Incl.mk ~loc
417-
(Mod.constraint_ ~loc
418-
(Mod.structure ~loc rest )
419-
(Mty.signature ~loc [])
420-
))]
421-
else rest
422-
end
423-
| _ ->
424380
unsafe_mapper.structure unsafe_mapper x in
425-
reset ();
426381
(* Keep this check since it is not inexpensive*)
427382
Bs_ast_invariant.emit_external_warnings_on_structure result;
428383
result

jscomp/test/build.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ build test/inner_call.cmi test/inner_call.cmj : cc test/inner_call.ml | test/inn
356356
build test/inner_define.cmj : cc_cmi test/inner_define.ml | test/inner_define.cmi $stdlib
357357
build test/inner_define.cmi : cc test/inner_define.mli | $stdlib
358358
build test/inner_unused.cmi test/inner_unused.cmj : cc test/inner_unused.ml | $stdlib
359-
build test/installation_test.cmi test/installation_test.cmj : cc test/installation_test.ml | test/app_root_finder.cmj test/mt.cmj $stdlib
359+
build test/installation_test.cmi test/installation_test.cmj : cc test/installation_test.ml | test/mt.cmj $stdlib
360360
build test/int32_test.cmi test/int32_test.cmj : cc test/int32_test.ml | test/ext_array_test.cmj test/mt.cmj $stdlib
361361
build test/int64_mul_div_test.cmi test/int64_mul_div_test.cmj : cc test/int64_mul_div_test.ml | test/mt.cmj $stdlib
362362
build test/int64_test.cmi test/int64_test.cmj : cc test/int64_test.ml | test/ext_array_test.cmj test/mt.cmj $stdlib

0 commit comments

Comments
 (0)