Skip to content

Commit 75e124b

Browse files
authored
Merge pull request #2686 from chenglou/hah
Make JSX PPX also support unicode
2 parents f0ceddc + 5991cd1 commit 75e124b

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

jscomp/bin/reactjs_jsx_ppx_v2.bspp.ml

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -311,40 +311,6 @@ let jsxMapper () =
311311
#end
312312

313313
#if BS_COMPILER_IN_BROWSER then
314-
let rewrite code =
315-
let mapper = jsxMapper () in
316-
Location.input_name := "//toplevel//";
317-
try
318-
let lexer = Lexing.from_string code in
319-
let pstr = Parse.implementation lexer in
320-
let pstr = mapper.structure mapper pstr in
321-
Pprintast.structure Format.str_formatter pstr;
322-
let ocaml_code = Format.flush_str_formatter () in
323-
Printf.sprintf "{\"ocaml_code\": %S}" ocaml_code
324-
with e ->
325-
match Location.error_of_exn e with
326-
| Some error ->
327-
let (file, line, startchar) =
328-
Location.get_pos_info error.loc.loc_start in
329-
let (file, endline, endchar) =
330-
Location.get_pos_info error.loc.loc_end in
331-
let result =
332-
Printf.sprintf
333-
"{ \
334-
\"ppx_error_msg\": %S, \
335-
\"row\": %d, \
336-
\"column\": %d, \
337-
\"endRow\": %d, \
338-
\"endColumn\": %d, \
339-
\"text\": %S, \
340-
\"type\": \"error\" \
341-
}"
342-
(Printf.sprintf "Line %d, %d: %s" line startchar error.msg)
343-
(line - 1) startchar (endline - 1) endchar error.msg in
344-
Location.report_error Format.std_formatter error;
345-
result
346-
| None ->
347-
Printf.sprintf "{\"js_error_msg\": %S}" (Printexc.to_string e)
348314

349315
module Js = struct
350316
module Unsafe = struct
@@ -365,6 +331,40 @@ module Js = struct
365331
external to_string : js_string t -> string = "caml_js_to_string"
366332
end
367333

334+
(* keep in sync with jscomp/core/jsoo_main.ml `let implementation` *)
335+
let rewrite code =
336+
let mapper = jsxMapper () in
337+
Location.input_name := "//toplevel//";
338+
try
339+
let lexer = Lexing.from_string code in
340+
let pstr = Parse.implementation lexer in
341+
let pstr = mapper.structure mapper pstr in
342+
let buffer = Buffer.create 1000 in
343+
Pprintast.structure Format.str_formatter pstr;
344+
let ocaml_code = Format.flush_str_formatter () in
345+
Js.Unsafe.(obj [| "ocaml_code", inject @@ Js.string ocaml_code |])
346+
with e ->
347+
match Location.error_of_exn e with
348+
| Some error ->
349+
Location.report_error Format.std_formatter error;
350+
let (file, line, startchar) = Location.get_pos_info error.loc.loc_start in
351+
let (file, endline, endchar) = Location.get_pos_info error.loc.loc_end in
352+
Js.Unsafe.(obj
353+
[|
354+
"ppx_error_msg", inject @@ Js.string (Printf.sprintf "Line %d, %d: %s" line startchar error.msg);
355+
"row", inject (line - 1);
356+
"column", inject startchar;
357+
"endRow", inject (endline - 1);
358+
"endColumn", inject endchar;
359+
"text", inject @@ Js.string error.msg;
360+
"type", inject @@ Js.string "error";
361+
|]
362+
)
363+
| None ->
364+
Js.Unsafe.(obj [|
365+
"js_error_msg" , inject @@ Js.string (Printexc.to_string e)
366+
|])
367+
368368
let export (field : string) v =
369369
Js.Unsafe.set (Js.Unsafe.global) field v
370370

@@ -374,9 +374,9 @@ let make_ppx name =
374374
[|"rewrite",
375375
inject @@
376376
Js.wrap_meth_callback
377-
(fun _ code ->
378-
Js.string (rewrite (Js.to_string code)));
377+
(fun _ code -> rewrite (Js.to_string code));
379378
|]))
379+
380380
let () = make_ppx "jsxv2"
381381
#elif defined BS_NO_COMPILER_PATCH then
382382
let () = Compiler_libs.Ast_mapper.register "JSX" (fun _argv -> jsxMapper ())

jscomp/repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var cmi_files =
8888
`moreLabels`, `queue`, `scanf`, `sort`,`stack`, `stdLabels`, `stream`,
8989
`stringLabels`,
9090

91-
`belt`,
91+
`belt`,
9292
`belt_Id`,
9393
`belt_Array`,
9494
`belt_SortArray`,

0 commit comments

Comments
 (0)