diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac5645a55..62e5ce9631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ #### :bug: Bug Fix - Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949 - Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953 +- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963 # 12.0.0-alpha.1 diff --git a/jscomp/syntax/src/res_parens.ml b/jscomp/syntax/src/res_parens.ml index b100933e86..336af9f77f 100644 --- a/jscomp/syntax/src/res_parens.ml +++ b/jscomp/syntax/src/res_parens.ml @@ -452,6 +452,23 @@ let include_mod_expr mod_expr = | Parsetree.Pmod_constraint _ -> true | _ -> false +let mod_expr_parens mod_expr = + match mod_expr with + | { + Parsetree.pmod_desc = + Pmod_constraint + ( {Parsetree.pmod_desc = Pmod_structure _}, + {Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]} ); + } -> + false + | { + Parsetree.pmod_desc = + Pmod_constraint + (_, {Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]}); + } -> + true + | _ -> false + let arrow_return_typ_expr typ_expr = match typ_expr.Parsetree.ptyp_desc with | Parsetree.Ptyp_arrow _ -> true diff --git a/jscomp/syntax/src/res_parens.mli b/jscomp/syntax/src/res_parens.mli index 28e35a6345..7c56c0ab56 100644 --- a/jscomp/syntax/src/res_parens.mli +++ b/jscomp/syntax/src/res_parens.mli @@ -33,6 +33,8 @@ val call_expr : Parsetree.expression -> kind val include_mod_expr : Parsetree.module_expr -> bool +val mod_expr_parens : Parsetree.module_expr -> bool + val arrow_return_typ_expr : Parsetree.core_type -> bool val pattern_record_row_rhs : Parsetree.pattern -> bool diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index ae8055d7b5..7b673eddac 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -685,6 +685,11 @@ and print_module_binding ~state ~is_rec module_binding cmt_tbl i = Doc.concat [Doc.text ": "; print_mod_type ~state mod_type cmt_tbl] ) | mod_expr -> (print_mod_expr ~state mod_expr cmt_tbl, Doc.nil) in + let mod_expr_doc_parens = + if Parens.mod_expr_parens module_binding.pmb_expr then + Doc.concat [Doc.lparen; mod_expr_doc; Doc.rparen] + else mod_expr_doc + in let mod_name = let doc = Doc.text module_binding.pmb_name.Location.txt in print_comments doc cmt_tbl module_binding.pmb_name.loc @@ -698,7 +703,7 @@ and print_module_binding ~state ~is_rec module_binding cmt_tbl i = mod_name; mod_constraint_doc; Doc.text " = "; - mod_expr_doc; + mod_expr_doc_parens; ] in print_comments doc cmt_tbl module_binding.pmb_loc diff --git a/jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt b/jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt index b5016cf426..a958790e76 100644 --- a/jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt +++ b/jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt @@ -22,3 +22,49 @@ let g = { module M: T = {} 0 } + +module M7: { + module N': { + let x: int + } +} = (M6: { + module N: { + let x: int + } + module N' = N +}) + +module M8 = M7 + +module M5 = G0() + +module M7: { + let x: int +} = { + let x = 8 +} + +module M3: { + module N': { + let x: int + } +} = { + include M' +} + +module G0: (X: {}) => +{ + module N': { + let x: int + } +} = F0 + +module M6 = { + module D = { + let y = 3 + } + module N = { + let x = 1 + } + module N' = N +} diff --git a/jscomp/syntax/tests/printer/modExpr/structure.res b/jscomp/syntax/tests/printer/modExpr/structure.res index a0bd2ff7c4..86a882d62d 100644 --- a/jscomp/syntax/tests/printer/modExpr/structure.res +++ b/jscomp/syntax/tests/printer/modExpr/structure.res @@ -21,4 +21,51 @@ module type T = {} let g = { module M: T = {} 0 -} \ No newline at end of file +} + +module M7: { + module N': { + let x: int + } +} = (M6: { + module N: { + let x: int + } + module N' = N +}) + + +module M8 = M7 + +module M5 = G0() + +module M7: { + let x: int +} = { + let x = 8 +} + +module M3: { + module N': { + let x: int + } +} = { + include M' +} + +module G0: (X: {}) => +{ + module N': { + let x: int + } +} = F0 + +module M6 = { + module D = { + let y = 3 + } + module N = { + let x = 1 + } + module N' = N +}