Skip to content

Commit

Permalink
fix: structure item extensions in nested modules
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Sep 19, 2023
1 parent 81097f5 commit 178c87c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
36 changes: 12 additions & 24 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 ?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 [ atom "" ]
| 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 Expand Up @@ -8232,7 +8220,7 @@ end;;
let toplevel_phrase ppf x =
match x with
| Ptop_def s -> format_layout ppf (printer#structure s)
| Ptop_def s -> format_layout ppf (printer#structure ~indent:(Some 0) s)
| Ptop_dir _ -> print_string "(* top directives not supported *)"
let case_list ppf x =
Expand Down Expand Up @@ -8364,7 +8352,7 @@ let signature (comments : Comment.t list) ppf x =
let structure (comments : Comment.t list) ppf x =
List.iter (fun comment -> printer#trackComment comment) comments;
format_layout ppf ~comments
(printer#structure
(printer#structure ~indent:(Some 0)
(preprocessing_mapper apply_mapper_to_structure x))
let expression ppf x =
Expand Down
2 changes: 1 addition & 1 deletion test/extension-str-in-module.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Format extensions in modules
module X = {
/* No payload */
[%%someExtension];
[%someExtension "payload"];
[%%someExtension "payload"];
};

0 comments on commit 178c87c

Please sign in to comment.