diff --git a/CHANGELOG.md b/CHANGELOG.md index 76551587577..7bdae257bfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ #### :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 - Preserve multibyte characters when wrapping long source lines in compiler code frames. https://github.com/rescript-lang/rescript/pull/8520 #### :memo: Documentation diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index da48b02561e..56894aba1c5 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/gentype_tests/typescript-react-example/src/ErrorHandler.resi b/tests/gentype_tests/typescript-react-example/src/ErrorHandler.resi index fac11f7511b..d20c3654e24 100644 --- a/tests/gentype_tests/typescript-react-example/src/ErrorHandler.resi +++ b/tests/gentype_tests/typescript-react-example/src/ErrorHandler.resi @@ -2,8 +2,7 @@ module type Error = { type t let notification: t => (string, string) } -module Make: (Error: Error) => -{ +module Make: (Error: Error) => { let notify: Error.t => (string, string) } 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 diff --git a/tests/tests/src/coercion_module_alias_test.res b/tests/tests/src/coercion_module_alias_test.res index 3801348a0d9..5bae073378d 100644 --- a/tests/tests/src/coercion_module_alias_test.res +++ b/tests/tests/src/coercion_module_alias_test.res @@ -84,8 +84,7 @@ module F0 = (X: {}) => { } module N' = N } -module G0: (X: {}) => -{ +module G0: (X: {}) => { module N': { let x: int } diff --git a/tests/tests/src/inline_const.resi b/tests/tests/src/inline_const.resi index e1698148a69..cfa1f2ab5d6 100644 --- a/tests/tests/src/inline_const.resi +++ b/tests/tests/src/inline_const.resi @@ -10,8 +10,7 @@ module N: { @inline(`中文`) let f3: string } -module N1: () => -{ +module N1: () => { @inline(`中文`) let f4: string @inline(3e-6) let xx: float }