Skip to content

Commit 6d294e5

Browse files
committed
Clean up processing attribute @as in externals.
1 parent f3de8f4 commit 6d294e5

12 files changed

+117
-145
lines changed

jscomp/core/js_dump.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,12 @@ and expression_desc cxt ~(level : int) f x : cxt =
595595
(*TODO --
596596
when utf8-> it will not escape '\\' which is definitely not we want
597597
*)
598-
if delim <> None then P.string f ("\"" ^ txt ^ "\"")
599-
else Js_dump_string.pp_string f txt;
598+
if delim = Some "j" || delim = Some "*j" then
599+
P.string f ("\"" ^ txt ^ "\"")
600+
else if delim = Some "json" then
601+
P.string f txt
602+
else
603+
Js_dump_string.pp_string f txt;
600604
cxt
601605
| Raw_js_code { code = s; code_info = info } -> (
602606
match info with

jscomp/core/lam_compile_const.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,4 @@ and translate (x : Lam_constant.t) : J.expression =
101101
let translate_arg_cst (cst : External_arg_spec.cst) =
102102
match cst with
103103
| Arg_int_lit i -> E.int (Int32.of_int i)
104-
| Arg_string_lit i -> E.str i ~delim:(Some "j")
105-
| Arg_js_literal s -> E.raw_js_code (Exp (Js_literal { comment = None })) s
104+
| Arg_string_lit (s, delim) -> E.str s ~delim

jscomp/frontend/ast_attributes.ml

+9-8
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ let iter_process_bs_int_as (attrs : t) =
271271
| _ -> ());
272272
!st
273273

274-
type as_const_payload = Int of int | Str of string | Js_literal_str of string
274+
type as_const_payload = Int of int | Str of string * string option
275275

276276
let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
277277
let st = ref None in
@@ -292,7 +292,7 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
292292
pexp_desc =
293293
Pexp_constant
294294
(Pconst_string
295-
(s, ((None | Some "json"| Some "*j") as dec)));
295+
(s, ((None | Some "json"| Some "*j") as delim)));
296296
pexp_loc;
297297
_;
298298
},
@@ -301,19 +301,20 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
301301
};
302302
]
303303
->
304-
if dec = None || dec = Some "*j" then st := Some (Str s)
305-
else (
306-
(match
304+
st := Some (Str (s, delim));
305+
if delim = Some "json" then (
306+
(* check that it is a valid object literal *)
307+
match
307308
Classify_function.classify
308309
~check:
309-
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset dec)
310+
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset delim)
310311
s
311312
with
312313
| Js_literal _ -> ()
313314
| _ ->
314315
Location.raise_errorf ~loc:pexp_loc
315-
"an object literal expected");
316-
st := Some (Js_literal_str s))
316+
"an object literal expected"
317+
)
317318
| _ -> Bs_syntaxerr.err loc (Expect_int_or_string_or_json_literal)
318319
)
319320
| Some v -> st := Some (Int v))

jscomp/frontend/ast_attributes.mli

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ val has_bs_optional : t -> bool
5555

5656
val iter_process_bs_int_as : t -> int option
5757

58-
type as_const_payload = Int of int | Str of string | Js_literal_str of string
59-
58+
type as_const_payload = Int of int | Str of string * string option
6059
val iter_process_bs_string_or_int_as : t -> as_const_payload option
6160

6261
val process_derive_type : t -> derive_attr * t

jscomp/frontend/ast_external_process.ml

+3-7
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ let refine_arg_type ~(nolabel : bool) (ptyp : Ast_core_type.t) :
102102
| Int i ->
103103
(* This type is used in obj only to construct obj type*)
104104
Arg_cst (External_arg_spec.cst_int i)
105-
| Str i -> Arg_cst (External_arg_spec.cst_string i)
106-
| Js_literal_str s -> Arg_cst (External_arg_spec.cst_obj_literal s))
105+
| Str (i, delim) -> Arg_cst (External_arg_spec.cst_string i delim))
107106
else (* ([`a|`b] [@string]) *)
108107
spec_of_ptyp nolabel ptyp
109108

@@ -123,12 +122,9 @@ let refine_obj_arg_type ~(nolabel : bool) (ptyp : Ast_core_type.t) :
123122
(* @as(24) *)
124123
(* This type is used in obj only to construct obj type *)
125124
Arg_cst (External_arg_spec.cst_int i)
126-
| Some (Str i) ->
125+
| Some (Str (s, delim)) ->
127126
(* @as("foo") *)
128-
Arg_cst (External_arg_spec.cst_string i)
129-
| Some (Js_literal_str s) ->
130-
(* @as(json`true`) *)
131-
Arg_cst (External_arg_spec.cst_obj_literal s))
127+
Arg_cst (External_arg_spec.cst_string s delim))
132128
else (* ([`a|`b] [@string]) *)
133129
spec_of_ptyp nolabel ptyp
134130

jscomp/frontend/external_arg_spec.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
type cst =
2828
| Arg_int_lit of int
29-
| Arg_string_lit of string
30-
| Arg_js_literal of string
29+
| Arg_string_lit of string * string option
3130

3231
type label_noname = Arg_label | Arg_empty | Arg_optional
3332

@@ -70,11 +69,9 @@ type obj_params = obj_param list
7069

7170
type params = param list
7271

73-
let cst_obj_literal s = Arg_js_literal s
74-
7572
let cst_int i = Arg_int_lit i
7673

77-
let cst_string s = Arg_string_lit s
74+
let cst_string s delim = Arg_string_lit (s, delim)
7875

7976
let empty_label = Obj_empty
8077

jscomp/frontend/external_arg_spec.mli

+2-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
type cst = private
2626
| Arg_int_lit of int
27-
| Arg_string_lit of string
28-
| Arg_js_literal of string
27+
| Arg_string_lit of string * string option
2928

3029
type attr =
3130
| Poly_var_string of { descr : (string * string) list }
@@ -55,11 +54,9 @@ type obj_params = obj_param list
5554

5655
type params = param list
5756

58-
val cst_obj_literal : string -> cst
59-
6057
val cst_int : int -> cst
6158

62-
val cst_string : string -> cst
59+
val cst_string : string -> string option -> cst
6360

6461
val empty_label : label
6562

jscomp/main/builtin_cmi_datasets.ml

+5-5
Large diffs are not rendered by default.

jscomp/test/prepend_data_ffi.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function f(x) {
4242
]);
4343
x.xx(116, 3, "xxx", 1, 2, 3);
4444
x.xx(117, 3, "xxx", 0, "b", 1, 2, 3, 4, 5);
45-
x.xx(118, 3, true, false, "你好", ["你好",1,2,3], [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}], [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}], "xxx", 0, "yyy", "b", 1, 2, 3, 4, 5);
45+
x.xx(118, 3, true, false, "你好", ["你好",1,2,3] , [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] , [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] , "xxx", 0, "yyy", "b", 1, 2, 3, 4, 5);
4646
}
4747

4848
process.on("exit", (function (exit_code) {

lib/4.06.1/unstable/js_compiler.ml

+29-36
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_playground_compiler.ml

+29-36
Large diffs are not rendered by default.

lib/4.06.1/whole_compiler.ml

+29-36
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)