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

test: interaction between [@@@bs.config {flags = ...}] and --as-ppx #554

Merged
merged 2 commits into from
Apr 27, 2023
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 @@ -56,6 +56,9 @@ Unreleased
- melange: allow installing melange in more OCaml versions and compiler
switches. Melange now migrates binary AST to the version it understands
([#548](https://github.com/melange-re/melange/pull/548))
- melange: don't run anonymous args function from
`[@@@bs.config {flags = [| ... |]}]` attributes
([#554](https://github.com/melange-re/melange/pull/554))

0.3.2 2022-11-19
---------------
Expand Down
14 changes: 7 additions & 7 deletions flake.lock

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

64 changes: 37 additions & 27 deletions jscomp/main/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,44 @@ let process_file sourcefile

let ppf = Format.err_formatter

let anonymous ~(rev_args : string list) =
if !Js_config.as_ppx then
match rev_args with
| [output; input] ->
`Ok (Melange_ppx_lib.Ppx_apply.apply_lazy
~source:input
~target:output
Melange_ppx_lib.Ppx_entry.rewrite_implementation
Melange_ppx_lib.Ppx_entry.rewrite_signature)
| _ -> `Error(false, "`--as-ppx` requires 2 arguments: `melc --as-ppx input output`")
else
begin
if !Js_config.syntax_only then begin
Ext_list.rev_iter rev_args (fun filename ->
begin
(* Clflags.reset_dump_state (); *)
(* Warnings.reset (); *)
process_file filename ppf
end );
`Ok ()
end else
let anonymous =
let executed = ref false in
fun ~(rev_args : string list) ->
match !executed with
| true ->
(* Don't re-run anonymous arguments again if this function has already
* been executed. If this code is executing the 2nd time, it's coming
* from a [@bs.config { flags = [| ... |] }].. *)
`Ok ()
| false ->
executed := true;
if !Js_config.as_ppx then
match rev_args with
| [output; input] ->
`Ok (Melange_ppx_lib.Ppx_apply.apply_lazy
~source:input
~target:output
Melange_ppx_lib.Ppx_entry.rewrite_implementation
Melange_ppx_lib.Ppx_entry.rewrite_signature)
| _ -> `Error(false, "`--as-ppx` requires 2 arguments: `melc --as-ppx input output`")
else
begin
if !Js_config.syntax_only then begin
Ext_list.rev_iter rev_args (fun filename ->
begin
(* Clflags.reset_dump_state (); *)
(* Warnings.reset (); *)
process_file filename ppf
end );
`Ok ()
end else

match rev_args with
| [filename] -> `Ok (process_file filename ppf)
| [] -> `Ok ()
| _ ->
`Error (false, "can not handle multiple files")
end
match rev_args with
| [filename] -> `Ok (process_file filename ppf)
| [] -> `Ok ()
| _ ->
`Error (false, "can not handle multiple files")
end

(** used by -impl -intf *)
let impl filename =
Expand Down
18 changes: 18 additions & 0 deletions test/as-ppx-config-attribute.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

$ cat > foo.ml <<EOF
> [@@@bs.config {flags = [| "-unsafe"; "-nopervasives" |] }]
> let x = 1
> EOF

$ melc foo.ml -as-pp foo.ml > foo.pp.ml
$ melc --as-ppx foo.pp.ml bar.pp.ml

$ melc ./bar.pp.ml
// Generated by Melange
'use strict';


var x = 1;

exports.x = x;
/* No side effect */