From 178c87caaff364dc3780f4055bd1f63236a47c4a Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Tue, 19 Sep 2023 14:10:36 -0700 Subject: [PATCH] fix: structure item extensions in nested modules --- src/reason-parser/reason_pprint_ast.ml | 36 +++++++++----------------- test/extension-str-in-module.t | 2 +- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index 3874ccfe4..16ad40458 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 = @@ -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 = diff --git a/test/extension-str-in-module.t b/test/extension-str-in-module.t index f1c7a0284..87d40152f 100644 --- a/test/extension-str-in-module.t +++ b/test/extension-str-in-module.t @@ -12,6 +12,6 @@ Format extensions in modules module X = { /* No payload */ [%%someExtension]; - [%someExtension "payload"]; + [%%someExtension "payload"]; };