From 3e673a66242af9176f9859e2f633f95c0dba0e01 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 3 Jan 2024 18:39:53 -0800 Subject: [PATCH] fix: don't let comments in `%mel.raw` break JS output --- jscomp/core/js_dump.ml | 4 +++- jscomp/core/js_exp_make.ml | 10 +++++++++- jscomp/core/lam_compile_primitive.ml | 7 +------ test/blackbox-tests/melange-ppx-raw.t | 3 ++- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index e6cb13b792..58153629eb 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -663,7 +663,9 @@ and expression_desc cxt ~(level : int) x : cxt = in if raw_paren then string cxt L.lparen; string cxt s; - if raw_paren then string cxt L.rparen; + if raw_paren then ( + newline cxt; + string cxt L.rparen); cxt | Stmt stmt_info -> if stmt_info = Js_stmt_comment then string cxt s diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 42263a11b8..cd8ca59c39 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -139,7 +139,15 @@ let unicode ?loc ?comment s : t = make_expression ?loc ?comment (Unicode s) let raw_js_code ?loc ?comment info s : t = make_expression ?loc ?comment - (Raw_js_code { code = String.trim s; code_info = info }) + (Raw_js_code + { + code = + (* FIXME: save one allocation + trim can not be done before syntax checking + otherwise location is incorrect *) + String.trim s; + code_info = info; + }) let array ?loc ?comment mt es : t = make_expression ?loc ?comment (Array (es, mt)) diff --git a/jscomp/core/lam_compile_primitive.ml b/jscomp/core/lam_compile_primitive.ml index 9f809266d3..1f17858fba 100644 --- a/jscomp/core/lam_compile_primitive.ml +++ b/jscomp/core/lam_compile_primitive.ml @@ -44,12 +44,7 @@ let translate loc (cxt : Lam_compile_context.t) (prim : Lam_primitive.t) | Pwrap_exn -> E.runtime_call Js_runtime_modules.caml_js_exceptions "internalToOCamlException" args - | Praw_js_code { code; code_info } -> - E.raw_js_code code_info code - (* FIXME: save one allocation - trim can not be done before syntax checking - otherwise location is incorrect - *) + | Praw_js_code { code; code_info } -> E.raw_js_code code_info code | Pjs_runtime_apply -> ( match args with [ f; args ] -> E.flat_call f args | _ -> assert false) | Pjs_apply -> ( diff --git a/test/blackbox-tests/melange-ppx-raw.t b/test/blackbox-tests/melange-ppx-raw.t index 4919ed5a9e..7f7868c669 100644 --- a/test/blackbox-tests/melange-ppx-raw.t +++ b/test/blackbox-tests/melange-ppx-raw.t @@ -48,6 +48,7 @@ Show expressions in `%mel.raw` get wrapped in parentheses with proper syntax ((// before f(1,2) - // after)); + // after + )); /* Not a pure module */