diff --git a/CHANGELOG.md b/CHANGELOG.md index 44659fbe6f..63d97eee57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/res_syntax/src/res_comments_table.ml b/res_syntax/src/res_comments_table.ml index f6ce468db5..ed63735281 100644 --- a/res_syntax/src/res_comments_table.ml +++ b/res_syntax/src/res_comments_table.ml @@ -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 diff --git a/res_syntax/src/res_printer.ml b/res_syntax/src/res_printer.ml index e89e1cba95..ff0937e1e1 100644 --- a/res_syntax/src/res_printer.ml +++ b/res_syntax/src/res_printer.ml @@ -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; ]; diff --git a/res_syntax/tests/printer/expr/expected/switch.res.txt b/res_syntax/tests/printer/expr/expected/switch.res.txt index 852c64b33d..da576e4d29 100644 --- a/res_syntax/tests/printer/expr/expected/switch.res.txt +++ b/res_syntax/tests/printer/expr/expected/switch.res.txt @@ -55,3 +55,11 @@ switch route {
{React.string("Second B div")}
} + +switch x { +| A => { + let _ = 1 + let _ = 2 + } // no blank line below +| B => () +} diff --git a/res_syntax/tests/printer/expr/switch.res b/res_syntax/tests/printer/expr/switch.res index 2807c7f3a6..7d4f8aa0f6 100644 --- a/res_syntax/tests/printer/expr/switch.res +++ b/res_syntax/tests/printer/expr/switch.res @@ -49,3 +49,11 @@ switch route {
{React.string("Second B div")}
} + +switch x { +| A => { + let _ = 1 + let _ = 2 + } // no blank line below +| B => () +}