Skip to content

Commit ca46607

Browse files
committed
bug fix: fix json payload in bs.obj which can not infer the types
1 parent 99f25cc commit ca46607

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

jscomp/syntax/ast_external_process.ml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,29 @@ let refine_arg_type ~(nolabel:bool) (ptyp : Ast_core_type.t)
119119
else (* ([`a|`b] [@bs.string]) *)
120120
ptyp, spec_of_ptyp nolabel ptyp
121121

122-
let refine_obj_arg_type = refine_arg_type
122+
let refine_obj_arg_type ~(nolabel:bool) (ptyp : Ast_core_type.t)
123+
: Ast_core_type.t * External_arg_spec.attr =
124+
if ptyp.ptyp_desc = Ptyp_any then
125+
let ptyp_attrs = ptyp.ptyp_attributes in
126+
let result = Ast_attributes.iter_process_bs_string_or_int_as ptyp_attrs in
127+
(* when ppx start dropping attributes
128+
we should warn, there is a trade off whether
129+
we should warn dropped non bs attribute or not
130+
*)
131+
Bs_ast_invariant.warn_discarded_unused_attributes ptyp_attrs;
132+
match result with
133+
| None ->
134+
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
135+
| Some (Int i) -> (* (_[@bs.as ])*)
136+
(* This type is used in bs.obj only to construct obj type*)
137+
Ast_literal.type_int ~loc:ptyp.ptyp_loc (), Arg_cst(External_arg_spec.cst_int i)
138+
| Some (Str i)->
139+
Ast_literal.type_string ~loc:ptyp.ptyp_loc (), Arg_cst (External_arg_spec.cst_string i)
140+
| Some (Json_str _) ->
141+
Location.raise_errorf ~loc:ptyp.ptyp_loc "json payload is not supported in bs.obj since its type can not be inferred"
142+
else (* ([`a|`b] [@bs.string]) *)
143+
ptyp, spec_of_ptyp nolabel ptyp
144+
123145
(** Given the type of argument, process its [bs.] attribute and new type,
124146
The new type is currently used to reconstruct the external type
125147
and result type in [@@bs.obj]

jscomp/test/gpr_1170.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ external set_hi :
88
"hi"
99
[@@bs.set]
1010

11+
12+
#if 0 then
13+
external ff_json : hi:int -> lo:(_[@bs.as {json|null|json}]) -> _ = "" [@@bs.obj]
14+
15+
let uu : < hi : int; lo : string > Js.t = ff_json ~hi:3
16+
#end
17+
1118
let f resp =
1219
set_okay resp ;
1320
set_hi resp

0 commit comments

Comments
 (0)