Skip to content

Fix formatting of switch expressions that contain brace cases inside #6015

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 2 commits into from
Feb 24, 2023
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 @@ -66,6 +66,7 @@ These are only breaking changes for unformatted code.
- Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/rescript-compiler/pull/6006
- Fix issue in the compiler back-end where async functions passed to an `@uncurry` external would be inlined and transformed in a way that loses async https://github.com/rescript-lang/rescript-compiler/pull/6010
- Fix location issue for the treatment of `async` functions where hovering on the body with a type error would show `'a => promise<'a>` everywhere https://github.com/rescript-lang/rescript-compiler/pull/6012
- Fix formatting of `switch` expressions that contain brace `cases` inside https://github.com/rescript-lang/rescript-compiler/pull/6015

#### :nail_care: Polish

Expand Down
8 changes: 7 additions & 1 deletion res_syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,13 @@ let getLoc node =
let open Parsetree in
match node with
| Case case ->
{case.pc_lhs.ppat_loc with loc_end = case.pc_rhs.pexp_loc.loc_end}
{
case.pc_lhs.ppat_loc with
loc_end =
(match ParsetreeViewer.processBracesAttr case.pc_rhs with
| None, _ -> case.pc_rhs.pexp_loc.loc_end
| Some ({loc}, _), _ -> loc.Location.loc_end);
}
| CoreType ct -> ct.ptyp_loc
| ExprArgument expr -> (
match expr.Parsetree.pexp_attributes with
Expand Down
5 changes: 4 additions & 1 deletion res_syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4659,7 +4659,10 @@ and printCases ~state (cases : Parsetree.case list) cmtTbl =
~getLoc:(fun n ->
{
n.Parsetree.pc_lhs.ppat_loc with
loc_end = n.pc_rhs.pexp_loc.loc_end;
loc_end =
(match ParsetreeViewer.processBracesAttr n.pc_rhs with
| None, _ -> n.pc_rhs.pexp_loc.loc_end
| Some ({loc}, _), _ -> loc.Location.loc_end);
})
~print:(printCase ~state) ~nodes:cases cmtTbl;
];
Expand Down
8 changes: 8 additions & 0 deletions res_syntax/tests/printer/expr/expected/switch.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ switch route {
<div> {React.string("Second B div")} </div>
</>
}

switch x {
| A => {
let _ = 1
let _ = 2
} // no blank line below
| B => ()
}
8 changes: 8 additions & 0 deletions res_syntax/tests/printer/expr/switch.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ switch route {
<div> {React.string("Second B div")} </div>
</>
}

switch x {
| A => {
let _ = 1
let _ = 2
} // no blank line below
| B => ()
}