Skip to content

Commit

Permalink
feat: support @mel.as renaming for modules (#879)
Browse files Browse the repository at this point in the history
* test: show that modules can't be renamed in the output

* feat: support `@mel.as` renaming for modules

* chore: add changelog entry
  • Loading branch information
anmonteiro authored Nov 10, 2023
1 parent 10bf73a commit 9f09d2c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Unreleased
([#852](https://github.com/melange-re/melange/pull/852))
- Fix crash when pattern matching in the presence of complex constant inlining
([#871](https://github.com/melange-re/melange/pull/871))
- Support renaming modules in the output JS with `@mel.as`
([#879](https://github.com/melange-re/melange/pull/879))

2.1.0 2023-10-22
---------------
Expand Down
44 changes: 44 additions & 0 deletions ppx/melange_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,28 @@ module Mapper = struct
({ attr_name = { txt = "mel.config" | "config"; _ }; _ } as attr) ->
Mel_ast_invariant.mark_used_mel_attribute attr;
str
| Pstr_module
({
pmb_name = { txt = Some _; loc = pmb_name_loc } as pmb_name_orig;
pmb_attributes;
pmb_loc;
_;
} as mb) ->
let pmb_name =
match Ast_attributes.has_mel_as_payload pmb_attributes with
| Some ({ attr_payload; _ } as attr) ->
Mel_ast_invariant.mark_used_mel_attribute attr;
{
txt =
Some
(Ast_payload.extract_mel_as_ident ~loc:pmb_loc
attr_payload);
loc = pmb_name_loc;
}
| None -> pmb_name_orig
in
super#structure_item
{ str with pstr_desc = Pstr_module { mb with pmb_name } }
| _ -> super#structure_item str

method! signature_item sigi =
Expand Down Expand Up @@ -855,6 +877,28 @@ module Mapper = struct
({ attr_name = { txt = "mel.config" | "config"; _ }; _ } as attr) ->
Mel_ast_invariant.mark_used_mel_attribute attr;
sigi
| Psig_module
({
pmd_name = { txt = Some _; loc = pmd_name_loc } as pmd_name_orig;
pmd_attributes;
pmd_loc;
_;
} as md) ->
let pmd_name =
match Ast_attributes.has_mel_as_payload pmd_attributes with
| Some ({ attr_payload; _ } as attr) ->
Mel_ast_invariant.mark_used_mel_attribute attr;
{
txt =
Some
(Ast_payload.extract_mel_as_ident ~loc:pmd_loc
attr_payload);
loc = pmd_name_loc;
}
| None -> pmd_name_orig
in
super#signature_item
{ sigi with psig_desc = Psig_module { md with pmd_name } }
| _ -> super#signature_item sigi
end
end
Expand Down
55 changes: 55 additions & 0 deletions test/blackbox-tests/mel-as-modules.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Use `[@mel.as]` to change how module output name in resulting JS

$ . ./setup.sh
$ cat > dune-project <<EOF
> (lang dune 3.8)
> (using melange 0.1)
> EOF
$ cat > dune <<EOF
> (melange.emit
> (emit_stdlib false)
> (preprocess (pps melange.ppx))
> (target js-out))
> EOF
$ cat > x.ml <<EOF
> module[@mel.as "Obj"] Object = struct end
> EOF
$ dune build @melange
$ cat _build/default/js-out/x.js
// Generated by Melange
'use strict';
var Obj = {};
exports.Obj = Obj;
/* No side effect */
Also applies to signatures
$ dune clean
$ cat > x.ml <<EOF
> module[@mel.as "Obj2"] Object = struct end
> EOF
$ cat > x.mli <<EOF
> module[@mel.as "Obj2"] Object : sig end
> EOF
$ dune build @melange
$ cat _build/default/js-out/x.js
// Generated by Melange
'use strict';
var Obj2 = {};
exports.Obj2 = Obj2;
/* No side effect */

0 comments on commit 9f09d2c

Please sign in to comment.