Skip to content

Commit 58bdc58

Browse files
committed
relax [@@bs.as {json| .. ] to allow object literals
1 parent 2a121e1 commit 58bdc58

File tree

8 files changed

+31
-59
lines changed

8 files changed

+31
-59
lines changed

jscomp/core/lam_compile_const.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,5 @@ let translate_arg_cst (cst : External_arg_spec.cst) =
130130
E.int (Int32.of_int i)
131131
| Arg_string_lit i ->
132132
E.str i
133-
| Arg_js_null -> E.nil
134133
| Arg_js_literal s
135134
-> E.raw_js_code (Exp (Js_literal {comment = None})) s
136-
137-
| Arg_js_true -> E.bool true
138-
| Arg_js_false -> E.bool false

jscomp/syntax/ast_attributes.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,20 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
285285
{pexp_desc =
286286
Pexp_constant
287287
(Pconst_string(s, (None | Some "json" as dec)))
288-
;
288+
; pexp_loc ;
289289
_},_);
290290
_}] ->
291291
if dec = None then
292292
st := Some (Str (s))
293293
else
294+
begin
295+
(match Classify_function.classify ~check:(pexp_loc, Bs_flow_ast_utils.flow_deli_offset dec)
296+
s with
297+
| Js_literal _ -> ()
298+
| _ ->
299+
Location.raise_errorf ~loc:pexp_loc "an object literal expected");
294300
st := Some (Js_literal_str s )
301+
end
295302
| _ ->
296303
Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
297304
end

jscomp/syntax/ast_external_process.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ let refine_arg_type ~(nolabel:bool) (ptyp : Ast_core_type.t)
114114
| Some (Str i)->
115115
Arg_cst (External_arg_spec.cst_string i)
116116
| Some (Js_literal_str s) ->
117-
(* FIXME: This seems to be wrong in bs.obj, we should disable such payload in bs.obj *)
118-
Arg_cst (External_arg_spec.cst_json ptyp.ptyp_loc s)
117+
Arg_cst (External_arg_spec.cst_obj_literal s)
119118
else (* ([`a|`b] [@bs.string]) *)
120119
spec_of_ptyp nolabel ptyp
121120
)

jscomp/syntax/ast_payload.ml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,7 @@ let is_single_int (x : t ) : int option =
6868

6969

7070

71-
let flow_deli_off_set deli =
72-
(match deli with
73-
| None -> 1 (* length of '"'*)
74-
| Some deli ->
75-
String.length deli + 2 (* length of "{|"*)
76-
)
77-
78-
;;
71+
7972
let raw_as_string_exp_exn
8073
~(kind: Js_raw_info.raw_kind)
8174
(x : t ) : _ option =
@@ -89,7 +82,7 @@ let raw_as_string_exp_exn
8982
;
9083
pexp_loc = loc} as e ,_);
9184
_}] ->
92-
Bs_flow_ast_utils.check_flow_errors ~loc ~offset:(flow_deli_off_set deli) (match kind with
85+
Bs_flow_ast_utils.check_flow_errors ~loc ~offset:(Bs_flow_ast_utils.flow_deli_offset deli) (match kind with
9386
| Raw_re
9487
| Raw_exp ->
9588
let (_loc,e),errors = (Parser_flow.parse_expression (Parser_env.init_env None str) false) in

jscomp/syntax/bs_flow_ast_utils.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
pos_cnum = pos_bol + column
3636
}
3737

38+
39+
let flow_deli_offset deli =
40+
(match deli with
41+
| None -> 1 (* length of '"'*)
42+
| Some deli ->
43+
String.length deli + 2 (* length of "{|"*)
44+
)
45+
46+
;;
47+
48+
3849
(* Here the loc is the payload loc *)
3950
let check_flow_errors ~(loc : Location.t)
4051
~offset

jscomp/syntax/bs_flow_ast_utils.mli

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
val flow_deli_offset:
26+
string option ->
27+
int
2528

26-
val check_flow_errors:
27-
loc:Location.t ->
28-
offset:int ->
29-
(Loc.t * Parse_error.t) list ->
30-
unit
29+
val check_flow_errors:
30+
loc:Location.t ->
31+
offset:int ->
32+
(Loc.t * Parse_error.t) list ->
33+
unit

jscomp/syntax/external_arg_spec.ml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
type cst =
2828
| Arg_int_lit of int
2929
| Arg_string_lit of string
30-
31-
| Arg_js_null
32-
| Arg_js_true
33-
| Arg_js_false
3430
| Arg_js_literal of string
3531

3632
type label_noname =
@@ -87,36 +83,6 @@ type obj_param =
8783
type obj_params = obj_param list
8884
type params = param list
8985

90-
exception Error of Location.t * Ext_json_parse.error
91-
92-
let pp_invaild_json fmt err =
93-
Format.fprintf fmt "@[Invalid json literal: %a@]@."
94-
Ext_json_parse.report_error err
95-
96-
let () =
97-
Location.register_error_of_exn (function
98-
| Error (loc,err) ->
99-
Some (Location.error_of_printer loc pp_invaild_json err)
100-
| _ -> None
101-
)
102-
103-
104-
let cst_json (loc : Location.t) s : cst =
105-
match Ext_json_parse.parse_json_from_string s with
106-
| True _ -> Arg_js_true
107-
| False _ -> Arg_js_false
108-
| Null _ -> Arg_js_null
109-
| _ -> Arg_js_literal s
110-
| exception Ext_json_parse.Error (start,finish,error_info)
111-
->
112-
let loc1 = {
113-
loc with
114-
loc_start =
115-
Ext_position.offset loc.loc_start start;
116-
loc_end =
117-
Ext_position.offset loc.loc_start finish;
118-
} in
119-
raise (Error (loc1 , error_info))
12086
let cst_obj_literal s = Arg_js_literal s
12187
let cst_int i = Arg_int_lit i
12288
let cst_string s = Arg_string_lit s

jscomp/syntax/external_arg_spec.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
type cst = private
2626
| Arg_int_lit of int
2727
| Arg_string_lit of string
28-
| Arg_js_null
29-
| Arg_js_true
30-
| Arg_js_false
3128
| Arg_js_literal of string
3229

3330

@@ -79,7 +76,7 @@ type obj_params = obj_param list
7976
type params = param list
8077

8178
val cst_obj_literal : string -> cst
82-
val cst_json : Location.t -> string -> cst
79+
8380
val cst_int : int -> cst
8481
val cst_string : string -> cst
8582

0 commit comments

Comments
 (0)