diff --git a/CHANGES.md b/CHANGES.md index b63805e4d..9681ac34c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -32,6 +32,8 @@ [#2794](https://github.com/reasonml/reason/pull/2794)) - Parse universal type variables in signature items (@anmonteiro, [#2797](https://github.com/reasonml/reason/pull/2797)) +- Fix formatting of callbacks with sequence expressions (@anmonteiro, + [#2799](https://github.com/reasonml/reason/pull/2799)) ## 3.12.0 diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index c197e7472..8bb6d3c95 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -9913,9 +9913,13 @@ let createFormatter () = "=> ", ")" ^ rightWrap in let wrap = - if self#should_preserve_requested_braces retCb - then leftWrap ^ "{", "}" ^ rightWrap - else wrap + match + ( self#should_preserve_requested_braces retCb + , self#isSeriesOfOpensFollowedByNonSequencyExpression + { retCb with pexp_attributes = [] } ) + with + | true, _ | _, false -> leftWrap ^ "{", "}" ^ rightWrap + | _ -> wrap in let right = source_map diff --git a/test/mlFunctions.t/input.ml b/test/mlFunctions.t/input.ml new file mode 100644 index 000000000..3393e361e --- /dev/null +++ b/test/mlFunctions.t/input.ml @@ -0,0 +1,7 @@ +(* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. *) + +let x = + ignore (fun y -> + let y = 4 in + y) + diff --git a/test/mlFunctions.t/run.t b/test/mlFunctions.t/run.t new file mode 100644 index 000000000..049227af4 --- /dev/null +++ b/test/mlFunctions.t/run.t @@ -0,0 +1,23 @@ +Format basic + + $ refmt ./input.ml | tee formatted.re + /* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. */ + + let x = + ignore(y => { + let y = 4; + y; + }); + +Format the formatted file back + $ refmt ./formatted.re | tee formatted_back.re + /* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. */ + + let x = + ignore(y => { + let y = 4; + y; + }); + +Ensure idempotency: first format and second format are the same + $ diff formatted.re formatted_back.re