Skip to content

Commit 8420635

Browse files
author
Hongbo Zhang
committed
clean up: unify js object support
1 parent 95bf235 commit 8420635

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

jscomp/syntax/ppx_entry.ml

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,11 @@ open Ast_helper
6060

6161

6262
let record_as_js_object = ref false (* otherwise has an attribute *)
63-
64-
65-
let obj_type_as_js_obj_type = ref false
66-
67-
6863
let no_export = ref false
6964

7065

7166
let reset () =
7267
record_as_js_object := false ;
73-
obj_type_as_js_obj_type := false ;
7468
no_export := false
7569

7670

@@ -170,7 +164,7 @@ let handle_typ
170164
match ty with
171165
| {ptyp_desc = Ptyp_extension({txt = "bs.obj"}, PTyp ty)}
172166
->
173-
Ext_ref.non_exn_protect obj_type_as_js_obj_type true
167+
Ext_ref.non_exn_protect record_as_js_object true
174168
(fun _ -> self.typ self ty )
175169
| {ptyp_attributes ;
176170
ptyp_desc = Ptyp_arrow (label, args, body);
@@ -227,7 +221,7 @@ let handle_typ
227221
{ ty
228222
with ptyp_desc = Ptyp_object(methods, closed_flag);
229223
} in
230-
if !obj_type_as_js_obj_type then
224+
if !record_as_js_object then
231225
Ast_comb.to_js_type loc inner_type
232226
else inner_type
233227
| _ -> super.typ self ty
@@ -295,11 +289,7 @@ let rec unsafe_mapper : Ast_mapper.mapper =
295289
begin match payload with
296290
| PStr [{pstr_desc = Pstr_eval (e,_)}]
297291
->
298-
Ext_ref.non_exn_protect2
299-
record_as_js_object
300-
obj_type_as_js_obj_type
301-
true
302-
true
292+
Ext_ref.non_exn_protect record_as_js_object true
303293
(fun () -> self.expr self e )
304294
| _ -> Location.raise_errorf ~loc "Expect an expression here"
305295
end
@@ -410,14 +400,18 @@ let rec unsafe_mapper : Ast_mapper.mapper =
410400
pexp_attributes }
411401
end
412402
end
413-
| Pexp_record (label_exprs, None) ->
414-
(* TODO better error message when [with] detected in [%bs.obj] *)
403+
| Pexp_record (label_exprs, opt_exp) ->
415404
if !record_as_js_object then
416-
{ e with
417-
pexp_desc =
418-
Ast_util.record_as_js_object loc self label_exprs;
419-
}
420-
else
405+
(match opt_exp with
406+
| None ->
407+
{ e with
408+
pexp_desc =
409+
Ast_util.record_as_js_object loc self label_exprs;
410+
}
411+
| Some e ->
412+
Location.raise_errorf
413+
~loc:e.pexp_loc "`with` construct is not supported in bs.obj ")
414+
else (* could be supported using `Object.assign`? *)
421415
Ast_mapper.default_mapper.expr self e
422416
| Pexp_object {pcstr_self; pcstr_fields} ->
423417
begin match Ast_attributes.process_bs e.pexp_attributes with

jscomp/test/mutable_obj_test.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ module type X = sig
1919
(* end [@bs] syntax error *)
2020

2121
end
22+
23+
24+
(* let u x = *)
25+
(* [%bs.obj{ *)
26+
(* x with y = 3 *)
27+
(* }] *)

0 commit comments

Comments
 (0)