Skip to content

Commit 3ed3dec

Browse files
shulhicknitt
authored andcommittedSep 10, 2024
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 680d0ad commit 3ed3dec

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
1616
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
17+
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
1718

1819
# 11.1.3
1920

‎jscomp/syntax/src/res_parens.ml

+17
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,23 @@ let includeModExpr modExpr =
450450
| Parsetree.Pmod_constraint _ -> true
451451
| _ -> false
452452

453+
let modExprParens modExpr =
454+
match modExpr with
455+
| {
456+
Parsetree.pmod_desc =
457+
Pmod_constraint
458+
( {Parsetree.pmod_desc = Pmod_structure _},
459+
{Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]} );
460+
} ->
461+
false
462+
| {
463+
Parsetree.pmod_desc =
464+
Pmod_constraint
465+
(_, {Parsetree.pmty_desc = Pmty_signature [{psig_desc = Psig_module _}]});
466+
} ->
467+
true
468+
| _ -> false
469+
453470
let arrowReturnTypExpr typExpr =
454471
match typExpr.Parsetree.ptyp_desc with
455472
| Parsetree.Ptyp_arrow _ -> true

‎jscomp/syntax/src/res_parens.mli

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ val callExpr : Parsetree.expression -> kind
3232

3333
val includeModExpr : Parsetree.module_expr -> bool
3434

35+
val modExprParens : Parsetree.module_expr -> bool
36+
3537
val arrowReturnTypExpr : Parsetree.core_type -> bool
3638

3739
val patternRecordRowRhs : Parsetree.pattern -> bool

‎jscomp/syntax/src/res_printer.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,11 @@ and printModuleBinding ~state ~isRec moduleBinding cmtTbl i =
719719
Doc.concat [Doc.text ": "; printModType ~state modType cmtTbl] )
720720
| modExpr -> (printModExpr ~state modExpr cmtTbl, Doc.nil)
721721
in
722+
let modExprDocParens =
723+
if Parens.modExprParens moduleBinding.pmb_expr then
724+
Doc.concat [Doc.lparen; modExprDoc; Doc.rparen]
725+
else modExprDoc
726+
in
722727
let modName =
723728
let doc = Doc.text moduleBinding.pmb_name.Location.txt in
724729
printComments doc cmtTbl moduleBinding.pmb_name.loc
@@ -732,7 +737,7 @@ and printModuleBinding ~state ~isRec moduleBinding cmtTbl i =
732737
modName;
733738
modConstraintDoc;
734739
Doc.text " = ";
735-
modExprDoc;
740+
modExprDocParens;
736741
]
737742
in
738743
printComments doc cmtTbl moduleBinding.pmb_loc

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

+46
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

+48-1
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)
Please sign in to comment.