diff --git a/CHANGELOG.md b/CHANGELOG.md index ef10dbd75e..f4f0a957c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080 - Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094 - Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058 +- Fix comments formatted away in function without arguments. https://github.com/rescript-lang/rescript-compiler/pull/7095 #### :nail_care: Polish diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 0c53e9cc00..da02a6c907 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4842,9 +4842,22 @@ and print_arguments_with_callback_in_last_position ~state args cmt_tbl = and print_arguments ~state ?(partial = false) (args : (Asttypes.arg_label * Parsetree.expression) list) cmt_tbl = match args with - | [(Nolabel, {pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)})] - -> - Doc.text "()" + | [ + ( Nolabel, + { + pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _); + pexp_loc = loc; + } ); + ] -> + if has_leading_line_comment cmt_tbl loc then + let cmt = print_comments Doc.nil cmt_tbl loc in + Doc.concat + [ + Doc.lparen; + Doc.indent (Doc.group (Doc.concat [Doc.soft_line; cmt])); + Doc.rparen; + ] + else Doc.text "()" | [(Nolabel, arg)] when ParsetreeViewer.is_huggable_expression arg -> let arg_doc = let doc = print_expression_with_comments ~state arg cmt_tbl in diff --git a/tests/syntax_tests/printer/comments/expected/expr.res.txt b/tests/syntax_tests/printer/comments/expected/expr.res.txt index 8fc8f09702..1085861c82 100644 --- a/tests/syntax_tests/printer/comments/expected/expr.res.txt +++ b/tests/syntax_tests/printer/comments/expected/expr.res.txt @@ -234,6 +234,32 @@ let multiply = ( /* c2 */ m2 /* c3 */, ) => () +f( + // a + // b + // c + ) + +f( + // a + // b + // c + ) + +let x = { + f( + // a + // b + // c + ) + + f( + // a + // b + // c + ) +} + f(() => 1) // c1 f(_ => 1) diff --git a/tests/syntax_tests/printer/comments/expr.res b/tests/syntax_tests/printer/comments/expr.res index 25723e34d5..4130510984 100644 --- a/tests/syntax_tests/printer/comments/expr.res +++ b/tests/syntax_tests/printer/comments/expr.res @@ -223,6 +223,35 @@ let f = (/* c0 */ ~greeting /* c1 */, /* c2 */ ~from /* c3 */ as /* c4 */ hometo let multiply = (/* c-2 */ type t /* c-1 */,/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => () let multiply = (/* c-4 */ type t /* c-3 */,/* c0 */ m1 /* c1 */, /* c-2 */ type s /* c-1 */, /* c2 */ m2 /* c3 */) => () +f( + // a + // b +// c +) + +f( + // a + // b + // c + () +) + +let x = { + f( + // a + // b + // c + ) + + + f( + // a + // b + // c + () + ) +} + f(() // c1 => 1);