Skip to content

Commit 095abc8

Browse files
authored
Fix formatting of switch expressions that contain brace cases inside (#6015)
* fix formatting of braced case expression * update CHANGELOG.md
1 parent b21f734 commit 095abc8

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ These are only breaking changes for unformatted code.
6666
- Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/rescript-compiler/pull/6006
6767
- 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
6868
- 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
69+
- Fix formatting of `switch` expressions that contain brace `cases` inside https://github.com/rescript-lang/rescript-compiler/pull/6015
6970

7071
#### :nail_care: Polish
7172

res_syntax/src/res_comments_table.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@ let getLoc node =
345345
let open Parsetree in
346346
match node with
347347
| Case case ->
348-
{case.pc_lhs.ppat_loc with loc_end = case.pc_rhs.pexp_loc.loc_end}
348+
{
349+
case.pc_lhs.ppat_loc with
350+
loc_end =
351+
(match ParsetreeViewer.processBracesAttr case.pc_rhs with
352+
| None, _ -> case.pc_rhs.pexp_loc.loc_end
353+
| Some ({loc}, _), _ -> loc.Location.loc_end);
354+
}
349355
| CoreType ct -> ct.ptyp_loc
350356
| ExprArgument expr -> (
351357
match expr.Parsetree.pexp_attributes with

res_syntax/src/res_printer.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4659,7 +4659,10 @@ and printCases ~state (cases : Parsetree.case list) cmtTbl =
46594659
~getLoc:(fun n ->
46604660
{
46614661
n.Parsetree.pc_lhs.ppat_loc with
4662-
loc_end = n.pc_rhs.pexp_loc.loc_end;
4662+
loc_end =
4663+
(match ParsetreeViewer.processBracesAttr n.pc_rhs with
4664+
| None, _ -> n.pc_rhs.pexp_loc.loc_end
4665+
| Some ({loc}, _), _ -> loc.Location.loc_end);
46634666
})
46644667
~print:(printCase ~state) ~nodes:cases cmtTbl;
46654668
];

res_syntax/tests/printer/expr/expected/switch.res.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ switch route {
5555
<div> {React.string("Second B div")} </div>
5656
</>
5757
}
58+
59+
switch x {
60+
| A => {
61+
let _ = 1
62+
let _ = 2
63+
} // no blank line below
64+
| B => ()
65+
}

res_syntax/tests/printer/expr/switch.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ switch route {
4949
<div> {React.string("Second B div")} </div>
5050
</>
5151
}
52+
53+
switch x {
54+
| A => {
55+
let _ = 1
56+
let _ = 2
57+
} // no blank line below
58+
| B => ()
59+
}

0 commit comments

Comments
 (0)