Skip to content

Commit 605919a

Browse files
committed
apply bs.config twice
-- when parsing -- when parsing is done
1 parent 613c7cd commit 605919a

15 files changed

+60314
-59976
lines changed

jscomp/core/js_implementation.ml

+19-3
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/bs_builtin_ppx.ml

+12-58
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
@@ -357,73 +353,31 @@ let unsafe_mapper : mapper =
357353
}
358354

359355

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

368357

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

380-
let signature_config_table : action_table =
381-
Map_string.of_list common_actions_table
382359

383360

384361
let rewrite_signature (x : Parsetree.signature) =
385362
Bs_ast_invariant.iter_warnings_on_sigi x;
386-
let result =
387-
match x with
388-
| {psig_desc = Psig_attribute ({txt = "ocaml.ppx.context"},_)}
389-
:: {psig_desc = Psig_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
390-
| {psig_desc = Psig_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
391-
->
392-
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
393-
(Ast_payload.table_dispatch signature_config_table) ;
394-
unsafe_mapper.signature unsafe_mapper rest
395-
| _ ->
363+
Ast_config.iter_on_bs_config_sigi x;
364+
let result =
396365
unsafe_mapper.signature unsafe_mapper x in
397-
reset ();
398366
(* Keep this check, since the check is not inexpensive*)
399367
Bs_ast_invariant.emit_external_warnings_on_signature result;
400368
result
401369

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

0 commit comments

Comments
 (0)