Skip to content

Commit b4c74af

Browse files
committed
Clean up utf8 transformation
1 parent 6839c3d commit b4c74af

File tree

7 files changed

+52
-36
lines changed

7 files changed

+52
-36
lines changed

jscomp/core/lam_constant.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ let rec convert_constant ( const : Lambda.structured_constant) : t =
9191
| Const_base (Const_int i) -> (Const_int i)
9292
| Const_base (Const_char i) -> (Const_char i)
9393
| Const_base (Const_string(i,opt)) ->
94-
begin match opt with
94+
(match opt with
9595
| Some opt when
96-
Ext_string.equal opt Literals.escaped_j_delimiter ->
96+
Ast_utf8_string_interp.is_unicode_string opt ->
9797
Const_unicode i
9898
| _ ->
99-
Const_string i
100-
end
99+
Const_string i)
100+
101101
| Const_base (Const_float i) -> (Const_float i)
102102
| Const_base (Const_int32 i) -> (Const_int32 i)
103103
| Const_base (Const_int64 i) -> (Const_int64 i)

jscomp/ext/literals.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ let reactjs_jsx_ppx_2_exe = "reactjs_jsx_ppx_2.exe"
119119
let reactjs_jsx_ppx_3_exe = "reactjs_jsx_ppx_3.exe"
120120
let unescaped_j_delimiter = "j"
121121
let unescaped_js_delimiter = "js"
122-
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
123122

124123
let native = "native"
125124
let bytecode = "bytecode"

jscomp/ext/literals.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ val dash_nostdlib : string
119119
val reactjs_jsx_ppx_2_exe : string
120120
val reactjs_jsx_ppx_3_exe : string
121121
val unescaped_j_delimiter : string
122-
val escaped_j_delimiter : string
122+
123123

124124
val unescaped_js_delimiter : string
125125

jscomp/syntax/ast_attributes.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,15 @@ let iter_process_bs_string_as (attrs : t) : string option =
249249
!st
250250

251251
let has_bs_optional (attrs : t) : bool =
252-
List.exists
253-
(fun
254-
(({txt ; loc}, _payload ) as attr : attr) ->
252+
Ext_list.exists attrs (fun
253+
(({txt ; }, _ ) as attr) ->
255254
match txt with
256255
| "bs.optional"
257256
->
258257
Bs_ast_invariant.mark_used_bs_attribute attr ;
259258
true
260259
| _ -> false
261-
) attrs
260+
)
262261

263262

264263

jscomp/syntax/ast_utf8_string_interp.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ let to_string_ident : Longident.t =
384384
Ldot (Ldot (Lident "Js", "String"), "make")
385385

386386

387+
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
387388

388-
let escaped = Some Literals.escaped_j_delimiter
389+
let escaped = Some escaped_j_delimiter
389390

390391
let concat_exp
391392
(a : Parsetree.expression)
@@ -440,8 +441,7 @@ let transform_interp loc s =
440441
let rev_segments = cxt.segments in
441442
match rev_segments with
442443
| [] ->
443-
Ast_compatible.const_exp_string ~loc
444-
"" ~delimiter:Literals.escaped_j_delimiter
444+
Ast_compatible.const_exp_string ~loc "" ?delimiter:escaped
445445
| [ segment] ->
446446
aux loc segment
447447
| a::rest ->
@@ -454,3 +454,21 @@ let transform_interp loc s =
454454
->
455455
Location.raise_errorf ~loc:(update border start pos loc )
456456
"%a" pp_error error
457+
458+
459+
let transform (e : Parsetree.expression) s delim : Parsetree.expression =
460+
if Ext_string.equal delim Literals.unescaped_js_delimiter then
461+
let js_str = Ast_utf8_string.transform e.pexp_loc s in
462+
{ e with pexp_desc =
463+
Pexp_constant (
464+
#if OCAML_VERSION =~ ">4.03.0" then
465+
Pconst_string
466+
#else
467+
Const_string
468+
#end
469+
(js_str, escaped))}
470+
else if Ext_string.equal delim Literals.unescaped_j_delimiter then
471+
transform_interp e.pexp_loc s
472+
else e
473+
474+
let is_unicode_string opt = Ext_string.equal opt escaped_j_delimiter

jscomp/syntax/ast_utf8_string_interp.mli

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ type exn += Error of pos * pos * error
6565
val empty_segment : segment -> bool
6666

6767
val transform_test : string -> segment list
68-
val transform_interp : Location.t -> string -> Parsetree.expression
68+
69+
70+
71+
val transform :
72+
Parsetree.expression ->
73+
string ->
74+
string ->
75+
Parsetree.expression
76+
77+
val is_unicode_string :
78+
string ->
79+
bool

jscomp/syntax/ppx_entry.ml

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ let reset () =
7070
record_as_js_object := false ;
7171
no_export := false
7272

73+
7374
let rec unsafe_mapper : Bs_ast_mapper.mapper =
7475
{ Bs_ast_mapper.default_mapper with
75-
expr = (fun self ({ pexp_loc = loc } as e) ->
76+
expr = (fun self e ->
7677
match e.pexp_desc with
7778
(** Its output should not be rewritten anymore *)
7879
| Pexp_extension extension ->
@@ -85,19 +86,7 @@ let rec unsafe_mapper : Bs_ast_mapper.mapper =
8586
#end
8687
(s, (Some delim)))
8788
->
88-
if Ext_string.equal delim Literals.unescaped_js_delimiter then
89-
let js_str = Ast_utf8_string.transform loc s in
90-
{ e with pexp_desc =
91-
Pexp_constant (
92-
#if OCAML_VERSION =~ ">4.03.0" then
93-
Pconst_string
94-
#else
95-
Const_string
96-
#end
97-
(js_str, Some Literals.escaped_j_delimiter))}
98-
else if Ext_string.equal delim Literals.unescaped_j_delimiter then
99-
Ast_utf8_string_interp.transform_interp loc s
100-
else e
89+
Ast_utf8_string_interp.transform e s delim
10190
(** End rewriting *)
10291
| Pexp_function cases ->
10392
(* {[ function [@bs.exn]
@@ -110,7 +99,7 @@ let rec unsafe_mapper : Bs_ast_mapper.mapper =
11099
| `Nothing, _ ->
111100
Bs_ast_mapper.default_mapper.expr self e
112101
| `Exn, pexp_attributes ->
113-
Ast_util.convertBsErrorFunction loc self pexp_attributes cases
102+
Ast_util.convertBsErrorFunction e.pexp_loc self pexp_attributes cases
114103
end
115104
| Pexp_fun (arg_label, _, pat , body)
116105
when Ast_compatible.is_arg_label_simple arg_label
@@ -121,13 +110,13 @@ let rec unsafe_mapper : Bs_ast_mapper.mapper =
121110
| Uncurry _, pexp_attributes
122111
->
123112
{e with
124-
pexp_desc = Ast_util.to_uncurry_fn loc self pat body ;
113+
pexp_desc = Ast_util.to_uncurry_fn e.pexp_loc self pat body ;
125114
pexp_attributes}
126115
| Method _ , _
127-
-> Location.raise_errorf ~loc "bs.meth is not supported in function expression"
116+
-> Location.raise_errorf ~loc:e.pexp_loc "bs.meth is not supported in function expression"
128117
| Meth_callback _, pexp_attributes
129118
->
130-
{e with pexp_desc = Ast_util.to_method_callback loc self pat body ;
119+
{e with pexp_desc = Ast_util.to_method_callback e.pexp_loc self pat body ;
131120
pexp_attributes }
132121
end
133122
| Pexp_apply (fn, args ) ->
@@ -138,7 +127,7 @@ let rec unsafe_mapper : Bs_ast_mapper.mapper =
138127
| None ->
139128
{ e with
140129
pexp_desc =
141-
Ast_util.record_as_js_object loc self label_exprs;
130+
Ast_util.record_as_js_object e.pexp_loc self label_exprs;
142131
}
143132
| Some e ->
144133
Location.raise_errorf
@@ -159,7 +148,7 @@ let rec unsafe_mapper : Bs_ast_mapper.mapper =
159148
{e with
160149
pexp_desc =
161150
Ast_util.ocaml_obj_as_js_object
162-
loc self pcstr_self pcstr_fields;
151+
e.pexp_loc self pcstr_self pcstr_fields;
163152
pexp_attributes
164153
}
165154
| `Nothing , _ ->
@@ -313,8 +302,8 @@ let rewrite_implementation : (Parsetree.structure -> Parsetree.structure) ref =
313302
| {pstr_desc = Pstr_attribute ({txt = "bs.config"; loc}, payload); _} :: rest
314303
->
315304
begin
316-
Ast_payload.ident_or_record_as_config loc payload
317-
|> List.iter (Ast_payload.table_dispatch structural_config_table) ;
305+
Ext_list.iter (Ast_payload.ident_or_record_as_config loc payload)
306+
(Ast_payload.table_dispatch structural_config_table) ;
318307
let rest = unsafe_mapper.structure unsafe_mapper rest in
319308
if !no_export then
320309
[Str.include_ ~loc

0 commit comments

Comments
 (0)