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

Fix formatting of switch expressions that contain braced cases inside #735

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 @@ -62,6 +62,7 @@
- Fix support for recursive components in JSX V4 https://github.com/rescript-lang/syntax/pull/733
- Fix issue with overlapping labelled argument with default value https://github.com/rescript-lang/syntax/pull/734
- Fix issue with using alias and default value together https://github.com/rescript-lang/syntax/pull/734
- Fix formatting of `switch` expressions that contain braced `cases` inside https://github.com/rescript-lang/syntax/pull/735

#### :eyeglasses: Spec Compliance

Expand Down
8 changes: 7 additions & 1 deletion 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 src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4743,7 +4743,10 @@ and printCases ~customLayout (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 ~customLayout) ~nodes:cases cmtTbl;
];
Expand Down
8 changes: 8 additions & 0 deletions 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 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 => ()
}