Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 52d508f

Browse files
authored
Fix formatting of switch expressions that contain braced cases inside (#735)
1 parent 2fd8f0c commit 52d508f

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- Fix support for recursive components in JSX V4 https://github.com/rescript-lang/syntax/pull/733
6363
- Fix issue with overlapping labelled argument with default value https://github.com/rescript-lang/syntax/pull/734
6464
- Fix issue with using alias and default value together https://github.com/rescript-lang/syntax/pull/734
65+
- Fix formatting of `switch` expressions that contain braced `cases` inside https://github.com/rescript-lang/syntax/pull/735
6566

6667
#### :eyeglasses: Spec Compliance
6768

src/res_comments_table.ml

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

src/res_printer.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -4743,7 +4743,10 @@ and printCases ~customLayout (cases : Parsetree.case list) cmtTbl =
47434743
~getLoc:(fun n ->
47444744
{
47454745
n.Parsetree.pc_lhs.ppat_loc with
4746-
loc_end = n.pc_rhs.pexp_loc.loc_end;
4746+
loc_end =
4747+
(match ParsetreeViewer.processBracesAttr n.pc_rhs with
4748+
| None, _ -> n.pc_rhs.pexp_loc.loc_end
4749+
| Some ({loc}, _), _ -> loc.Location.loc_end);
47474750
})
47484751
~print:(printCase ~customLayout) ~nodes:cases cmtTbl;
47494752
];

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

+8
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+
}

tests/printer/expr/switch.res

+8
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)