diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c6f8242005..f8422ef6e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ #### :bug: Bug fix +- Fix formatter breaking the opening brace of a functor module type's result signature onto a new line (e.g. `module Make: Pattern => {`). https://github.com/rescript-lang/rescript/pull/8519 + #### :memo: Documentation #### :nail_care: Polish diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index dd15d0bb630..a56837fb1b0 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -931,11 +931,19 @@ and print_mod_type ~state mod_type cmt_tbl = if Parens.mod_type_functor_return return_type then add_parens doc else doc in + (* When the functor result is a signature, keep its opening brace on the + same line as `=>` instead of breaking onto a new line, mirroring how + module-expression functors are printed (see print_mod_functor). *) + let arrow_sep = + match return_type.pmty_desc with + | Pmty_signature _ -> Doc.space + | _ -> Doc.line + in Doc.group (Doc.concat [ parameters_doc; - Doc.group (Doc.concat [Doc.text " =>"; Doc.line; return_doc]); + Doc.group (Doc.concat [Doc.text " =>"; arrow_sep; return_doc]); ]) | Pmty_typeof mod_expr -> Doc.concat diff --git a/tests/syntax_tests/data/printer/modExpr/expected/structure.res.txt b/tests/syntax_tests/data/printer/modExpr/expected/structure.res.txt index a958790e769..14e1ecd1f67 100644 --- a/tests/syntax_tests/data/printer/modExpr/expected/structure.res.txt +++ b/tests/syntax_tests/data/printer/modExpr/expected/structure.res.txt @@ -52,8 +52,7 @@ module M3: { include M' } -module G0: (X: {}) => -{ +module G0: (X: {}) => { module N': { let x: int } diff --git a/tests/syntax_tests/data/printer/modType/expected/functor.res.txt b/tests/syntax_tests/data/printer/modType/expected/functor.res.txt index 311a32c474a..2af4bf0c652 100644 --- a/tests/syntax_tests/data/printer/modType/expected/functor.res.txt +++ b/tests/syntax_tests/data/printer/modType/expected/functor.res.txt @@ -13,7 +13,17 @@ module type Functor = (@attr1 SetLike, @attr2 BtreeLike) => @attr3 NeoTree module type Functor = (SetLike => Set) with type t = A.t module type Functor = SetLike => (Set with type t = A.t) -module type B = () => -{ +module type B = () => { } + +module type ThisIsAVeryLongFunctorModuleTypeNameThatExceedsTheEightyCharacterLimit = Pattern => { + let fmt: event => string +} + +module type SecondVeryLongFunctorModuleTypeNameWithMultipleShortParameters = (A, B) => { + let x: int +} + +module type YetAnotherVeryLongFunctorModuleTypeNameHere = SomeVeryLongParameterModuleTypeName => +SomeVeryLongResultingModuleTypeName diff --git a/tests/syntax_tests/data/printer/modType/expected/functorInterface.resi.txt b/tests/syntax_tests/data/printer/modType/expected/functorInterface.resi.txt new file mode 100644 index 00000000000..8dcc62c8735 --- /dev/null +++ b/tests/syntax_tests/data/printer/modType/expected/functorInterface.resi.txt @@ -0,0 +1,28 @@ +module Make: Pattern => { + let fmt: event => string +} + +module Curried: (A, B) => { + let x: int +} + +module Nested: (A, B) => { + let y: int +} + +module Empty: () => {} + +module ResultIdent: A => B + +module ResultWith: A => (B with type t = int) + +module ThisIsAVeryLongFunctorModuleTypeNameThatExceedsTheEightyCharacterLimit: Pattern => { + let fmt: event => string +} + +module SecondVeryLongFunctorModuleTypeNameWithMultipleShortParameters: (A, B) => { + let x: int +} + +module YetAnotherVeryLongFunctorModuleTypeNameHere: SomeVeryLongParameterModuleTypeName => +SomeVeryLongResultingModuleTypeName diff --git a/tests/syntax_tests/data/printer/modType/functor.res b/tests/syntax_tests/data/printer/modType/functor.res index 9149ed582c6..cea142cee36 100644 --- a/tests/syntax_tests/data/printer/modType/functor.res +++ b/tests/syntax_tests/data/printer/modType/functor.res @@ -16,4 +16,14 @@ module type Functor = SetLike => (Set with type t = A.t) module type B = () => { -} \ No newline at end of file +} + +module type ThisIsAVeryLongFunctorModuleTypeNameThatExceedsTheEightyCharacterLimit = Pattern => { + let fmt: event => string +} + +module type SecondVeryLongFunctorModuleTypeNameWithMultipleShortParameters = (A, B) => { + let x: int +} + +module type YetAnotherVeryLongFunctorModuleTypeNameHere = SomeVeryLongParameterModuleTypeName => SomeVeryLongResultingModuleTypeName \ No newline at end of file diff --git a/tests/syntax_tests/data/printer/modType/functorInterface.resi b/tests/syntax_tests/data/printer/modType/functorInterface.resi new file mode 100644 index 00000000000..07a8aa5a108 --- /dev/null +++ b/tests/syntax_tests/data/printer/modType/functorInterface.resi @@ -0,0 +1,27 @@ +module Make: Pattern => { + let fmt: event => string +} + +module Curried: (A, B) => { + let x: int +} + +module Nested: A => B => { + let y: int +} + +module Empty: () => {} + +module ResultIdent: A => B + +module ResultWith: A => (B with type t = int) + +module ThisIsAVeryLongFunctorModuleTypeNameThatExceedsTheEightyCharacterLimit: Pattern => { + let fmt: event => string +} + +module SecondVeryLongFunctorModuleTypeNameWithMultipleShortParameters: (A, B) => { + let x: int +} + +module YetAnotherVeryLongFunctorModuleTypeNameHere: SomeVeryLongParameterModuleTypeName => SomeVeryLongResultingModuleTypeName