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: reproduce extension bug when nested inside a module #2723

Merged
merged 3 commits into from
Sep 20, 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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

- Print structure items extension nodes correctly inside modules (@anmonteiro,
[#2723](https://github.com/reasonml/reason/pull/2723))

## 3.10.0

- Support `@mel.*` attributes in addition to `@bs.*` (@anmonteiro,
Expand Down
26 changes: 13 additions & 13 deletions flake.lock

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

32 changes: 10 additions & 22 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5570,11 +5570,11 @@ let printer = object(self:'self)
* brace {} in the let sequence. *)
let layout = source_map ~loc:letModuleLoc letModuleLayout in
let (_, return) = self#curriedFunctorPatternsAndReturnStruct moduleExpr in
let loc = {
letModuleLoc with
loc_end = return.pmod_loc.loc_end
} in
processLetList ((loc, layout)::acc) e
let loc = {
letModuleLoc with
loc_end = return.pmod_loc.loc_end
} in
processLetList ((loc, layout)::acc) e
| ([], Pexp_letexception (extensionConstructor, expr)) ->
let exc = self#exception_declaration extensionConstructor in
let layout = source_map ~loc:extensionConstructor.pext_loc exc in
Expand Down Expand Up @@ -7548,20 +7548,7 @@ let printer = object(self:'self)
else
("({", "})")
else ("{", "}") in
let items =
groupAndPrint
~xf:self#structure_item
~getLoc:(fun x -> x.pstr_loc)
~comments:self#comments
s
in
makeList
~break:Layout.Always_rec
~inline:(true, false)
~wrap
~postSpace:true
~sep:(SepFinal (";", ";"))
items
self#structure ~indent:None ~wrap s
| _ ->
(* For example, functor application will be wrapped. *)
formatPrecedence ~wrap:("", "") (self#module_expr x)
Expand All @@ -7582,7 +7569,7 @@ let printer = object(self:'self)
| Pmod_constraint _
| Pmod_structure _ -> self#simple_module_expr x

method structure structureItems =
method structure ?(indent=Some 0) ?wrap structureItems =
(* We don't have any way to know if an extension is placed at the top level by the parsetree
while there's a difference syntactically (% for structure_items/expressons and %% for top_level).
This small fn detects this particular case (structure > structure_item > extension > value) and
Expand All @@ -7598,7 +7585,7 @@ let printer = object(self:'self)
| _ -> self#structure_item item
in
match structureItems with
| [] -> atom ""
| [] -> makeList ?wrap []
| first :: _ as structureItems ->
let last = match (List.rev structureItems) with | last::_ -> last | [] -> assert false in
let loc_start = first.pstr_loc.loc_start in
Expand All @@ -7614,7 +7601,8 @@ let printer = object(self:'self)
(makeList
~postSpace:true
~break:Always_rec
~indent:0
?wrap
?indent
~inline:(true, false)
~sep:(SepFinal (";", ";"))
items)
Expand Down
17 changes: 17 additions & 0 deletions test/extension-str-in-module.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Format extensions in modules

$ refmt <<EOF
> [%%toplevelExtension "payload"];
> module X = {
> /* No payload */
> [%%someExtension];
> [%%someExtension "payload"];
> };
> EOF
[%%toplevelExtension "payload"];
module X = {
/* No payload */
[%%someExtension];
[%%someExtension "payload"];
};

Loading