Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't let comments in %mel.raw break JS output #1017

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ Unreleased
- Upgrade the Melange JS parser to [Flow
v0.225.1](https://github.com/facebook/flow/releases/tag/v0.225.1)
([#1012](https://github.com/melange-re/melange/pull/1012))
- fix: add a newline after `%mel.raw` expressions to avoid breaking JS output
when they contain single line comments
([#1017](https://github.com/melange-re/melange/pull/1017))

2.2.0 2023-12-05
---------------
Expand Down
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 jscomp/test/dist/jscomp/test/bs_hashtbl_string_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/bs_node_string_buffer_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/caml_compare_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/epsilon_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions jscomp/test/dist/jscomp/test/exception_raise_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/ffi_arity_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/ffi_js_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions jscomp/test/dist/jscomp/test/flow_parser_reg_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions jscomp/test/dist/jscomp/test/gpr_2682_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/gpr_4442_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/polymorphic_raw_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/raw_output_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions jscomp/test/dist/jscomp/test/string_interp_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion jscomp/test/dist/jscomp/test/unsafe_ppx_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/blackbox-tests/melange-ppx-raw.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@ Adding a callback breaks
^^^^^^^^^^^^^^^^^^^^^^
Error: `%mel.raw' can only be applied to a string
[1]

Show expressions in `%mel.raw` get wrapped in parentheses with proper syntax

$ cat > main.ml <<EOF
> let () = [%mel.raw {|
> // before
> f(1,2)
> // after
> |}]
> EOF
$ melc -ppx melppx main.ml
// Generated by Melange
'use strict';


((// before
f(1,2)
// after
));

/* Not a pure module */
Loading