Skip to content

Commit a8a5f65

Browse files
committed
Refactor: list explicitly all the possible xxxfoo delimiters in a variant.
#5522
1 parent 241565b commit a8a5f65

13 files changed

+1737
-1713
lines changed

Diff for: jscomp/core/j.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ and for_ident = ident
7676
and for_direction = Js_op.direction_flag
7777
and property_map = (property_name * expression) list
7878
and length_object = Js_op.length_object
79+
and delim = | Nothing | J | JS | StarJ | Json
7980

8081
and expression_desc =
8182
| Length of expression * length_object
@@ -135,7 +136,7 @@ and expression_desc =
135136
return_unit : bool;
136137
async : bool;
137138
}
138-
| Str of { delim : string option; txt : string }
139+
| Str of { delim : delim; txt : string }
139140
(* A string is UTF-8 encoded, and may contain
140141
escape sequences.
141142
*)

Diff for: jscomp/core/js_dump.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
597597
(*TODO --
598598
when utf8-> it will not escape '\\' which is definitely not we want
599599
*)
600-
if delim = Some "j" || delim = Some "*j" then
600+
if delim = J || delim = StarJ then
601601
P.string f ("\"" ^ txt ^ "\"")
602-
else if delim = Some "json" then P.string f txt
602+
else if delim = Json then P.string f txt
603603
else Js_dump_string.pp_string f txt;
604604
cxt
605605
| Raw_js_code { code = s; code_info = info } -> (

Diff for: jscomp/core/js_exp_make.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ let pure_runtime_call module_name fn_name args =
123123

124124
let runtime_ref module_name fn_name = runtime_var_dot module_name fn_name
125125

126-
let str ?(delim = None) ?comment txt : t =
126+
let str ?(delim = J.Nothing) ?comment txt : t =
127127
{ expression_desc = Str { txt; delim }; comment }
128128

129129
let raw_js_code ?comment info s : t =
@@ -487,7 +487,7 @@ let array_length ?comment (e : t) : t =
487487

488488
let string_length ?comment (e : t) : t =
489489
match e.expression_desc with
490-
| Str { txt; delim = None } -> int ?comment (Int32.of_int (String.length txt))
490+
| Str { txt; delim = Nothing } -> int ?comment (Int32.of_int (String.length txt))
491491
(* No optimization for {j||j}*)
492492
| _ -> { expression_desc = Length (e, String); comment }
493493

Diff for: jscomp/core/js_exp_make.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ val pure_runtime_call :
8282

8383
val runtime_ref : string -> string -> t
8484

85-
val str : ?delim: string option -> ?comment: string -> string -> t
85+
val str : ?delim: J.delim -> ?comment: string -> string -> t
8686

8787
val ocaml_fun :
8888
?comment:string ->

Diff for: jscomp/core/lam_compile_const.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ and translate (x : Lam_constant.t) : J.expression =
7171
(* https://github.com/google/closure-library/blob/master/closure%2Fgoog%2Fmath%2Flong.js *)
7272
| Const_float f -> E.float f (* TODO: preserve float *)
7373
| Const_string { s; unicode = false } -> E.str s
74-
| Const_string { s; unicode = true } -> E.str ~delim:(Some "j") s
74+
| Const_string { s; unicode = true } -> E.str ~delim:J s
7575
| Const_pointer name -> E.str name
7676
| Const_block (tag, tag_info, xs) ->
7777
Js_of_lam_block.make_block NA tag_info (E.small_int tag)

Diff for: jscomp/frontend/ast_attributes.ml

+24-19
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
type attr = Parsetree.attribute
26-
2726
type t = attr list
28-
2927
type ('a, 'b) st = { get : 'a option; set : 'b option }
3028

3129
let process_method_attributes_rev (attrs : t) =
@@ -164,14 +162,13 @@ let is_inline : attr -> bool =
164162
let has_inline_payload (attrs : t) = Ext_list.find_first attrs is_inline
165163

166164
let is_await : attr -> bool =
167-
fun ({ txt }, _) -> txt = "await" || txt = "res.await"
165+
fun ({ txt }, _) -> txt = "await" || txt = "res.await"
168166

169167
let is_async : attr -> bool =
170-
fun ({ txt }, _) -> txt = "async" || txt = "res.async"
168+
fun ({ txt }, _) -> txt = "async" || txt = "res.async"
171169

172170
let has_await_payload (attrs : t) = Ext_list.find_first attrs is_await
173171
let has_async_payload (attrs : t) = Ext_list.find_first attrs is_async
174-
175172

176173
type derive_attr = { bs_deriving : Ast_payload.action list option } [@@unboxed]
177174

@@ -281,10 +278,16 @@ let iter_process_bs_int_as (attrs : t) =
281278
| _ -> ());
282279
!st
283280

284-
type as_const_payload = Int of int | Str of string * string option
281+
type as_const_payload = Int of int | Str of string * J.delim
285282

286283
let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
287284
let st = ref None in
285+
let process_delim = function
286+
| None -> Some J.Nothing
287+
| Some "json" -> Some Json
288+
| Some "*j" -> Some StarJ
289+
| _ -> None
290+
in
288291
Ext_list.iter attrs (fun (({ txt; loc }, payload) as attr) ->
289292
match txt with
290293
| "bs.as" | "as" ->
@@ -300,32 +303,34 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
300303
Pstr_eval
301304
( {
302305
pexp_desc =
303-
Pexp_constant
304-
(Pconst_string
305-
(s, ((None | Some "json"| Some "*j") as delim)));
306+
Pexp_constant (Pconst_string (s, delim_));
306307
pexp_loc;
307308
_;
308309
},
309310
_ );
310311
_;
311312
};
312313
]
313-
->
314+
when process_delim delim_ <> None -> (
315+
let delim =
316+
match process_delim delim_ with
317+
| None -> assert false
318+
| Some delim -> delim
319+
in
314320
st := Some (Str (s, delim));
315-
if delim = Some "json" then (
321+
if delim = Json then
316322
(* check that it is a valid object literal *)
317323
match
318-
Classify_function.classify
319-
~check:
320-
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset delim)
321-
s
322-
with
324+
Classify_function.classify
325+
~check:
326+
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset delim_)
327+
s
328+
with
323329
| Js_literal _ -> ()
324330
| _ ->
325331
Location.raise_errorf ~loc:pexp_loc
326-
"an object literal expected"
327-
)
328-
| _ -> Bs_syntaxerr.err loc (Expect_int_or_string_or_json_literal)
332+
"an object literal expected")
333+
| _ -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
329334
)
330335
| Some v -> st := Some (Int v))
331336
else Bs_syntaxerr.err loc Duplicated_bs_as

Diff for: jscomp/frontend/ast_attributes.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ val has_bs_optional : t -> bool
5858

5959
val iter_process_bs_int_as : t -> int option
6060

61-
type as_const_payload = Int of int | Str of string * string option
61+
type as_const_payload = Int of int | Str of string * J.delim
6262
val iter_process_bs_string_or_int_as : t -> as_const_payload option
6363

6464
val process_derive_type : t -> derive_attr * t

Diff for: jscomp/frontend/external_arg_spec.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
type cst =
2828
| Arg_int_lit of int
29-
| Arg_string_lit of string * string option
29+
| Arg_string_lit of string * J.delim
3030

3131
type label_noname = Arg_label | Arg_empty | Arg_optional
3232

Diff for: jscomp/frontend/external_arg_spec.mli

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

2525
type cst = private
2626
| Arg_int_lit of int
27-
| Arg_string_lit of string * string option
27+
| Arg_string_lit of string * J.delim
2828

2929
type attr =
3030
| Poly_var_string of { descr : (string * string) list }
@@ -56,7 +56,7 @@ type params = param list
5656

5757
val cst_int : int -> cst
5858

59-
val cst_string : string -> string option -> cst
59+
val cst_string : string -> J.delim -> cst
6060

6161
val empty_label : label
6262

Diff for: jscomp/main/builtin_cmi_datasets.ml

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

Diff for: lib/4.06.1/unstable/js_compiler.ml

+39-33
Large diffs are not rendered by default.

Diff for: lib/4.06.1/unstable/js_playground_compiler.ml

+39-33
Large diffs are not rendered by default.

Diff for: lib/4.06.1/whole_compiler.ml

+1,620-1,614
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)