diff --git a/Changes.md b/Changes.md index 2d62056459..b378538ec1 100644 --- a/Changes.md +++ b/Changes.md @@ -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 --------------- diff --git a/flake.lock b/flake.lock index c18c78c556..d948507357 100644 --- a/flake.lock +++ b/flake.lock @@ -64,11 +64,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1682287386, - "narHash": "sha256-FKYZqW0GGRh5GFVpPlW/m7Xf0pCS/JKBflDTFCwDPpw=", + "lastModified": 1682455662, + "narHash": "sha256-8v3OUG5kjfDhuXBZFgdHAHqv741eZRro2WhKD7Odn/o=", "owner": "nix-ocaml", "repo": "nix-overlays", - "rev": "47c2c7838ebdcc434265744d4f198f5d03cecb03", + "rev": "d1bef8a898bdd5f348a67b6d766ea1fe9d1808fb", "type": "github" }, "original": { @@ -79,17 +79,17 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1682266827, - "narHash": "sha256-K+ZriFdFKMWbJ+IjJf3LJAKp3KePOvO76W2mzlxdsA4=", + "lastModified": 1682385804, + "narHash": "sha256-iK6k4jQ8jV3Lo79WkHQDugL/qTVqeynWXbdIcetZuMo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5d91a896449650d13fbba8c8abc65d1615c2b654", + "rev": "392218684aea34e48419ad57e690c6fd08aa3ea4", "type": "github" }, "original": { "owner": "NixOS", "repo": "nixpkgs", - "rev": "5d91a896449650d13fbba8c8abc65d1615c2b654", + "rev": "392218684aea34e48419ad57e690c6fd08aa3ea4", "type": "github" } }, diff --git a/jscomp/main/melc.ml b/jscomp/main/melc.ml index aea0e0ecaa..d2a4ad20a1 100644 --- a/jscomp/main/melc.ml +++ b/jscomp/main/melc.ml @@ -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 = diff --git a/test/as-ppx-config-attribute.t b/test/as-ppx-config-attribute.t new file mode 100644 index 0000000000..61710956ab --- /dev/null +++ b/test/as-ppx-config-attribute.t @@ -0,0 +1,18 @@ + + $ cat > foo.ml < [@@@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 */