diff --git a/CHANGES.md b/CHANGES.md index 92a18868d..6be902d35 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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, diff --git a/flake.lock b/flake.lock index d4f897541..43e4fadc6 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1692799911, - "narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=", + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", "owner": "numtide", "repo": "flake-utils", - "rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", "type": "github" }, "original": { @@ -20,11 +20,11 @@ }, "nix-filter": { "locked": { - "lastModified": 1693833173, - "narHash": "sha256-hlMABKrGbEiJD5dwUSfnw1CQ3bG7KKwDV+Nx3bEZd7U=", + "lastModified": 1694857738, + "narHash": "sha256-bxxNyLHjhu0N8T3REINXQ2ZkJco0ABFPn6PIe2QUfqo=", "owner": "numtide", "repo": "nix-filter", - "rev": "ac030bd9ba98e318e1f4c4328d60766ade8ebe8b", + "rev": "41fd48e00c22b4ced525af521ead8792402de0ea", "type": "github" }, "original": { @@ -41,11 +41,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1694321661, - "narHash": "sha256-x7eYMZrseg9Mr31T8JdJpWP0nBPnRFZF+CMZyr1BoQU=", + "lastModified": 1695149910, + "narHash": "sha256-rzpTFXX0yVO02Y2l9J+9zcnHEEWJ0/uKB74aBoW8PUs=", "owner": "nix-ocaml", "repo": "nix-overlays", - "rev": "71ddf5eb18018ef1a5e27f34af5df62a0fd68622", + "rev": "c9e78a97c6dc5ce6ec9a96aaabe74e21f2a485d8", "type": "github" }, "original": { @@ -56,17 +56,17 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1694254102, - "narHash": "sha256-oM2qC5TyeM1zZE3BQnOodg31a2u/G5DF/Yx02UJRGfc=", + "lastModified": 1695043561, + "narHash": "sha256-ajrDIUJA5RB6Y2I1G4suDhiDMJuwg1WarNuasshRobE=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "4a76638020344ae8a9f061856431182179ab0d28", + "rev": "089313d7c7c864b21648d78fb8700062dafab1f2", "type": "github" }, "original": { "owner": "NixOS", "repo": "nixpkgs", - "rev": "4a76638020344ae8a9f061856431182179ab0d28", + "rev": "089313d7c7c864b21648d78fb8700062dafab1f2", "type": "github" } }, diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index 3874ccfe4..4784535d6 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=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 @@ -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 @@ -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) diff --git a/test/extension-str-in-module.t b/test/extension-str-in-module.t new file mode 100644 index 000000000..87d40152f --- /dev/null +++ b/test/extension-str-in-module.t @@ -0,0 +1,17 @@ +Format extensions in modules + + $ refmt < [%%toplevelExtension "payload"]; + > module X = { + > /* No payload */ + > [%%someExtension]; + > [%%someExtension "payload"]; + > }; + > EOF + [%%toplevelExtension "payload"]; + module X = { + /* No payload */ + [%%someExtension]; + [%%someExtension "payload"]; + }; +