Skip to content

Commit

Permalink
fix: don't let comments in %mel.raw break JS output
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Jan 4, 2024
1 parent 6e69fd1 commit 3e673a6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 1 addition & 6 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> (
Expand Down
3 changes: 2 additions & 1 deletion test/blackbox-tests/melange-ppx-raw.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 3e673a6

Please sign in to comment.