diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc49f2fe0..37bbaf41b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - Fix `typeof` parens on functions. https://github.com/rescript-lang/rescript/pull/7643 - Rewatch: Add --dev flag to clean command. https://github.com/rescript-lang/rescript/pull/7622 - Rewatch: Use root package suffix in clean log messages. https://github.com/rescript-lang/rescript/pull/7648 +- Fix inside comment printing for empty dict. https://github.com/rescript-lang/rescript/pull/7654 # 12.0.0-beta.1 diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 3941c57691..17dd46f102 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -1512,7 +1512,7 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = Doc.indent (Doc.concat [ - Doc.soft_line; + (if rows = [] then Doc.nil else Doc.soft_line); Doc.join ~sep:(Doc.concat [Doc.text ","; Doc.line]) (List.map @@ -1523,8 +1523,8 @@ and print_literal_dict_expr ~state (e : Parsetree.expression) cmt_tbl = print_comments doc cmt_tbl e.pexp_loc) rows); ]); - Doc.trailing_comma; - Doc.soft_line; + (if rows = [] then Doc.nil + else Doc.concat [Doc.trailing_comma; Doc.soft_line]); ]) and print_constructor_declarations ~state ~private_flag @@ -4225,6 +4225,7 @@ and print_pexp_apply ~state expr cmt_tbl = Doc.concat [ Doc.text "dict{"; + print_comments_inside cmt_tbl expr.pexp_loc; print_literal_dict_expr ~state key_values cmt_tbl; Doc.rbrace; ] diff --git a/tests/syntax_tests/data/printer/expr/dict.res b/tests/syntax_tests/data/printer/expr/dict.res index 12984008db..0a05246e98 100644 --- a/tests/syntax_tests/data/printer/expr/dict.res +++ b/tests/syntax_tests/data/printer/expr/dict.res @@ -1,6 +1,12 @@ // empty dict let x = dict{} +// empty dict with inside comment +let x = dict{ + // inside comment + +} + // one value let x = dict{"foo": "bar"} diff --git a/tests/syntax_tests/data/printer/expr/expected/dict.res.txt b/tests/syntax_tests/data/printer/expr/expected/dict.res.txt index 25cfcb6ca5..c93760b4d2 100644 --- a/tests/syntax_tests/data/printer/expr/expected/dict.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/dict.res.txt @@ -1,6 +1,11 @@ // empty dict let x = dict{} +// empty dict with inside comment +let x = dict{ + // inside comment +} + // one value let x = dict{"foo": "bar"}