Skip to content

Commit 2745a3c

Browse files
authored
Fix module printing (#6963)
* Handle parens when mod type is a module signature * Refactor * Update CHANGELOG * Add tests * Handle more edge cases * Add more test cases from jscomp/test/coercion_module_alias_test.res
1 parent 4a1fbf3 commit 2745a3c

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#### :bug: Bug Fix
2727
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
2828
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
29+
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
2930

3031
# 12.0.0-alpha.1
3132

jscomp/syntax/src/res_parens.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,23 @@ let include_mod_expr mod_expr =
452452
| Parsetree.Pmod_constraint _ -> true
453453
| _ -> false
454454

455+
let mod_expr_parens mod_expr =
456+
match mod_expr with
457+
| {
458+
Parsetree.pmod_desc =
459+
Pmod_constraint
460+
( {Parsetree.pmod_desc = Pmod_structure _},
461+
{Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]} );
462+
} ->
463+
false
464+
| {
465+
Parsetree.pmod_desc =
466+
Pmod_constraint
467+
(_, {Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]});
468+
} ->
469+
true
470+
| _ -> false
471+
455472
let arrow_return_typ_expr typ_expr =
456473
match typ_expr.Parsetree.ptyp_desc with
457474
| Parsetree.Ptyp_arrow _ -> true

jscomp/syntax/src/res_parens.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ val call_expr : Parsetree.expression -> kind
3333

3434
val include_mod_expr : Parsetree.module_expr -> bool
3535

36+
val mod_expr_parens : Parsetree.module_expr -> bool
37+
3638
val arrow_return_typ_expr : Parsetree.core_type -> bool
3739

3840
val pattern_record_row_rhs : Parsetree.pattern -> bool

jscomp/syntax/src/res_printer.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ and print_module_binding ~state ~is_rec module_binding cmt_tbl i =
685685
Doc.concat [Doc.text ": "; print_mod_type ~state mod_type cmt_tbl] )
686686
| mod_expr -> (print_mod_expr ~state mod_expr cmt_tbl, Doc.nil)
687687
in
688+
let mod_expr_doc_parens =
689+
if Parens.mod_expr_parens module_binding.pmb_expr then
690+
Doc.concat [Doc.lparen; mod_expr_doc; Doc.rparen]
691+
else mod_expr_doc
692+
in
688693
let mod_name =
689694
let doc = Doc.text module_binding.pmb_name.Location.txt in
690695
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 =
698703
mod_name;
699704
mod_constraint_doc;
700705
Doc.text " = ";
701-
mod_expr_doc;
706+
mod_expr_doc_parens;
702707
]
703708
in
704709
print_comments doc cmt_tbl module_binding.pmb_loc

jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,49 @@ let g = {
2222
module M: T = {}
2323
0
2424
}
25+
26+
module M7: {
27+
module N': {
28+
let x: int
29+
}
30+
} = (M6: {
31+
module N: {
32+
let x: int
33+
}
34+
module N' = N
35+
})
36+
37+
module M8 = M7
38+
39+
module M5 = G0()
40+
41+
module M7: {
42+
let x: int
43+
} = {
44+
let x = 8
45+
}
46+
47+
module M3: {
48+
module N': {
49+
let x: int
50+
}
51+
} = {
52+
include M'
53+
}
54+
55+
module G0: (X: {}) =>
56+
{
57+
module N': {
58+
let x: int
59+
}
60+
} = F0
61+
62+
module M6 = {
63+
module D = {
64+
let y = 3
65+
}
66+
module N = {
67+
let x = 1
68+
}
69+
module N' = N
70+
}

jscomp/syntax/tests/printer/modExpr/structure.res

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,51 @@ module type T = {}
2121
let g = {
2222
module M: T = {}
2323
0
24-
}
24+
}
25+
26+
module M7: {
27+
module N': {
28+
let x: int
29+
}
30+
} = (M6: {
31+
module N: {
32+
let x: int
33+
}
34+
module N' = N
35+
})
36+
37+
38+
module M8 = M7
39+
40+
module M5 = G0()
41+
42+
module M7: {
43+
let x: int
44+
} = {
45+
let x = 8
46+
}
47+
48+
module M3: {
49+
module N': {
50+
let x: int
51+
}
52+
} = {
53+
include M'
54+
}
55+
56+
module G0: (X: {}) =>
57+
{
58+
module N': {
59+
let x: int
60+
}
61+
} = F0
62+
63+
module M6 = {
64+
module D = {
65+
let y = 3
66+
}
67+
module N = {
68+
let x = 1
69+
}
70+
module N' = N
71+
}

0 commit comments

Comments
 (0)