Skip to content

Fix module printing #6963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions jscomp/syntax/src/res_parens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions jscomp/syntax/src/res_parens.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
46 changes: 46 additions & 0 deletions jscomp/syntax/tests/printer/modExpr/expected/structure.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
49 changes: 48 additions & 1 deletion jscomp/syntax/tests/printer/modExpr/structure.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,51 @@ module type T = {}
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
}